2013-04-30 51 views
-1

我需要在我的Java GUI中運行後臺線程,該後臺線程僅在單擊按鈕時運行,並在再次單擊該按鈕時暫停。我不完全確定如何設置,但我已經在我的構造函數中放置了一個線程,並且在將特定的布爾值設置爲TRUE時,將內置的while循環設置爲經過。一個按鈕從設置布爾值TRUE或FALSE切換。通過單擊按鈕運行並暫停GUI後臺線程

我在這個圖形用戶界面中的其他一切工作正常。當我嘗試調試線程時,它實際上是在我逐步完成線程時運行的,但是當我嘗試完全運行GUI時沒有任何問題。圖形用戶界面相當大,所以我要放置一部分構造函數和按鈕的動作監聽器。其餘的代碼是不必要的,因爲它工作得很好。我需要知道我在做什麼錯在這裏:

public BasketballGUI() { 
    // certain labels and buttons 
    Thread runningSim = new Thread() { 
     public void run() { 
      while(simRun) { 
       // do stuff here 
      } 
     } 
    }; 
    runningSim.start(); 
} 

// other GUI stuff 

// actionListener that should run the thread. 
class SimButtonListener implements ActionListener { 
    public void actionPerformed(ActionEvent arg0) { 
     if(!simRun) { 
      simRun = true; 
      sim.setText("Pause Simulator"); 
     } 
     else if(simRun) { 
      simRun = false; 
      sim.setText("Run Simulator"); 
     } 
     // other stuff in this actionListener 
    } 
} 
+2

@LittleChild不,他們不能[[線程#暫停]](http://docs.oracle.com/javase/7/docs/api/java/ lang/Thread.html#suspend%28%29)和['Thread#resume'](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#resume%28 %29)被棄用,並被認爲是不穩定的... – MadProgrammer 2013-04-30 02:47:07

+1

@原始海報,請顯示一些代碼,請給我們一個想法,你正在嘗試做什麼。 – 2013-04-30 02:48:07

+2

[無代碼示例](http://sscce.org/),無回答 – MadProgrammer 2013-04-30 02:48:08

回答

5
  1. 建立基於Timer一個Swing與ActionListener將被反覆調用。在actionPerformed(ActionEvent)方法調用repaint()
  2. 當用戶點擊 開始
  3. 停止計時器(Timer.stop()),當用戶點擊停止

如果你不能把它從該說明書的工作,我建議

  • 啓動定時器(Timer.start())你張貼了你最好的SSCCE。


    我想我有一個「躺在身邊」 ..試試這個working SSCCE它使用this SSCCE創建的圖像。

  • +0

    另請參閱更新。 – 2013-04-30 03:39:53

    -1

    在處理按鈕事件以影響文本區域或進度條之類的東西時,我可以看到此後臺線程對Java GUI有用。

    爲了論證的緣故,我將爲您構建一個影響文本區域的小GUI。我希望這可以幫助你。

    import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.util.concurrent.atomic.AtomicBoolean; 
    import javax.swing.*; 
    
    public class TestClass extends JPanel { 
    
        super("TestClass - Title"); 
        private AtomicBoolean paused; 
        private JTextArea jta; 
        private JButton btn; 
        private Thread thread; 
    
        public TestClass() { 
    
         paused = new AtomicBoolean(false); 
         jta = new JTextArea(100, 100); 
         btn = new JButton(); 
    
         initialize(); 
        } 
    
        public void initialize() { 
    
         jta.setLineWrap(true); 
         jta.setWrapStyleWord(true); 
         add(new JScrollPane(jta)); 
         btn.setPreferredSize(new Dimension(100, 100)); 
         btn.setText("Pause"); 
         btn.addActionListener(new ButtonListener()); 
         add(btn); 
    
         Runnable runnable = new Runnable() { 
          @Override 
          public void run() { 
           while(true) { 
    
            for(int i = 0; i < Integer.MAX_VALUE; i++) { 
    
             if(paused.get()) { 
              synchronized(thread) { 
               try { 
    
                thread.wait(); 
    
               } catch(InterruptedException e) { 
               } 
              } 
             } 
            } 
    
            jta.append(Integer.toString(i) + ", "); 
    
           try { 
    
            Thread.sleep(500); 
    
           } catch (InterruptedException e) { 
           } 
           } 
          } 
         }; 
         thread = new Thread(runnable); 
         thread.start(); 
        } 
    
        @Override 
        public Dimension getPreferredSize() { 
    
         return new Dimension(100, 30); 
    
        } 
    
          class ButtonListener implements ActionListener { 
    
           @Override 
           public void actionPerformed(ActionEvent event) { 
    
            if(!paused.get()) { 
             btn.setText("Start"); 
             paused.set(true); 
            } else { 
             btn.setText("Pause"); 
             paused.set(false); 
    
             synchronized(thread) { 
              thread.notify(); 
             } 
            } 
           } 
          } 
    } 
    

    主要類叫一切。

    import javax.swing.JFrame; 
    import javax.swing.SwingUtilities; 
    
    public class MainClass { 
    
        public static void main(final String[] arg) { 
    
         SwingUtilities.invokeLater(new Runnable() { 
          public void run() { 
           JFrame frame = new JFrame(); 
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
           frame.add(new TestClass()); 
           frame.pack(); 
           frame.setVisible(true); 
           frame.setLocationRelativeTo(null); 
          } 
         }); 
        } 
    } 
    

    我沒有測試此代碼,看看它的工作原理完全,其主要目標是打破你通過你的編碼塊,用我的組件來解決您的問題。希望這有助於。需要任何東西給我發電子郵件在[email protected]

    +2

    通過告訴原始海報進行Swing調用,特別是'jta.append(Integer.toString(i)+「,」);'擺脫Swing事件調度線程,您的是非常危險的建議。請修改此。 – 2013-04-30 03:23:26

    +0

    你的代碼工作,這就是我希望完成的。看起來我的語法有點不合適,但如果我能做出調整,我應該讓這個工作。 – user2334278 2013-04-30 04:06:15

    +0

    @PatchGuru你知道我是否可以確保TextArea始終位於scrollpane的底部嗎? – user2334278 2013-04-30 05:29:26