2016-04-12 7 views
-2
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 148, 120); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JButton btnStart = new JButton("Start"); 
    btnStart.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) 
     { 
      while(chckbxNewCheckBox.isSelected()){ 
      try { 

       Robot auto = new Robot(); 


       auto.delay(2300); 
       auto.mouseMove(377, 182); 
       auto.mousePress(InputEvent.BUTTON3_DOWN_MASK); 
       auto.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); 
       // 
       auto.delay(1000); 
       auto.mouseMove(466, 293); 
       auto.mousePress(InputEvent.BUTTON1_DOWN_MASK); 
       auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 
       // 
       auto.delay(1000); 
       auto.mouseMove(1061, 217); 
       auto.mousePress(InputEvent.BUTTON1_DOWN_MASK); 
       auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 
       // 
       auto.delay(8000); 
       auto.mouseMove(601, 493); 
       auto.mousePress(InputEvent.BUTTON1_DOWN_MASK); 
       auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 
       // 
       auto.delay(60000); 
       auto.mouseMove(387, 355); 
       auto.mousePress(InputEvent.BUTTON1_DOWN_MASK); 
       auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 
       // 
       auto.delay(8000); 
       auto.mouseMove(705, 652); 
       auto.mousePress(InputEvent.BUTTON1_DOWN_MASK); 
       auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 

      } catch (AWTException e) { 
       e.printStackTrace(); 
      } 
      } 
     }  }); 
    btnStart.setBounds(10, 47, 89, 23); 
    frame.getContentPane().add(btnStart); 

    JCheckBox chckbxNewCheckBox = new JCheckBox("New check box"); 
    chckbxNewCheckBox.setBounds(2, 7, 97, 23); 
    frame.getContentPane().add(chckbxNewCheckBox); 
} 

私のロボットのコマンドをループに入れてアドバイスしたいと思います。ループは、チェックボックスが選択されているときにのみ実行されます。私はそれを行ういくつかの異なる方法を試みたが、誰も動作するようだ。私は何かシンプルなものを欠いていると確信していますが、それが何であるかを見つけることができません。私のためにやっていると助けになるだろうが、それも素晴らしいと説明している。私はeclipseを使用しており、ウィンドウビルダはアイテムを追加しています。チェックボックスをオンにした場合のJavaループのみ

+0

また、コマンドを停止する方法も役立ちます。 –

答えて

0

あなたはこのような何か行うことができます:私は今、質問を理解:

while(CheckBox.isSelected()) { 
    // do the stuff 
} 

更新。別のスレッドで長期間のタスクを実行すると良いです。ここではSwingWorkerを使用してタスクを実行できます。あなたが別のスレッドで、あなたのコマンドを実行することができます

:あなたがスレッドを使用しない場合は、UI

+0

私はこれを試しましたが、何らかの理由でIDEが私が作成したチェックボックスを見つけられず、エラーが表示され続けました。 –

+0

IDEに属していないので、IDEはコードを書くだけで使用されます。 –

+0

私はこれをもう一度試して、私が説明しようとするエラーを投稿します。 –

1

スレッドを使用しwhileループによってハングアップとなりました。 UIは独自のスレッドで実行されます。

あなたは新しいスレッドとしてコードを書くことができます。

Class Robot implements Runnable{ 
    public void run(){ 
    while(programRunning)){ 
    if(checkBox.isSelected()) 
     { 
     //Perform commands. 
     } 
    else {// you may choose to sleep this thread here as well in case of not selected} 
    } 
    } 

} 

programRunningは、このチェックボックスが選択されていない場合でも、実行中のループを維持するように選択することができます別の変数で、UIは、プログラム/アプリケーションがまだ実行されています。

関連する問題