2014-01-09 2 views
1

그래서 NetBeans JFrame Form을 온라인 자습서에서 참조하여 레이아웃을 도왔습니다. (필자가 본 적이있는 GUI 교과서에서는 내 교과서에 언급되지 않았습니다!) 어려운 내가 작업하고있는 프로그램의 레이아웃과 시간 (전체 윈도우를 차지하는 대신 텍스트 영역이 작동하여 창 중앙에 머물러있게 할 수는 없습니다!) 저는 시각 보조가 도움이 될 것이라고 생각했습니다. 그러나 당신이 짐작 했겠지만 이미 많은 양의 코드가이 프로그램에 침투했습니다. 새 JFrame Form을 기존 클래스와 연결할 수 있습니까? 그렇다면 어떻게해야할까요? 필요한 경우 제 코드를 제공 할 수 있지만 주요 세 클래스 중 하나에서만 500 행의 코드를 사용하고 있습니다.기존 클래스에 jframe 폼을 추가 할 수 있습니까?

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package theproblem; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.TextArea; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

/** 
* 
* @author Heather 
*/ 
public class TheProblem { 

/** 
* @param args the command line arguments 
*/ 

public static void main(String[] args) { 

    JFrame window2 = new JFrame(); 
    TextArea battleLogging = new TextArea(3,10); 
    JScrollPane logScrollPane = new JScrollPane(battleLogging); 
    JLabel BattleLog = new JLabel(); 
    JLabel p1HPLabel= new JLabel(); 
    JLabel p2HPLabel= new JLabel(); 
    String attack1ButtonContents = "Just an attack"; 
    String attack2ButtonContents = "Just another attack"; 
    JButton attack1=new JButton(attack1ButtonContents); 
    JButton attack2=new JButton(attack2ButtonContents); 


    window2.setLayout(new BorderLayout()); 
    window2.setSize(400,400); 
    JPanel attackPanel = new JPanel(); 
    attackPanel.add(attack1); 
    attackPanel.add(attack2); 
//  attack1 = new JButton(p1A1); 
//  attack2 = new JButton(p1A2); 
//  attack1.addActionListener(new Attack1()); 
//  attack2.addActionListener(new Attack2()); 
    //window2.add(attackPanel, BorderLayout.CENTER); 
    window2.add(battleLogging, BorderLayout.CENTER); 
    battleLogging.setEditable(false); 
    logScrollPane.setVerticalScrollBarPolicy(
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    logScrollPane.setPreferredSize(new Dimension(50, 50)); 
    //battleLogging.setLineWrap(true); 
    //battleLogging.setWrapStyleWord(true); 
    window2.add(BattleLog, BorderLayout.NORTH); 
    window2.add(p1HPLabel, BorderLayout.WEST); 
    window2.add(p2HPLabel, BorderLayout.EAST); 
    window2.setVisible(true); 
    window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 


} 
+0

기존 코드가있는 GUI 작성기를 사용하려고하면 IMHO보다 더 많은 문제가 발생할 수 있습니다. 직접 코드를 작성 하는게 어떨까요? –

+0

문제를 보여주는 [SSCCE] (http://sscce.org)를 만드십시오. – nachokk

+0

내가 한 다음 JTextArea를 추가하여 창에 표시되는 발생 로그를 포함하려고 시도했으며 지정된 BorderLayout의 가운데에 있지 않습니다. 그것은 모든 버튼을 볼 수 없으며 상단의 JLabel을 볼 수없는 곳까지 전체 창을 차지합니다. –

답변

2

다음, 당신은 (실제로는 사용자 정의 클래스 JFrame의 확장) JFrame의를 작성하고 넷빈즈 비주얼 디자이너에서 디자인 할 수 있습니다 직접 귀하의 질문에 대답 기존의 클래스를 인스턴스화합니다. 컴포지션 (http://en.wikipedia.org/wiki/Object_composition)을 사용하고 JFrame에 대한 참조를 기존 클래스의 필드로 사용할 수 있습니다. JFrame에 데이터를 전달하기위한 추가 메소드를 제공 할 수 있습니다.

관련 문제