JTextField不工作,但JLabel可以



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

@user3720725:您的关注是有效的。

如果你添加jtextfield而不是JLabel,它将显示空白屏幕。即JTextField Field = new JTextField("Answer");

请参阅java文档中的Container类:

 /**
     * Appends the specified component to the end of this container.
     * This is a convenience method for {@link #addImpl}.
     * <p>
     * This method changes layout-related information, and therefore,
     * invalidates the component hierarchy. If the container has already been
     * displayed, the hierarchy must be validated thereafter in order to
     * display the added component.
     *
     * @param     comp   the component to be added
     * @exception NullPointerException if {@code comp} is {@code null}
     * @see #addImpl
     * @see #invalidate
     * @see #validate
     * @see javax.swing.JComponent#revalidate()
     * @return    the component argument
     */
    public Component add(Component comp)

由于您已经使框架可见,您需要重新验证它。

所以要么在最后调用f.validate();,要么在最后而不是开始调用f.setVisible(true)

MadProgrammer是正确的,试着让它成为一个习惯,按照编码约定编码。

最新更新