我从来没有做过多线程(我认为这是我需要的,但我不确定),并且真的不明白,我认为这个问题会在这个问题结束时显而易见......
我已经使用JButtons等用Java创建了GUI,当用户点击特定按钮时,我想将GUI的控制权传递给另一个类,以便第二类可以更改/更新GUI。然后,在用户单击另一个按钮(由第二个类放在GUI上)之后,它将GUI的控制权返回给第一个类。
这是我尝试过的,但行为不正确:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
public class Addition implements ActionListener
{
public JFrame window;
public JPanel windowContents;
public JTextArea windowDialog;
public JButton[] buttons;
public int i = 1;
public Subtraction subtract;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Addition();
}
});
}
Addition ()
{
window = new JFrame();
windowContents = new JPanel();
windowDialog = new JTextArea();
buttons = new JButton[2];
buttons[0] = new JButton();
buttons[1] = new JButton();
subtract = new Subtraction();
window.setSize(300, 300);
windowDialog.setText("Click on a button below. The current value is " + i);
buttons[0].setText("Click me to add 1");
buttons[1].setText("Click me to change control");
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
windowContents.add(windowDialog);
windowContents.add(buttons[0]);
windowContents.add(buttons[1]);
window.add(windowContents);
window.setVisible(true);
}
public void updateGUI()
{
windowDialog.setText("Click on a button below. The current value is " + i);
windowDialog.revalidate();
windowDialog.repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttons[0])
{
i = i + 1;
updateGUI();
}
else if(e.getSource() == buttons[1])
{
subtract.getGUI(window, windowContents, buttons, windowDialog, i);
subtract.takeControl();
while(subtract.done == false)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
}
System.out.println("Final int = " + i);
}
}
}
第二节课:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Subtraction implements ActionListener
{
public JFrame window;
public JPanel windowContents;
public JTextArea windowDialog;
public JButton[] buttons;
public int i;
public boolean done;
public void getGUI(JFrame window, JPanel windowContents, JButton[] buttons, JTextArea windowDialog, int i)
{
this.window = window;
this.windowContents = windowContents;
this.windowDialog = windowDialog;
this.buttons = buttons;
this.i = i;
}
public void takeControl()
{
done = false;
buttons[0].setText("Click me to subtract 1");
buttons[1].setText("Click me to change control back to addition");
updateGUI();
}
public void updateGUI()
{
windowDialog.setText("Click on a button below. The current value is " + i);
windowDialog.revalidate();
windowDialog.repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttons[0])
{
i = i - 1;
updateGUI();
}
else if(e.getSource() == buttons[2])
{
done = true;
}
}
}
当我运行此GUI时,GUI冻结在
行if(e.getSource == buttons[1]) //From the Addition Class
所以当我从OtherClass调用updateGUI()时,我永远不会得到更新的GUI。
注意:我从我的第一个类复制了UpdateGUI()方法,它在那里工作得很好,所以我知道该方法有效。这只是屏幕实际上没有改变。
注意2:我知道只需要一个名为“Math”的类或者类似的东西,这个代码可以做得更好,但这不是我的实际项目。我的实际项目超过1000行,GUI确实需要由2个类处理。为了不给读者带来1000多行代码,我制作了一个行为相同的简单程序,只是为了说明发生了什么以及问题是什么。
提前非常感谢!!!