我正在尝试使用 Java 制作一个简单的计算器。我用于创建 GUI 的代码如下。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JButton one, two, three, four, five, six, seven, eight, nine, zero, plus, minus,
multiply, divide, equalTo, point;
private JPanel panelForResult, panelForKeys;
private JLabel Result;
private JTextField result;
public Calculator() {
}
@Override
public void actionPerformed(ActionEvent ae) {
}
public static void main(String... args) {
Calculator calcFrame = new Calculator();
calcFrame.setSize(330, 400);
calcFrame.setVisible(true);
calcFrame.setResizable(false);
calcFrame.createCalcGUI();
}
private void createCalcGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setBackground(Color.blue);
window.setSize(400, 400);
FlowLayout windowLayout = new FlowLayout();
windowLayout.setHgap(50);
window.setLayout(windowLayout);
panelForKeys = new JPanel();
panelForKeys.setBackground(Color.CYAN);
panelForKeys.setPreferredSize(new Dimension(200, 250));
FlowLayout buttonLayout = new FlowLayout();
// buttonLayout.setAlignOnBaseline(true);
panelForKeys.setLayout(buttonLayout);
panelForResult = new JPanel();
panelForResult.setBackground(Color.CYAN);
panelForResult.setPreferredSize(new Dimension(200, 50));
panelForResult.setLayout(new FlowLayout());
Result = new JLabel("=");
result = new JTextField();
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
zero = new JButton("0");
plus = new JButton("+");
minus = new JButton("-");
multiply = new JButton("*");
divide = new JButton("÷");
equalTo = new JButton("=");
point = new JButton(". ");
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
divide.addActionListener(this);
multiply.addActionListener(this);
equalTo.addActionListener(this);
point.addActionListener(this);
panelForKeys.add(one);
panelForKeys.add(two);
panelForKeys.add(three);
panelForKeys.add(four);
panelForKeys.add(five);
panelForKeys.add(six);
panelForKeys.add(seven);
panelForKeys.add(eight);
panelForKeys.add(nine);
panelForKeys.add(zero);
panelForKeys.add(minus);
panelForKeys.add(plus);
panelForKeys.add(multiply);
panelForKeys.add(divide);
panelForKeys.add(equalTo);
panelForKeys.add(point);
window.add(panelForResult);
window.add(this.panelForKeys);
panelForResult.add(Result);
panelForResult.add(result);
}
}
每当我创建实例JTextField
并将其添加panelForResult
到整个容器窗口中时,都会变成蓝色。如果我注释掉JTextField
它就可以了。我只是Java的初学者,我可以知道造成这种情况的可能原因以及如何纠正它吗?