[英]JTextField not visible until mouse over
I am trying to make a simple calculator program with Java. 我试图用Java编写一个简单的计算器程序。 When I added a JTextField
however it made all the button and the field itself invisible until I hover over it. 但是,当我添加JTextField
时,它使所有按钮和字段本身不可见,直到将鼠标悬停在它上面。 If I comment out the text field everything goes back to normal and all the button are visible. 如果我注释掉文本字段,一切将恢复正常,并且所有按钮都可见。
Here is my code: 这是我的代码:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JFrame {
// Numbers
JButton btn_zero;
JButton btn_one;
JButton btn_two;
JButton btn_three;
JButton btn_four;
JButton btn_five;
JButton btn_six;
JButton btn_seven;
JButton btn_eight;
JButton btn_nine;
// Operators
JButton btn_add;
JButton btn_subtract;
JButton btn_multiply;
JButton btn_divide;
JButton btn_equals;
JButton btn_decimal;
JButton btn_pm;
JButton btn_clear;
// Panel
JPanel buttonPanel;
// Dimensions
final int WIDTH = 340;
final int HEIGHT = 500;
public Calculator() {
// Characteristics of frame
super("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Insets frameInsets = getInsets();
int frameWidth = WIDTH + (frameInsets.left + frameInsets.right);
int frameHeight = HEIGHT + (frameInsets.top + frameInsets.bottom);
setPreferredSize(new Dimension(frameWidth, frameHeight));
//setLayout(null);
pack();
setVisible(true);
// Add values to all buttons
btn_zero = new JButton("0");
btn_one = new JButton("1");
btn_two = new JButton("2");
btn_three = new JButton("3");
btn_four = new JButton("4");
btn_five = new JButton("5");
btn_six = new JButton("6");
btn_seven = new JButton("7");
btn_eight = new JButton("8");
btn_nine = new JButton("9");
btn_add = new JButton("+");
btn_subtract = new JButton("-");
btn_multiply = new JButton("×");
btn_divide = new JButton("÷");
btn_equals = new JButton("=");
btn_decimal = new JButton(".");
btn_pm = new JButton("±");
btn_clear = new JButton("C");
// Adds the panel
buttonPanel = new JPanel();
buttonPanel.setSize(new Dimension(frameWidth, frameHeight));
buttonPanel.setLayout(null);
// Textfield
JTextField AnswerBox = new JTextField ("");
AnswerBox.setBounds(0, 0, 320, 70);
buttonPanel.add(AnswerBox);
//Buttons
btn_decimal.setBounds(70, 100, 50, 50);
buttonPanel.add(btn_decimal);
btn_pm.setBounds(130, 100, 50, 50);
buttonPanel.add(btn_pm);
btn_clear.setBounds(190, 100, 50, 50);
buttonPanel.add(btn_clear);
btn_add.setBounds(250, 100, 50, 50);
buttonPanel.add(btn_add);
btn_subtract.setBounds(250, 160, 50, 50);
buttonPanel.add(btn_subtract);
btn_multiply.setBounds(250, 220, 50, 50);
buttonPanel.add(btn_multiply);
btn_divide.setBounds(250, 280, 50, 50);
buttonPanel.add(btn_divide);
btn_equals.setBounds(10, 350, 290, 50);
buttonPanel.add(btn_equals);
btn_zero.setBounds(190, 160, 50, 170);
buttonPanel.add(btn_zero);
btn_one.setBounds(10, 160, 50, 50);
buttonPanel.add(btn_one);
btn_two.setBounds(70, 160 , 50, 50);
buttonPanel.add(btn_two);
btn_three.setBounds(130, 160, 50, 50);
buttonPanel.add(btn_three);
btn_four.setBounds(10, 220, 50, 50);
buttonPanel.add(btn_four);
btn_five.setBounds(70, 220, 50, 50);
buttonPanel.add(btn_five);
btn_six.setBounds(130, 220, 50, 50);
buttonPanel.add(btn_six);
btn_seven.setBounds(10, 280, 50, 50);
buttonPanel.add(btn_seven);
btn_eight.setBounds(70, 280, 50, 50);
buttonPanel.add(btn_eight);
btn_nine.setBounds(130, 280, 50, 50);
buttonPanel.add(btn_nine);
buttonPanel.setVisible(true);
add(buttonPanel);
}
}
Also the code is also right now just showing the buttons of the calculator, and not actually doing anything, because I want to focus on fixing this bug. 同样,代码现在也只是显示计算器的按钮,实际上不执行任何操作,因为我想集中精力解决此错误。
Call revalidate()
after you add all the widgets to recalculate the layout of the container (in your case a JFrame
): 添加所有小部件后,调用revalidate()
以重新计算容器的布局(在您的情况下为JFrame
):
buttonPanel.setVisible(true);
add(buttonPanel);
revalidate();
Calling revalidate()
at the end would work or you can simply put 最后调用revalidate()
将起作用,或者您可以简单地将
setVisible(true)
to the end of your constructor. 到构造函数的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.