Java:为什么这个 JFrame 按钮不显示?

标签 java swing

基本上,我不想显示这个按钮。它在我之前的另一个程序中工作,但似乎在这个程序中不起作用,我不知道为什么。如果有人能提供帮助那就太好了。

public void fixtureList()
{
    JButton editButton;

    setLayout(null);

    editButton = new JButton("Edit");
    editButton.setBounds(200, 200, 100, 100);
    add(editButton);

}

public void loginPanel()
{
    setLayout(null);

    JButton loginButton;

    loginButton = new JButton("Login");
    loginButton.setBounds(10, 10, 100, 100);
    add(loginButton);
    loginButton.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            fixtureList();
            System.out.println("Loading the fixtures screen");
        }

    });

}

最佳答案

您忘记调用loginPanel()。尝试:

    Main window = new Main();

    window.setTitle("PE Fixtures v1.0");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.loginPanel();
    window.setSize(250, 430);
    window.getContentPane().setBackground(new Color(53, 56, 64));
    window.setVisible(true);

尽管如此,因为您正在子类化 JFrame,所以我建议在构造函数中完成大部分工作。

关于Java:为什么这个 JFrame 按钮不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802212/

相关文章:

Java : is it possible to have 'nested' map methods (java. util.stream)?

java - 边唱歌边连续语音识别?

java - 如何将事件监听器与 "asking"结合起来进行事件?

java - 如何从 Java 调用 GraphViz

通过 Web 应用程序启动的 Java 套接字连接,在服务器重新启动时重新打开

java - 在 Swing 应用程序中处置元素

java - 在swing中拖动对象,MouseMotionListener是在对象中还是在JPanel中?

java - 如何获取Java Swing列表中未选中的右键单击项?

java - 如何从单个输入扫描不确定的数字列表?

java - 在 paintComponent 中使用变量