2

我的 JLabel 调整了我在这里制作的 GUI 上的 JButton 的大小。有没有办法让名为 answer 的 JLabel 不调整我的按钮大小?这是我的代码:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Calc extends JFrame {
    JLabel prompt1, prompt2, answer;
    JTextField field1, field2;
    JButton add, sub, times, div;

    Calc() {
        super("My First Calculator!");
        setSize(500,500);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();    

        prompt1 = new JLabel("<html><body>1<sup>st</sup></body></body>");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    add(prompt1, c);

    field1 = new JTextField(10);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 3;
    add(field1, c);

    prompt2 = new JLabel("<html><body>2<sup>nd</sup></body></body>");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    add(prompt2, c);

    field2 = new JTextField(10);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 3;
    add(field2, c);

    add = new JButton("+");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 1;
    add(add, c);

    sub = new JButton("-");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    c.gridy = 2;
    add(sub, c);

    times = new JButton("x");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 2;
    c.gridy = 2;
    add(times, c);

    div = new JButton("<html><body><p>&divide;</p></body></html>");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 3;
    c.gridy = 2;
    add(div, c);

        JPanel p1 = new JPanel();

        answer = new JLabel("");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 2;
        c.gridy = 4;
        add(answer, c);

       event a = new event();
       add.addActionListener(a);
       sub.addActionListener(a);
       times.addActionListener(a);
       div.addActionListener(a);
    }

//    ################ ActionListener ################

    public class event implements ActionListener {
    public void actionPerformed(ActionEvent a) {
            double fnum, snum;
            double ans;

            try {
                fnum = Double.parseDouble(field1.getText());

            } catch(NumberFormatException e) {
                answer.setText("Illegal Data Entered (Error O1)");
                answer.setForeground(Color.RED);
                return;
            }
            try {
                snum = Double.parseDouble(field2.getText());
            } catch (NumberFormatException e){
                answer.setText("Illegal Data Entered (Error 01)");
                answer.setForeground(Color.RED);
                return;
            }            
            String operation = a.getActionCommand();

            if(operation.equals("+")) {
                ans = fnum + snum;
                answer.setText(fnum + " + " + snum + " = " + ans);
                answer.setForeground(Color.BLUE);
            } else if(operation.equals("-")) {
                ans = fnum - snum;
                answer.setText(fnum + " - " + snum + " = " + ans);
                answer.setForeground(Color.BLUE);
            } else if(operation.equals("x")){
                ans = fnum * snum;
                answer.setText(fnum + " x " + snum + " = " + ans);
                answer.setForeground(Color.BLUE);
            } else if(operation.equals("/")) {
                ans = fnum / snum;
                answer.setText(fnum + " / " + snum + " = " + ans);
                answer.setForeground(Color.BLUE);
            }

        } 

    }

    public static void main(String[] args) {
        new Calc().setVisible(true);
    }
}

当我运行程序时,它显示如下在此处输入图像描述

当您输入数据并点击按钮时,按钮会改变大小。我该如何阻止它在此处输入图像描述

4

1 回答 1

5

而不是JLabel只填充一个单元格,这将影响该列中的所有其他组件,您可以调整gridwidth以允许答案标签“溢出”到其他列中

也许,像...

answer = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = 0;
c.gridy = 4;
c.anchor = GridBagConstraints.WEST;
add(answer, c);

这将允许答案完全填满第 4 行,但会向左对齐

更新

正如 Hovercraft 所评论的,您还可以使用复合布局管理器来满足您的要求,将 UI 的各种元素添加到子面板,然后将它们添加回主面板......

于 2013-06-04T02:25:21.137 回答