所以我正在设计一个GUI(GUI本身在称为mainGUI的单独类中)。 GUI有2个按钮,每个按钮将main.subProgramNumber设置为1或2(取决于按钮),将main.safeToContinue设置为false,然后使用dispose()处置GUI。但是当我单击GUI中的按钮时,GUI关闭,然后什么也没有发生。我已经使用了调试器,奇怪的是,使用调试工具时,代码的行为有所不同。使用调试工具时,单击该按钮后GUI仍保持打开状态并永久加载。任何帮助都将受到欢迎。
genData main = new genData();
main.safeToContinue = false;
main.running = true;
while(main.running == true){
//trigger GUI
mainGUI MG = new mainGUI();
MG.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
MG.setSize(250, 275);
MG.setVisible(true);
//wait tell the GUI says its safe to continue
while(main.safeToContinue == false){
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(mainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
//check if main.subProgram is 1
if(main.subProgramNumber == 1){
System.out.println("subProgram = 1");
}
//check if main.subProgram is 2
if(main.subProgramNumber == 2){
System.out.println("subProgram = 2");
}
}
下面的是我的GUI代码的简化版本。 (抱歉,它的代码很多,但我想不出如何使它比这更简单...)
public class mainGUI extends JFrame{
public JButton add;
public JButton remove;
public JButton edit;
public JButton report;
public JButton close;
public mainGUI(){
super("Guest Manager");
setLayout(new FlowLayout());
add = new JButton("Add a Guest");
remove = new JButton("Remove a Guest");
close = new JButton("Close");
add(add);
add(remove);
add(close);
addGuest ag = new addGuest();
add.addActionListener(ag);
removeGuest rg = new removeGuest();
remove.addActionListener(rg);
Close c = new Close();
close.addActionListener(c);
}
private class addGuest implements ActionListener{
public void actionPerformed(ActionEvent e){
genData main = new genData();
main.subProgramNumber = 1;
main.safeToContinue = true;
dispose();
}
}
private class removeGuest implements ActionListener{
public void actionPerformed(ActionEvent e){
genData main = new genData();
main.subProgramNumber = 2;
main.safeToContinue = true;
dispose();
}
}
private class Close implements ActionListener{
public void actionPerformed(ActionEvent e){
genData main = new genData();
main.safeToContinue = true;
dispose();
}
}
}
下面是我所使用(并且正在上面使用)的一种请求的,唤醒的示例
主程序(当我单击“开始”时称为该程序):
varData MC = new varData();
do{
MC.safeToContinue = false;
//open and run the main GUI
ExerciseMainGUI EM = new ExerciseMainGUI();
EM.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
EM.setSize(250, 275);
EM.setVisible(true);
//System.out.println("debug 1");
//wait tell the sub program closes to continue
do{
hutLib.pause(1);
}while(MC.safeToContinue==false);
hutLib.pause(1);
}while(MC.running == true);
我在上面调用的GUI:
public class ExerciseMainGUI extends JFrame{
private JButton bottlePacker;
private JButton triangleTester;
private JButton primeChecker;
private JButton averager;
private JButton rollTheDice;
private JButton close;
public ExerciseMainGUI(){
super("Java Exercises");
setLayout(new FlowLayout());
bottlePacker = new JButton(" Bottle Packer ");
triangleTester = new JButton(" Triangle Tester ");
primeChecker = new JButton(" Prime Checker ");
averager = new JButton ("Averager (offline)");
rollTheDice = new JButton("Roll the Dice (offline)");
close = new JButton(" Close ");
add(bottlePacker);
add(triangleTester);
add(primeChecker);
add(averager);
add(rollTheDice);
add(close);
bottlePacker bp = new bottlePacker();
bottlePacker.addActionListener(bp);
triangleTester tt = new triangleTester();
triangleTester.addActionListener(tt);
primeChecker pc = new primeChecker();
primeChecker.addActionListener(pc);
averager a = new averager();
averager.addActionListener(a);
rollTheDice rtd = new rollTheDice();
rollTheDice.addActionListener(rtd);
close c = new close();
close.addActionListener(c);
}
private class bottlePacker implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
//runs a subprogram
}
}
private class triangleTester implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
//runs a subprogram
}
}
private class primeChecker implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
//runs a subprogram
}
}
private class averager implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
//runs a subprogram
}
}
private class rollTheDice implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
//runs a subprogram
//JOptionPane.showMessageDialog(null, "NOTE: Roll the Dice will run in the command line.");
//oldCodeStorage.rollTheDice.rollTheDice();
}
}
private class close implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
varData MC = new varData();
MC.running = false;
MC.safeToContinue = true;
}
}
}
注意:每个子程序完成后,运行以下代码:
varData MC = new varData();
MC.safeToContinue = true;
我知道很多东西,但是我被明确要求张贴。
答案 0 :(得分:2)
您在这里滥用了Swing组件。您应该显示您的应用并通过事件监听器对事件做出反应。这不需要循环等。
例如
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b1 = new JButton("Action 1");
JButton b2 = new JButton("Action 2");
b1.addActionListener((e)->System.out.println("Action 1 taken")); // your subcommand 1
b2.addActionListener((e)->System.out.println("Action 2 taken")); //your subcomand 2
frame.add(b1);
frame.add(b2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
还请记住不要在actionListeners
中调用昂贵的操作,而要将它们委派给后台作业-否则它将冻结UI。用Swing编写UI应用程序并不像您想象的那样简单,并且需要少量的基础知识(关于Swing本身)。尝试也不错,但是如果不熟悉Swing的基础知识,您将遭受不胜枚举的麻烦-相信我,我去过那里。
您已经提到过,Thing是陌生的-Swing本身是单线程的-但它涉及线程来执行各种任务,这些任务会使应用程序变慢或使其无响应。
我强烈建议您开始研究Oracle Swing教程。
答案 1 :(得分:0)
您根本没有修改main.safeToContinue
。这就是为什么它陷入无限的while循环
答案 2 :(得分:0)
我们说我们有一堂课。
class GenData{
static int x = 0;
int y = 1;
}
现在我有该对象的两个实例。
GenData a = new GenData();
GenData b = new GenData();
a.x = 5;
a.y = 10;
System.out.println( b.x + ", " + b.y );
您期望输出是什么?
b有它自己的实例变量。所以我希望输出是。
5,1
在您的代码中,调用new genData()
来创建该类的新实例。如果您的字段不是静态的,则不会与您正在检查的genData版本共享。
也请使用Java命名约定。 GenData
更适合用作类名。
如何修复。
public class mainGUI extends JFrame{
public JButton add;
public JButton remove;
public JButton edit;
public JButton report;
public JButton close;
genData main;
public mainGUI(genData main){
this.main = main;
//don't change the rest.
}
private class addGuest implements ActionListener{
public void actionPerformed(ActionEvent e){
//genData main = new genData();
//don't call new here, use the reference you
//received.
}
}
}