为什么JComponents没有出现在框架中?

时间:2015-10-23 12:37:09

标签: java

我对java很新。我无法让这个工作......我正在尝试使用此代码添加组件:

    public class Board
    {
        public static void main(String[] args) {
           JFrame window = new JFrame("Tellraw Generator");
           window.setVisible(true);
           window.setSize(400, 600);
           window.setResizable(false);
           window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JPanel panel = new JPanel();
           JLabel label = new JLabel();
           panel.setLayout(null);
           window.add(panel);
           //"Generate" Button
           JButton button1 = new JButton("Generate");
           button1.setBounds(262, 485, 100, 37);
           panel.add(button1);
           //"Add Text" Button
           JButton button2 = new JButton("Add Text");
           button2.setBounds(51, 337, 88, 33);
           panel.add(button2);
           //Title
           JLabel txt1 = new JLabel("Tellraw Generator");
           txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
           txt1.setBounds(61, 18, 278, 30);
           panel.add(txt1);
        }
    }

但是当我尝试这样做时,组件没有显示在屏幕上。

那么有人可以告诉我它为什么不起作用/出现以及我如何添加它?

由于

4 个答案:

答案 0 :(得分:0)

您的意思是JLabelJButton没有出现?对 ?不是JTextField,因为代码中没有JTextField

无论如何只需将这行代码添加到最后:

window.add(panel);

您要将包含所有JPanel的{​​{1}}添加到JComponent

答案 1 :(得分:0)

始终:将setVisible(true)电话作为最后一次通话。您添加到窗口/框架的所有内容都必须在setVisible调用之前完成

答案 2 :(得分:0)

这是声明JTextField

的方法
  

final JTextField variableName = new JTextField(size);

这是从JTextField获取文本的方法

  

variableName.getText()

希望有所帮助。

答案 3 :(得分:0)

试试这个:

public class Board
    {
        public static void main(String[] args) {
           JFrame window = new JFrame("Tellraw Generator");
           window.setVisible(true);
           window.setSize(400, 600);
           window.setResizable(false);
           window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JPanel panel = new JPanel();
           JLabel label = new JLabel();
           panel.setLayout(null);
           window.add(panel);
           //"Generate" Button
           JButton button1 = new JButton("Generate");
           button1.setBounds(262, 485, 100, 37);
           panel.add(button1);
           //"Add Text" Button
           JButton button2 = new JButton("Add Text");
           button2.setBounds(51, 337, 88, 33);
           panel.add(button2);
           //Title
           JTextField txt1 = new JTextField ();
           txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
           txt1.setBounds(61, 18, 278, 30);
           panel.add(txt1);
        }
    }
相关问题