Java一半的JButton在JPanel中停止工作
我正试图用jpanel中的Jbuttons制作一个单位转换程序。到目前为止,当我运行程序时,第一个窗口弹出,我选择“长度”。现在,每侧显示8个按钮,选择其中一个后,一半按钮将不再工作,只有每侧的右栏工作Java一半的JButton在JPanel中停止工作,java,jpanel,jbutton,Java,Jpanel,Jbutton,我正试图用jpanel中的Jbuttons制作一个单位转换程序。到目前为止,当我运行程序时,第一个窗口弹出,我选择“长度”。现在,每侧显示8个按钮,选择其中一个后,一半按钮将不再工作,只有每侧的右栏工作 public class Gui extends JFrame { private JButton Subject[] = new JButton[8]; private String SubjNames[] = {"Length", "Mass", "Currency", "Temperat
public class Gui extends JFrame {
private JButton Subject[] = new JButton[8];
private String SubjNames[] = {"Length", "Mass", "Currency", "Temperature", "Time", "Speed", "Data", "Cooking"};
private JButton Length1[] = new JButton[8];
private JButton Length2[] = new JButton[8];
private String LengNames[] = {"inches", "feet", "yards", "miles", "millimeters", "centimeters", "meters", "kilometers"};
private JTextField convertedFrom;
private JTextField amountFrom;
private JTextField convertedTo;
private JTextField amountTo;
private String from;
private String CTo;
private String ATo;
private int SubjectLocX = 40;
private int SubjectLocY = 50;
private int Length1LocX = 40;
private int Length1LocY = 150;
private int Length2LocX = 330;
private int Length2LocY = 150;
private int t = 0;
public Gui (){
super("Converter");
setLayout(null);
System.out.println("yes");
for (int i = 0; i<8; i++) {
Subject[i] = new JButton(SubjNames[i]);
Subject[i].setLocation(SubjectLocX,SubjectLocY);
Subject[i].setSize(200,50);
add(Subject[i]);
if (i < 3) {
SubjectLocX = 40;
SubjectLocY += 100;
} else if (i == 3) {
SubjectLocX = 330;
SubjectLocY = 50;
} else if (i > 3) {
SubjectLocY += 100;
}
}
HandlerClass handler = new HandlerClass();
for (int i = 0; i<8; i++) {
Subject[i].addActionListener(handler);
}
for (int i = 0; i<8; i++) {
Length1[i] = new JButton(SubjNames[i]);
Length2[i] = new JButton(SubjNames[i]);
}
for (int i = 0; i<8; i++) {
Length1[i].addActionListener(handler);
Length2[i].addActionListener(handler);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,500);
setLocation(400,200);
setVisible(true);
}
public void Step2() {
for (int i = 0; i<8; i++) {
remove(Subject[i]);
}
for (int i = 0; i<8; i++) {
remove(Length1[i]);
remove(Length2[i]);
}
HandlerClass handler = new HandlerClass();
convertedFrom = new JTextField(from, 20);
convertedFrom.setEditable(false);
convertedFrom.setLocation(40,50);
convertedFrom.setSize(200,30);
add(convertedFrom);
convertedTo = new JTextField(CTo, 20);
convertedTo.setEditable(false);
convertedTo.setLocation(330,50);
convertedTo.setSize(200,30);
add(convertedTo);
amountFrom = new JTextField("amount", 20);
amountFrom.setLocation(40,100);
amountFrom.setSize(200,30);
add(amountFrom);
amountTo = new JTextField(ATo, 20);
amountTo.setEditable(false);
amountTo.setLocation(330,100);
amountTo.setSize(200,30);
add(amountTo);
for (int i = 0; i<8; i++) {
Length1[i].setLocation(Length1LocX, Length1LocY);
Length1[i].setSize(90, 50);
add(Length1[i]);
if (i < 3) {
Length1LocX = 40;
Length1LocY += 100;
} else if (i == 3) {
Length1LocX = 150;
Length1LocY = 150;
} else if (i > 3) {
Length1LocY += 100;
}
Length2[i].setLocation(Length2LocX, Length2LocY);
Length2[i].setSize(90, 50);
add(Length2[i]);
if (i < 3) {
Length2LocX = 330;
Length2LocY += 100;
} else if (i == 3) {
Length2LocX = 440;
Length2LocY = 150;
} else if (i > 3) {
Length2LocY += 100;
}
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,600);
setLocation(400,200);
setVisible(true);
}
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
for (int i = 0; i<8; i++) {
if (event.getSource() == Length1[i]) {
from = event.getActionCommand();
}
if (event.getSource() == Length2[i]) {
CTo = event.getActionCommand();
}
}
Step2();
}
}
}`
公共类Gui扩展JFrame{
私有JButton Subject[]=新JButton[8];
私有字符串名称[]={“长度”、“质量”、“货币”、“温度”、“时间”、“速度”、“数据”、“烹饪”};
私有JButton Length1[]=新JButton[8];
私有JButton Length2[]=新JButton[8];
私有字符串长度名称[]={“英寸”、“英尺”、“码”、“英里”、“毫米”、“厘米”、“米”、“公里”};
私有JTextField从转换而来;
私有JTextField amountFrom;
私有JTextField转换为;
私人JTextField amountTo;
来自的私有字符串;
私有字符串CTo;
私人字符串ATo;
私有int SubjectLocX=40;
私有int SubjectLocY=50;
专用int长度1LOCX=40;
专用内部长度1LOCY=150;
专用int长度2LOCX=330;
专用int长度2LOCY=150;
私有整数t=0;
公共Gui(){
超级(“转换器”);
setLayout(空);
System.out.println(“是”);
对于(int i=0;i 3){
受试者LOCY+=100;
}
}
HandlerClass handler=新的HandlerClass();
对于(int i=0;i您正在创建和添加JTextField UI项,并在同一位置多次添加它们,但从不删除它们:
- 转换自
- 数量
- 转化为
- 数量
这些项将堆叠在上一个项上或下一个项下;用户可以向上一个项添加文本,程序可以向下一个项下的模糊项写入或读取,这将使程序无法按预期运行。您正在创建和添加JTextField UI项,并在同一位置多次添加它们,并且从不删除它们鼓励他们:
- 转换自
- 数量
- 转化为
- 数量
这些按钮将堆叠在前一个按钮的上方或下方;用户可以向上方的按钮添加文本,程序可以向下方的模糊按钮写入或读取,这将停止程序按预期运行。当您选择16个按钮中的一个时,方法步骤2()将再次调用,这将重新定位按钮,而不重置LocationValue。在Step2()方法中重置locationvariables时,按钮将工作。如下所示:
step2()
Length1LocX = 40;
Length1LocY = 150;
Length2LocX = 330;
Length2LocY = 150;
[rest of method....]
当然,您不希望每次单击按钮时都调用step2()。当您选择16个按钮中的一个时,将再次调用方法step2(),这将重新定位按钮,而不会重置位置值。当您重置step2()方法中的位置变量时,按钮将工作。如下所示:
step2()
Length1LocX = 40;
Length1LocY = 150;
Length2LocX = 330;
Length2LocY = 150;
[rest of method....]
但你当然不想叫step2()每次点击一个按钮…太棒了,我甚至没有想过要再次重置这些按钮。现在可以了,非常感谢!:p不客气,但请查看答案表@AJNeufeld!他也有一点……太棒了,我甚至没有想过要再次重置这些按钮。现在可以了,非常感谢!:p不客气,但请检查一下t答案形式@AJNeufeld!他也有一点……我忘了每次调用该方法时我都需要删除这些按钮。谢谢!我忘了每次调用该方法时我都需要删除这些按钮。谢谢!避免使用null
布局,像素完美的布局在现代ui设计中是一种错觉。影响组件单个大小的因素太多,您无法控制。Swing的设计宗旨是与布局管理器一起工作,摒弃这些因素将导致无休止的问题,您将花费越来越多的时间尝试使用null
布局来纠正这些问题,像素完美的布局非常重要现代ui设计中的一种错觉。有太多的因素会影响组件的单个大小,而这些因素都是您无法控制的。Swing设计的核心是布局管理器,丢弃这些因素将导致无休止的问题,您将花费越来越多的时间试图纠正这些问题