在调试模式下,当ScheduledExecutorService正在运行时,我无法与GUI进行交互。

huangapple 未分类评论55阅读模式
英文:

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(&quot;0&quot;);
private JLabel statusLabel = new JLabel(&quot;Task not completed&quot;);
private JButton monsterButton = new JButton(&quot;Move Monster&quot;);

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(&quot;Monster position is: &quot; + 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(() -&gt; moveMonster(), 1000L, 1000L, TimeUnit.MILLISECONDS);
	
	setSize(200, 400);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setVisible(true);		
	
}

public void setMonsterPosition(int monsterPosition) {
	if(monsterPosition &gt; 1 &amp;&amp; monsterPosition &lt; playerPosition) {
		monsterLabel.setText(&quot;Monster position is: &quot; + monsterPosition);
	}
	else {
		monsterPosition = 0;
		monsterLabel.setText(&quot;Monster has eaten the Player&quot;);
	}		
}


public void moveMonster() {
	SwingWorker&lt;Integer, Void&gt; worker = new SwingWorker&lt;&gt;() {

		@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();
}

}

huangapple
  • 本文由 发表于 2020年5月2日 13:59:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/61555132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定