gpt4 book ai didi

java - JTextField 不起作用,但 JLabel 起作用?

转载 作者:太空宇宙 更新时间:2023-11-04 14:48:20 26 4
gpt4 key购买 nike

所以,这是一个应该是计算器的程序,但我仍在开发 GUI。所以这是我的问题:我已经检查过它并且工作正常,但是当我写 Field= new JtextField(); 时然后它停止工作(打开空白窗口)。然而,Jlabel 工作正常......

代码如下:

public static void main(String[] args) {
GridBagLayout l;
JButton[] numberButtons;
JButton ADD;
JButton MINUS;
JButton MULTIPLY;
JButton DIVIDE;
JButton EQUALS;
JButton DECIMAL;
JButton PLUSMINUS;
JButton CLEAR;
JFrame f;
JPanel p;
JLabel Field;
String Serif = null;

l = new GridBagLayout();
l.columnWidths = new int[] { 88, 88, 88, 88 };
l.rowHeights = new int[] { 88, 88, 88, 88, 88, 88 };

numberButtons = new JButton[10];
int[][] numConstraints = new int[][] {
{ 0, 5, 2, 1 },
{ 0, 4, 1, 1 },
{ 1, 4, 1, 1 },
{ 2, 4, 1, 1 },
{ 0, 3, 1, 1 },
{ 1, 3, 1, 1 },
{ 2, 3, 1, 1 },
{ 0, 2, 1, 1 },
{ 1, 2, 1, 1 },
{ 2, 2, 1, 1 },
};
f = new JFrame("Satvir's Calculator");
f.setVisible(true);
f.setSize(360, 540);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);

p = new JPanel(l);
EQUALS = new JButton("=");

GridBagConstraints c = new GridBagConstraints();

for (int i = 0; i < numberButtons.length; i++) {
numberButtons[i] = new JButton("" + i);
c.gridx = numConstraints[i][0];
c.gridy = numConstraints[i][1];
c.gridwidth = numConstraints[i][2];
c.gridheight = numConstraints[i][3];
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(2, 2, 2, 2);
p.add(numberButtons[i], c);
}

PLUSMINUS = new JButton("+/-");
CLEAR = new JButton("C");
ADD = new JButton("+");
MINUS = new JButton("-");
DIVIDE = new JButton("÷");
EQUALS = new JButton("=");
DECIMAL = new JButton(".");
MULTIPLY = new JButton("x");
c.gridx = 3;
c.gridy = 3;
p.add(ADD, c);
c.gridx = 3;
c.gridy = 2;
p.add(MINUS, c);
c.gridx = 3;
c.gridy = 1;
p.add(MULTIPLY, c);
c.gridx = 2;
c.gridy = 1;
p.add(DIVIDE, c);
c.gridx = 1;
c.gridy = 1;
p.add(PLUSMINUS, c);
c.gridx = 0;
c.gridy = 1;
p.add(CLEAR, c);
c.gridx = 2;
c.gridy = 5;
p.add(DECIMAL, c);
c.gridx = 3;
c.gridy = 4;
c.gridheight = 2;
p.add(EQUALS, c);
// c.gridx=0;
// c.gridy = 0;
// c.gridwidth =4;
// c.gridheight =1;
// p.add(Field,c);

// gridx = 3;
// gridy= 4;
// gridheight =2;
// gridwidth = 1;
// add(EQUALS,c);
Field = new JLabel("Answer");
Field.setBorder(BorderFactory.createLineBorder(Color.BLACK));
Field.setOpaque(true);
Field.setBackground(Color.WHITE);
Field.setHorizontalAlignment(SwingConstants.RIGHT);
Field.setBorder(new EmptyBorder(10, 10, 10, 10));
Field.setFont(new Font(Serif, Font.PLAIN, 30));
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 4;
p.add(Field, c);
p.setBackground(Color.cyan);

f.add(p);
}

最佳答案

Field 是示例 JLabel Field 中的类型 JLabel

您所做的是尝试使用 JTextField 实例化 JLabel

它当然会捕获异常并关闭应用程序,因为 JTextField 不是从 JLabel 继承的。

关于java - JTextField 不起作用,但 JLabel 起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24111861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com