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

标签 java swing jlabel jtextfield helper

所以,这是一个应该是计算器的程序,但我仍在开发 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/

相关文章:

java - 在没有 ODBC 的情况下从 Java 操作 Access 数据库

java - 如何使用 JSlider 更改 JLabel 背景颜色

java - 鼠标滑过 JLabel 时不显示文本

Java Swing - 制作透明 JButtons、不透明边框

java - 混合音频安卓

java - 使用旧实例的paintComponent

java - 关于 ActivityThread 中使用的 final 关键字的混淆

java - 将 jTable 行及其网格线复制到 Excel/Word 文档中

java - 关闭 JDialog Java

java - 创建按下 JButton 时返回的方法