JTextField不起作用,但JLabel呢?

时间:2014-06-08 23:15:14

标签: 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);
}

2 个答案:

答案 0 :(得分:0)

Field是您的示例JLabel

中的JLabel Field;类型

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

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

答案 1 :(得分:0)

@ user3720725:您的担忧是有效的。

如果添加JLabel的JTextFiled,它将显示空白屏幕。 即JTextField Field = new JTextField("Answer");

请参阅类文档容器的java文档:

 /**
     * 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是正确的,尝试按照编码惯例制定代码习惯 希望它有所帮助。