【问题标题】:JTextField does not work but JLabel does?JTextField 不起作用,但 JLabel 可以吗?
【发布时间】:2014-06-08 23:15:14
【问题描述】:

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

【问题讨论】:

标签: java swing jlabel jtextfield helper


【解决方案1】:

Field 是您的示例 JLabel 中的一个类型 JLabel Field;

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

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

【讨论】:

    【解决方案2】:

    @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 是正确的,请尽量养成按照编码约定进行编码的习惯。
    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多