我想從Java中的自定義JFrame接收選定的選項。當在NetBeans中以調試模式運行時(當循環內部存在斷點時),代碼可以正常工作,但不能實時運行。雖然迴路沒有反應的Java當收到選項
boolean keepgoing = true;
while (keepgoing) {
if (ioFrame.getOption() == 0) {
ioFrame.setVisible(false);
keepgoing = false;
//more code
}
else if (ioFrame.getOption() == 1) {
ioFrame.setVisible(false);
keepgoing = false;
//more code
}
}
ioFrame.getOption()
返回-1,直到按鈕被點擊了的JFrame,然後根據按鈕點擊它是0或1
ioFrame動作偵聽器:
JButton loadButton = new JButton("Load Inventory");
class ChoiceListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
initialOption = 0;
}
}
loadButton.addActionListener(new ChoiceListener());
JButton updateButton = new JButton("Update Inventory");
class ChoiceListener2 implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
initialOption = 1;
}
}
updateButton.addActionListener(new ChoiceListener2());
您應該使用事件處理而不是while循環。詳情請參閱:http://docs.oracle.com/javase/tutorial/uiswing/events/ – svz
嘗試發佈[SSCCE](http://www.sscce.org)以獲得更好的幫助。有了這些代碼,我不能說你什麼都不清楚。 – nachokk
像svz建議的一樣,更好地使用事件 - 使用busy-wait是一個糟糕的做法:http://en.wikipedia.org/wiki/Busy_waiting – alfasin