英文:
In debuggin mode, I cannot interact with the GUI when ScheduledExecutorService is running
问题
我有一个GUI应用程序,它运行得很好,但是我想在调试模式下查看所有内容的运行情况。GUI中的怪物每1秒钟在ScheduledExecutorService中移动一次。在调试模式下运行服务时,我想看到用户按按钮对应用程序产生的影响。但是,在调试模式下,按钮无法正常工作,而在正常运行应用程序时它是有效的。请指导我如何在调试模式下查看用户交互。
public class MainFrame extends JFrame{
private JLabel monsterLabel = new JLabel("0");
private JLabel statusLabel = new JLabel("任务未完成");
private JButton monsterButton = new JButton("移动怪物");
int monsterPosition = 1;
int playerPosition = 10;
public MainFrame(String title) {
super(title);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.NONE;
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 1;
gc.weighty = 1;
add(monsterLabel, gc);
monsterLabel.setText("怪物位置为:" + monsterPosition);
gc.gridx = 0;
gc.gridy = 1;
gc.weightx = 1;
gc.weighty = 1;
add(statusLabel, gc);
gc.gridx = 0;
gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
add(monsterButton, gc);
// 移动怪物的按钮
monsterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
monsterPosition = monsterPosition + 1;
setMonsterPosition(monsterPosition);
}
});
// ScheduledExecutorService每1秒钟运行一次。
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(() -> moveMonster(), 1000L, 1000L, TimeUnit.MILLISECONDS);
setSize(200, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void setMonsterPosition(int monsterPosition) {
if(monsterPosition > 1 && monsterPosition < playerPosition) {
monsterLabel.setText("怪物位置为:" + monsterPosition);
}
else {
monsterPosition = 0;
monsterLabel.setText("怪物吃掉了玩家");
}
}
public void moveMonster() {
SwingWorker<Integer, Void> worker = new SwingWorker<>() {
@Override
protected Integer doInBackground() throws Exception {
monsterPosition++;
return monsterPosition;
}
@Override
protected void done() {
try {
Integer nextMonsterPosition = get();
setMonsterPosition(nextMonsterPosition);
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
worker.execute();
}
}
英文:
I have a GUI application which is running fine, however I want to see how everything runs in debugging mode. The GUI has Monster moving in ScheduledExecutorService every 1 second. While the service is running in Debugging Mode, I want to see how User pressing button effects the Application. But, in debugging mode the button does not work, whereas it works when I am running the application normally. Please advise on how I can see user interaction in debuggin mode.
public class MainFrame extends JFrame{
private JLabel monsterLabel = new JLabel("0");
private JLabel statusLabel = new JLabel("Task not completed");
private JButton monsterButton = new JButton("Move Monster");
int monsterPosition = 1;
int playerPosition = 10;
public MainFrame(String title) {
super(title);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.NONE;
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 1;
gc.weighty = 1;
add(monsterLabel, gc);
monsterLabel.setText("Monster position is: " + monsterPosition);
gc.gridx = 0;
gc.gridy = 1;
gc.weightx = 1;
gc.weighty = 1;
add(statusLabel, gc);
gc.gridx = 0;
gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
add(monsterButton, gc);
// Button to Move the monster
monsterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
monsterPosition = monsterPosition + 1;
setMonsterPosition(monsterPosition);
}
});
// The ScheduledExecutorService runs every 1 second.
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(() -> moveMonster(), 1000L, 1000L, TimeUnit.MILLISECONDS);
setSize(200, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void setMonsterPosition(int monsterPosition) {
if(monsterPosition > 1 && monsterPosition < playerPosition) {
monsterLabel.setText("Monster position is: " + monsterPosition);
}
else {
monsterPosition = 0;
monsterLabel.setText("Monster has eaten the Player");
}
}
public void moveMonster() {
SwingWorker<Integer, Void> worker = new SwingWorker<>() {
@Override
protected Integer doInBackground() throws Exception {
monsterPosition++;
return monsterPosition;
}
@Override
protected void done() {
try {
Integer nextMonsterPosition = get();
setMonsterPosition(nextMonsterPosition);
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
worker.execute();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论