JButtons 没有被放置在正确的位置或正在消失

JButtons are not being placed in the correct spot or are disappearing

我是一名初级程序员,我正在尝试制作一个简单的计算器,但由于某种原因,按钮似乎无法正常工作,它们要么消失,要么填满整个面板,要么设置在错误的位置。奇怪的是,当我将鼠标悬停并单击它们时,按钮会出现。我可能遗漏了一些非常明显的东西,但请帮忙(代码也不是很干净)。谢谢!

主要

public abstract class Main {
    public static JFrame frame = new JFrame("Calculator");
    public static JPanel panel = new JPanel();
    
    public static CaculatorButton buttons = new CalculatorButton(); 
    
    public static void main(String[] args) {
        Window.window(frame, panel);
        buttons.b0(panel); 
    }
}

计算器按钮

public class CaculatorButton {
    
    private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9; 
    private static JTextArea text;
    private static JButton plus;
    private static JButton minus;
    private static JButton equals; 
    private static String temp;
    
    public void b0(JPanel panel) {
        b0 = new JButton("0");      
        b0.setFocusable(false);
        b0.setBackground(Color.GRAY);
        b0.setPreferredSize(new Dimension(80, 80));
        panel.add(b0);      
        b0.setBounds(10, 395, 80, 80);  
        
        b1(panel); b2(panel); b3(panel); b4(panel); b5(panel); b6(panel); b7(panel); b8(panel); b9(panel); textArea(panel); plus(panel); minus(panel); 
        equals(panel); input();
    }
    
    private static void b1(JPanel panel) {
        b1 = new JButton("1");
        b1.setFocusable(false);
        b1.setBackground(Color.GRAY);
        b1.setPreferredSize(new Dimension(80, 80));
        panel.add(b1);
        b1.setBounds(10, 140, 80, 80);
    }

    private static void b2(JPanel panel) {
        b2 = new JButton("2");
        b2.setFocusable(false);
        b2.setBackground(Color.GRAY);
        b2.setPreferredSize(new Dimension(80, 80));
        panel.add(b2);
        b2.setBounds(95, 140, 80, 80);
    }
    
    private static void b3(JPanel panel) {

        b3 = new JButton("3");
        b3.setFocusable(false);
        b3.setBackground(Color.GRAY);
        b3.setPreferredSize(new Dimension(80, 80));
        panel.add(b3);
        b3.setBounds(180, 140, 80, 80);
    }
    
    private static void b4(JPanel panel) {
        b4 = new JButton("4");
        b4.setFocusable(false);
        b4.setBackground(Color.GRAY);
        b4.setPreferredSize(new Dimension(80, 80));
        panel.add(b4);
        b4.setBounds(10, 225, 80, 80);
    }

    private static void b5(JPanel panel) {
        b5 = new JButton("5");
        b5.setFocusable(false);
        b5.setBackground(Color.GRAY);
        b5.setPreferredSize(new Dimension(80, 80));
        panel.add(b5);
        b5.setBounds(95, 225, 80, 80);
    }

    private static void b6(JPanel panel) {
        b6 = new JButton("6");
        b6.setFocusable(false);
        b6.setBackground(Color.GRAY);
        b6.setPreferredSize(new Dimension(80, 80));
        panel.add(b6);
        b6.setBounds(180, 225, 80, 80);
    }

    private static void b7(JPanel panel) {
        b7 = new JButton("7");
        b7.setFocusable(false);
        b7.setBackground(Color.GRAY);
        b7.setPreferredSize(new Dimension(80, 80));
        panel.add(b7);
        b7.setBounds(10, 310, 80, 80);
    }
    
    private static void b8(JPanel panel) {
        b8 = new JButton("8");
        b8.setFocusable(false);
        b8.setBackground(Color.GRAY);
        b8.setPreferredSize(new Dimension(80, 80));
        panel.add(b8);
        b8.setBounds(95, 310, 80, 80);
    }
    
    private static void b9(JPanel panel) {
        b9 = new JButton("9");
        b9.setFocusable(false);
        b9.setBackground(Color.GRAY);
        b9.setPreferredSize(new Dimension(80, 80));
        panel.add(b9);
        b9.setBounds(180, 310, 80, 80);
    }
    
    private static void plus(JPanel panel) {
        plus = new JButton("+");
        plus.setFocusable(false);
        plus.setBackground(new Color(0, 200, 150));
        plus.setPreferredSize(new Dimension(80, 80));
        panel.add(plus); 
        plus.setBounds(95, 395, 80, 80);
    }
    
    private static void minus(JPanel panel) {
        minus = new JButton("-");
        minus.setFocusable(false);
        minus.setBackground(new Color(0, 200, 150));
        b0.setPreferredSize(new Dimension(80, 80));
        panel.add(minus); 
        minus.setBounds(180, 395, 80, 80);
    }
    
    private static void equals(JPanel panel) {
        equals = new JButton("="); 
        equals.setFocusable(false);
        equals.setBackground(new Color(200, 125, 0));
        b0.setPreferredSize(new Dimension(80, 335));
        panel.add(equals);
        equals.setBounds(265, 140, 80, 335);
    }
    
    private static void input() {

        JButton[] buttons = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
        
        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
                for (int i = 0; i <= 9; i++) {
                    if(e.getSource()== buttons[i]) {
                        String str = Integer.toString(i); 
                        if (temp == null) {
                            temp = str;
                        } else {
                            temp = temp + str;
                            text.setText(temp);
                        }
                    }
                }
            }   
        };
        
        b0.addActionListener(al);
        b1.addActionListener(al);
        b2.addActionListener(al);
        b3.addActionListener(al);
        b4.addActionListener(al);
        b5.addActionListener(al);
        b6.addActionListener(al);
        b7.addActionListener(al);
        b8.addActionListener(al);
        b9.addActionListener(al);
    }

    private static void textArea(JPanel panel) {
        
        text = new JTextArea("");
        panel.add(text);    
        text.setVisible(true);
        text.setFocusable(true);
        text.setForeground(new Color(51,255,255));
        text.setFont(text.getFont().deriveFont(25f));
        text.setBackground(Color.DARK_GRAY);
        text.setBounds(10, 10, 335, 120);
    }
}

Window

public class Window extends JFrame {    
    public static void window(JFrame frame, JPanel panel) {     
        frame.setSize(370, 522);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true); 
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new BorderLayout());
        
        frame.add(panel, BorderLayout.CENTER); 
        panel.setBackground(Color.DARK_GRAY);
    }

}

您的 JPanel 是使用默认构造函数构造的,这意味着它有一个 FlowLayout 作为 LayoutManager。这意味着手动设置每个按钮的边界会产生奇怪的效果。尝试使用您喜欢的适当 LayoutManager。或者创建一个自定义 LayoutManager(通过实现该接口),它不会在重新验证时更改组件的位置。

LayoutManagers上有对应的教程:
https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

注意JPanel 是一个 Container,所以这就是本教程也适用于 JPanel 的原因。您已经在为名为 frameJFrame 使用 LayoutManager(名为 BorderLayout)...您也应该为 JPanel 这样做。默认情况下它有一个 FlowLayout,但是通过手动设置每个 JButton 的边界,可以理解您并不真正需要 FlowLayout,但可能需要其他东西。您可以在您的问题中给我们一张图片,说明您希望布局的外观如何,这样我们就可以为您提供指导 LayoutManager 尝试。

编辑 1:

因为你正在尝试创建一个计算器,我建议尝试使用 GridLayout3 列(以及 0 作为 rows 参数,这将使网格在达到 3 列时始终创建新行)。

不要使用静态变量。不要使用静态方法。这不是正确的设计。

您的 class 可以:

  1. 创建所需的实例变量。
  2. 创建所有按钮并将其添加到面板。

无需为每个按钮创建单独的方法。使用循环创建每个按钮,然后将其添加到面板。面板应该使用布局管理器,这样您就不必担心按钮的 size/location。 GridLayout 将是易于使用的。它将在 row/column 网格中添加按钮。

有关使用此方法的工作示例,请参阅:。它将为您的 class 展示一个包含上述所有建议的更好设计。

The weird thing is that the buttons appear when I hover and click my mouse over them.

应在框架可见之前将组件添加到框架。