Java 如何使JLabel不影响GridBagLayout中其他元素的大小
我的JLabel调整了我在这里制作的GUI上的JButtons的大小。有没有一种方法可以使名为answer的JLabel不调整我的按钮大小?这是我的密码:Java 如何使JLabel不影响GridBagLayout中其他元素的大小,java,swing,jframe,actionlistener,layout-manager,Java,Swing,Jframe,Actionlistener,Layout Manager,我的JLabel调整了我在这里制作的GUI上的JButtons的大小。有没有一种方法可以使名为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 ja
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);
}
}
导入java.awt.Color;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
公共类计算扩展JFrame{
JLabel prompt1,prompt2,回答;
JTextField字段1、字段2;
JButton add、sub、times、div;
Calc(){
超级(“我的第一个计算器!”);
设置大小(500500);
可设置大小(真);
setDefaultCloseOperation(关闭时退出);
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
prompt1=新的JLabel(“第一”);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=0;
c、 gridy=0;
添加(prompt1,c);
field1=新的JTextField(10);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=1;
c、 gridy=0;
c、 网格宽度=3;
添加(字段1,c);
prompt2=新的JLabel(“第二”);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=0;
c、 gridy=1;
c、 网格宽度=1;
添加(prompt2,c);
field2=新的JTextField(10);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=1;
c、 gridy=1;
c、 网格宽度=3;
添加(字段2,c);
add=新的JButton(“+”);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=0;
c、 gridy=2;
c、 网格宽度=1;
添加(添加,c);
sub=新的JButton(“-”);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=1;
c、 gridy=2;
增加(分段,c);
时间=新的按钮(“x”);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=2;
c、 gridy=2;
加(次,c);
div=新的JButton(“÷;”;
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=3;
c、 gridy=2;
加上(c组);
JPanel p1=新的JPanel();
答案=新的JLabel(“”);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=2;
c、 gridy=4;
加(答复c);
事件a=新事件();
add.addActionListener(a);
sub.addActionListener(a);
times.addActionListener(a);
div.addActionListener(a);
}
//行动听众################
公共类事件实现ActionListener{
已执行的公共无效操作(操作事件a){
双fnum,snum;
双ans;
试一试{
fnum=Double.parseDouble(field1.getText());
}捕获(数字格式){
response.setText(“输入非法数据(错误O1)”);
回答:设置前景(颜色:红色);
返回;
}
试一试{
snum=Double.parseDouble(field2.getText());
}捕获(数字格式){
response.setText(“输入非法数据(错误01)”);
回答:设置前景(颜色:红色);
返回;
}
字符串操作=a.getActionCommand();
if(操作.等于(“+”)){
ans=fnum+snum;
answer.setText(fnum+“+”+snum+“=”+ans);
回答:设置前景(颜色:蓝色);
}else if(运算.等于(“-”){
ans=fnum-snum;
response.setText(fnum+“-”+snum+“=”+ans);
回答:设置前景(颜色:蓝色);
}else if(运算等于(“x”)){
ans=fnum*snum;
response.setText(fnum+“x”+snum+“=”+ans);
回答:设置前景(颜色:蓝色);
}else if(操作.等于(“/”){
ans=fnum/snum;
response.setText(fnum+“/”+snum+“=”+ans);
回答:设置前景(颜色:蓝色);
}
}
}
公共静态void main(字符串[]args){
新计算().setVisible(真);
}
}
当我运行程序时,它显示如下
当你输入数据并按下按钮时,按钮的大小会改变。如何停止该操作,而不是让
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作为一个布局来处理所有类型的布局。