В режиме отладки я не могу взаимодействовать с GUI во время работы ScheduledExecutorService - PullRequest
1 голос
/ 02 мая 2020

У меня есть GUI приложение, которое работает нормально, однако я хочу посмотреть, как все работает в режиме отладки. У GUI Monster перемещается в ScheduledExecutorService каждую 1 секунду. Пока служба работает в режиме отладки, я хочу посмотреть, как нажатие кнопки «Пользователь» влияет на приложение. Но в режиме отладки кнопка не работает, тогда как она работает, когда я нормально запускаю приложение. Посоветуйте, пожалуйста, как я могу видеть взаимодействие с пользователем в режиме отладки.

publi c Класс MainFrame расширяет 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();
}

}

...