Java JFrame对象仅在鼠标悬停时显示

Java JFrame对象仅在鼠标悬停时显示,java,swing,jframe,jpanel,jbutton,Java,Swing,Jframe,Jpanel,Jbutton,我正试图从头开始构建自己的计算器。 当我按现在的方式运行程序时,一些按钮不会显示,直到我用光标悬停在它们上面,并且我的边界都已设置好。但是,当我移动window.setVisible(true)时到我的构造函数的开头,所有对象的每个边界都被正确放置,但是所有我的对象只显示一次鼠标移过 package guitest; import java.awt.Color; import java.awt.TextField; import java.awt.event.ActionEvent; impo

我正试图从头开始构建自己的计算器。 当我按现在的方式运行程序时,一些按钮不会显示,直到我用光标悬停在它们上面,并且我的边界都已设置好。但是,当我移动
window.setVisible(true)时到我的构造函数的开头,所有对象的每个边界都被正确放置,但是所有我的对象只显示一次鼠标移过

package guitest;

import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;    
import static javax.swing.JFrame.*;

public class Frame implements ActionListener{  


public static int Calculator(int n1, int n2){
    
    return n1 + n2;
}

JTextField num1,num2,ans;
JButton calculate, add, sub, pro, div;
JPanel textFields, actions;

Frame(){
    //Window is being created.
    JFrame window = new JFrame("Calculator");
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setResizable(false);
    window.setSize(400, 400);
    
    //Creating panel
    textFields = new JPanel();
    actions = new JPanel();
    
    //Adjusting JPanel
    textFields.setBounds(0, 0, 240, 400);
    actions.setBounds(240, 0, 160, 400);
    
    
    //Creating textfields.
    num1 = new JTextField("Number 1");
    num2 = new JTextField("Number 2");
    ans = new JTextField("Answer");
    ans.setEditable(false);
    
    //Creating calculate button.
    calculate = new JButton("Calclulate");
    add = new JButton("+");
    sub = new JButton("-");
    pro = new JButton("*");
    div = new JButton("/");
    
    //adjusting TextFields to my window
    num1.setBounds(30, 20, 200, 20);
    num2.setBounds(30, 60, 200, 20);
    ans.setBounds(30,100,200,20);
    

    
    //adjusting Buttons to my window
    calculate.setBounds(30, 140, 90, 30);
    
    add.setBounds(20, 20, 50, 50);
    sub.setBounds(75, 20, 50, 50);
    pro.setBounds(20, 75, 50, 50);
    div.setBounds(75, 75, 50, 50);
    
    calculate.addActionListener(this);
    
    //adding to my window
    textFields.add(num1);textFields.add(num2);textFields.add(ans);textFields.add(calculate);
    actions.add(add);actions.add(sub);actions.add(pro);actions.add(div);
    window.add(textFields);window.add(actions);
    
    
    window.setVisible(true);

    
    //Setting everything visible
    //textFields.setVisible(true);actions.setVisible(true);
    //num1.setVisible(true);num2.setVisible(true);ans.setVisible(true);
    //calculate.setVisible(true);add.setVisible(true);sub.setVisible(true);pro.setVisible(true);div.setVisible(true);

    
}

public void actionPerformed(ActionEvent e){
    String n1 = num1.getText();
    String n2 = num2.getText();
    int a = Integer.parseInt(n1);
    int b = Integer.parseInt(n2);
    
    int c;
    c = Calculator(a,b);
    
    String result = String.valueOf(c);
    ans.setText(result);
    
}

public static void main(String[] args) { 
    new Frame();
    
}  


} 


(我已手动将鼠标悬停在所有对象上。)

我更改了您的代码并使用布局,而不是
setBouds()

Swing提供不同的LayoutManager,它们用于以特定方式排列组件。以下类用于表示Swing中的布局管理器:

1) java.awt.BorderLayout
2) java.awt.FlowLayout
3) java.awt.GridLayout
4) java.awt.CardLayout
5) java.awt.GridBagLayout
6) javax.swing.BoxLayout
7) javax.swing.GroupLayout
8) javax.swing.ScrollPaneLayout
9) javax.swing.SpringLayout etc.
它们中的每一个都将按照特定的顺序排列组件,您可以在此处()中了解更多有关它的信息


停止执行
.setBounds(…)
。Swing本不应该以这种方式使用,这样做是在对抗库和布局管理器。作弊解决方案是将布局管理器设置为
null
,但这只会延续坏习惯,并允许创建仅在一个系统上工作的GUI。真正的解决方案是学习和使用布局管理器。请不要简单地提供一个代码转储答案,还要解释它。你认为是什么导致了OP的问题?您的代码如何解决这个问题?任何其他对原始海报和(最重要的)所有有类似问题的未来网站访问者有用的信息?
public class Frame implements ActionListener {

    public static int Calculator(int n1, int n2) {

        return n1 + n2;
    }

    JTextField num1, num2, ans;
    JButton calculate, add, sub, pro, div;
    JPanel textFields, actions;

    Frame() {
        // Window is being created.
        JFrame window = new JFrame("Calculator");
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setSize(400, 400);
        window.setLayout(new BorderLayout());
        // Creating panel

        // Creating textfields.
        num1 = new JTextField("Number 1");
        num2 = new JTextField("Number 2");
        ans = new JTextField("Answer");
        ans.setEditable(false);

        // Creating calculate button.
        calculate = new JButton("Calclulate");
        add = new JButton("+");
        sub = new JButton("-");
        pro = new JButton("*");
        div = new JButton("/");

        calculate.addActionListener(this);

        textFields = new JPanel();
        actions = new JPanel();

        GridLayout txtBox = new GridLayout(4, 1);
        txtBox.setVgap(20);
        textFields.setLayout(txtBox);
        
        GridLayout actionBox = new GridLayout(2, 2);
        actions.setLayout(actionBox);
        actions.setPreferredSize(new Dimension(100, 100));
        
        // adding to my window
        textFields.add(num1);
        textFields.add(num2);
        textFields.add(ans);
        textFields.add(calculate);
        actions.add(add);
        actions.add(sub);
        actions.add(pro);
        actions.add(div);
        
        window.setLayout(new FlowLayout());
        
        window.add(textFields);
        window.add(actions);

        window.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        String n1 = num1.getText();
        String n2 = num2.getText();
        int a = Integer.parseInt(n1);
        int b = Integer.parseInt(n2);

        int c;
        c = Calculator(a, b);

        String result = String.valueOf(c);
        ans.setText(result);

    }

    public static void main(String[] args) {
        new Frame();

    }
}