简体   繁体   English

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

[英]Why JComponents does not appear in the frame?

I'm pretty new to java. 我是Java的新手。 And i can't get this to work... I'm trying to add components using this code: 而且我无法使它正常工作...我正在尝试使用以下代码添加组件:

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

But when i'm trying to do it, the components aren't showing up on the screen. 但是,当我尝试执行此操作时,组件未显示在屏幕上。

So are there someone who can tell me why it isn't working/Showing up and how i can add it in ? 所以有人可以告诉我为什么它不起作用/显示出来以及如何添加它吗?

Thanks 谢谢

You mean that JLabel and JButton don't appear? 您是说JLabelJButton没有出现? right ? 对 ? not JTextField because there is no JTextField in the code 不是JTextField因为代码中没有JTextField

Anyway just add this line of code to the end: 无论如何,只需将这行代码添加到末尾:

window.add(panel);

Here you are adding the JPanel that contains all the JComponent s to the JFrame 在这里,您将包含所有JComponentJPanel添加到JFrame

and always: have the setVisible(true) call as last call. 并且始终:将setVisible(true)调用作为最后一个调用。 Everything that you add to the window/frame must be done before the setVisible call 必须在setVisible调用之前完成添加到窗口/框架的所有操作

This is how to declare a JTextField 这是如何声明JTextField

final JTextField variableName = new JTextField(size); 最后的JTextField variableName = new JTextField(size);

This is how you get the text from JTextField 这是从JTextField获取文本的方式

variableName.getText() variableName.getText()

Hope that helps. 希望能有所帮助。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM