2012-03-17 63 views
3

我想創建一個簡單的GUI窗體,它只有2個元素 - 一個簡單的標籤和一個按鈕。顯示在按鈕上的文字是'開始'。標籤默認顯示0。Java - 更新在Swing中製作的GUI

當我點擊開始按鈕,下列行爲發生:

  1. 計數器將啓動由1處從0每1秒遞增。
  2. 「開始」按鈕上顯示的文本應更改爲「停止」。
  3. 當我再次點擊相同的按鈕(現在顯示標題爲停止)時,增量應停止。
  4. 按鈕上的文字應改爲開始。等等...

我正在Netbeans中開發我的應用程序。

如上圖所示,有2個.java文件

AGC.java的內容是:

public class AGC extends javax.swing.JFrame 
{ 
    public AGC() 
    {  
     initComponents(); 
    } 

    public static void main(String args[]) 
    { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() 
      { 
       new AGC().setVisible(true); 
      } 
     }); 
    } 

    private javax.swing.JButton btnStartStop; // name of start stop button 
    private javax.swing.JLabel lblCounter; // name of the label 

} 

Main.java的內容是:

public class Main 
{ 
    public static int count = 0; 
    public static boolean started = false; 
} 

我想實現以下邏輯:

private void btnStartStopMouseClicked(java.awt.event.MouseEvent evt) 
{ 
    if (Main.stared == true) 
    { 
     // logic to start counting 
    } 
    else 
    { 
     // logic to stop counting 
    } 
} 

我的問題是這樣的:

  1. 如何更新lblCounter每1秒?
  2. 我應該實現什麼邏輯來啓動1秒計時器以及如何訪問該方法中的lblCounter?

請幫忙。工作代碼將非常感激。提前致謝。

周杰倫

+0

如果我缺少任何相關信息,請將其還原。我將能夠提供相同的。 – Jay 2012-03-17 16:42:32

+0

我已經添加了工作示例代碼,這是你想要的嗎? – 2012-03-17 17:02:28

回答

6

只需使用一個javax.swing.Timer,使一個ActionListener,做這件事情給你。給我一分鐘的工作代碼示例:-)

下面是進一步的幫助的一個範例程序:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class UpdateWithTimer extends JFrame 
{ 
    private Timer timer; 
    private JButton startStopButton; 
    private JLabel changingLabel; 
    private int counter = 0; 
    private boolean flag = false; 
    private ActionListener timerAction = new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      counter++; 
      changingLabel.setText("" + counter); 
     } 
    }; 

    private ActionListener buttonAction = new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      if (!flag) 
      { 
       startStopButton.setText("STOP TIMER"); 
       timer.start(); 
       flag = true; 
      } 
      else if (flag) 
      { 
       startStopButton.setText("START TIMER"); 
       timer.stop(); 
       flag = false; 
      } 
     } 
    }; 

    private void createAndDisplayGUI() 
    { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationByPlatform(true); 

     JPanel contentPane = new JPanel(); 
     changingLabel = new JLabel("" + counter); 
     contentPane.add(changingLabel); 

     startStopButton = new JButton("START TIMER"); 
     startStopButton.addActionListener(buttonAction); 

     add(contentPane, BorderLayout.CENTER); 
     add(startStopButton, BorderLayout.PAGE_END); 

     timer = new Timer(1000, timerAction); 

     setSize(300, 300); 
     setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new UpdateWithTimer().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

如果你想在櫃檯再次回恢復到0,就停止計時器,簡單地添加

else if (flag) 
{ 
    startStopButton.setText("START TIMER"); 
    timer.stop(); 
    flag = false; 
    counter = 0; 
    changingLabel.setText("" + counter); 
} 

這部分到buttonActionactionPerformed(...)方法。

+0

非常感謝Gagandeep。這讓我的一天...太好了。現在我可以在此基礎上繼續開展模擬設計。再次感謝。 – Jay 2012-03-17 17:26:26

+0

@Jay:你最歡迎並保持微笑:-) – 2012-03-17 17:27:22

2

好的,所以我建議看看SwingWorker。您可以擴展SwingWorker,並在doBackground()中執行a while(!isCancelled())循環,執行Thread.sleep(1000);執行睡眠後,您可以簡單地觸發一個屬性更改,以增加標籤的值。

只要您按下停止按鈕,只需取消當前的揮杆工。當您按下開始按鈕時,只需​​揮杆工