0

我正在尝试绘制一个简单的 GUI(在 Eclipse 中使用 windowbuilder 创建),我希望它们之间有 2 个按钮和一个可滚动的文本区域。我创建了以下代码来实现上述目的:

public class Main extends JFrame implements ActionListener{
    public Font font; //used for the font file
    public JTextArea txtDataWillBe;

     public Main() throws FontFormatException, IOException{
        setTitle("Main title ");

        setBounds(100, 100, 1200, 600);
        getContentPane().setLayout(null);

        txtDataWillBe = new JTextArea();
        txtDataWillBe.setText("Your data will display here");
        txtDataWillBe.setFont(new Font("Droid Sans", Font.BOLD, 18));
        txtDataWillBe.setEditable(false);
        txtDataWillBe.setColumns(1);
        txtDataWillBe.setBounds(0, 40, 919, 484);
        getContentPane().add(txtDataWillBe);

        JButton button = new JButton("CLICK TO OPEN");
        button.setBounds(0, 0, 940, 40);
        button.setFont(new Font("Coalition", Font.PLAIN, 18));
        getContentPane().add(button);

        JButton btnPrint = new JButton("PRINT");
        btnPrint.setBounds(0, 531, 940, 40);
        btnPrint.setFont(new Font("Coalition", Font.PLAIN, 18));
        getContentPane().add(btnPrint);

    }

        private final String JTextFile = null;
        JFileChooser chooser;
        String choosertitle;

        public static File deletefile; 

编辑:

 public static void main(String s[]) {

            JFrame frame = new JFrame("Reader");
            Main panel = null;
            try {
                panel = new Main();
            } catch (FontFormatException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            frame.addWindowListener(
              new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    File deleteme = new File (deletefile + "mx.txt");
                    deleteme.delete();
                  System.exit(0);
                  }
                }
              );
            frame.getContentPane().add(panel,"Center");
            frame.setSize(panel.getPreferredSize());
            frame.setVisible(true);
            }

我最初在 JScrollPane 中有 JTextarea (认为这是获得我想要的滚动的最佳方式)。我删除了导致控制台错误的 JScrollPane 想法,但我仍然收到错误消息。

控制台输出为:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Container.java:439)
    at java.awt.Container.addImpl(Container.java:1035)
    at java.awt.Container.add(Container.java:923)

编辑:上面添加了主要内容。

我的 GUI 做错了什么?
我需要 JScrollPane 和 JTextArea 来启用加载文本的垂直滚动吗?

谢谢你的帮助;

安迪

编辑:

我已根据以下建议进行了编辑,因此我的代码现在为:

 public Main() throws FontFormatException, IOException{


        JFrame frame = new JFrame("Reader ");
          frame.addWindowListener(
                  new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        File deleteme = new File (deletefile + "mx.txt");
                        deleteme.delete();
                      System.exit(0);
                      }
                    }
                  );
                frame.getContentPane().add(panel,"Center");
                frame.setSize(getPreferredSize());
                frame.setVisible(true);

其余代码与以前一样,但我所显示的只是一个空白的灰色框架,没有我的任何组件(尽管它们都显示在 windowbuilder 中)。

感谢您的持续帮助。

4

1 回答 1

3

控制台输出准确描述了这里出了什么问题。

IllegalArgumentException: adding a window to a container

frame.getContentPane().add(panel,"Center");您添加panel到内容窗格的行中,但panel它本身是Main extends JFrame.

您应该完全删除对外部框架的任何引用,只需将窗口侦听器添加到Main框架中,即主代码简化为类似

JFrame frame = new Main();
frame.addWindowListener( ... );
frame.setVisible(true);

您可能还想addWindowListener在 class 内移动部件Main

于 2013-04-11T12:51:12.430 回答