我的 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>÷</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);
}
}
当我运行程序时,它显示如下
当您输入数据并点击按钮时,按钮会改变大小。我该如何阻止它