【问题标题】:How to make JLabel not effect size of other elements in GridBagLayout如何使 JLabel 不影响 GridBagLayout 中其他元素的大小
【发布时间】:2013-06-04 02:23:14
【问题描述】:

我的 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);
    }
}

当我运行程序时,它显示如下

当您输入数据并点击按钮时,按钮会改变大小。我该如何阻止

【问题讨论】:

    标签: java swing jframe actionlistener layout-manager


    【解决方案1】:

    而不是让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 的各种元素添加到子面板,然后将它们添加回主面板...

    【讨论】:

    • 或者不要尝试将 GridBagLayout 用作所有布局的单一布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2011-05-29
    • 2019-01-27
    相关资源
    最近更新 更多