是否可以将jframe表单添加到现有类中?

时间:2014-01-09 14:34:00

标签: java swing jframe netbeans-7 jtextarea

所以我刚刚在在线教程中找到了Netbeans JFrame Form来帮助布局(我的教科书在GUI部分没有提到它,至少我已经看过了!)因为我有一个我正在处理的程序布局困难时期(我不能让文本区域表现自己并保持在窗口的中心而不是占据整个窗口!)我认为视觉辅助可能会有所帮助但是,正如您可能已经猜到的那样,我已经有大量的代码沉入这个程序中。是否可以将新的JFrame表单与现有类链接?如果是这样,我该怎么做呢?如果需要,我可以提供我的代码,但我们在主要的三个类中只讨论了500行代码。

/*
* 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);
}


}

1 个答案:

答案 0 :(得分:2)

要直接回答您的问题,您可以创建一个JFrame(实际上是一个扩展JFrame的自定义类)并在Netbeans可视化设计器中进行设计,然后在现有类中实例化它。您可以使用合成(http://en.wikipedia.org/wiki/Object_composition)并将JFrame引用为现有类中的字段。您可以在JFrame中提供其他方法以将数据传递给它。

相关问题