2011-12-13 52 views
0

(完整源代碼在底部)如何獲取Timer(Util或Swing)讓我可以在Netbeans桌面應用程序中調用方法?

因此,現在我已經有相當一段時間了,我一直在努力。我有一個終極目標,但爲了學習如何做到這一點,我創建了一個應用程序,它簡單地將數字加1,並且每秒顯示爲jLabel1。我的問題是我無法獲得Timer的工作。

我的研究已經向我展示了不同的方法,以及使用包java.util.timerjavax.swing.timerTimer功能的示例。迄今爲止,他們都沒有工作。這從來不是代碼錯誤的問題,但有些方法似乎沒有被調用。在我的大部分試驗中,它通常都涉及方法時間表。在我的最新嘗試,它來到了:

new Timer(delay, taskPerformer).start();

已經給了我的問題。原來的錯誤是

illegal start of type 

invalid method declaration; return type required 

cannot find symbol 
    symbol: class delay 
    location: class timer.TimerView.two 

<identifier> expected 

cannot find symbol 
    symbol: class taskPerformer 
    location: class timer.TimerView.two 

<identifier> expected 

';' expected 

以及延遲顯然是定義爲一個整數,但是即使我更換1000延遲它告訴我

illegal start of type 

invalid method declaration; return type required 

illegal start of type 

所以我現在處於虧損狀態。我看了看,看過很多成功案例,但是他們幾乎從不使用netbeans默認bs。我無法導入java.util.timer,因爲據我所知,javax.swing.timer本身或某些廢話導入它。

我很少要求幫助,但在幾個小時的查找過程中,我有36小時前用一堆橡皮擦燒製的完全相同的源代碼。所以我真的希望有人能夠至少指向正確的方向。我已經看過Oricals教程,等等。那總是讓我在同一個地方。我嘗試過使用定時器功能來操縱其他程序,我甚至試圖在NetBeans中對空白桌面應用程序附帶的狀態欄進行反向工程。沒有任何東西似乎想要工作,或者更可能的是,我的無意中讓我看錯了地方。我看待它的方式不可能是罕見的問題,但我無法實現它的工作。下面發佈的是我的程序的全部內容,由於其簡單的本質,它不是一個空白的netbeans應用程序。 (所有它是一個JLabel和我一直在工作類)

/* 
* TimerView.java 
*/ 

package timer; 

import org.jdesktop.application.Action; 
import org.jdesktop.application.ResourceMap; 
import org.jdesktop.application.SingleFrameApplication; 
import org.jdesktop.application.FrameView; 
import org.jdesktop.application.TaskMonitor; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.Timer; 
import javax.swing.Icon; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 

/** 
* The application's main frame. 
*/ 
public class TimerView extends FrameView { 

    public TimerView(SingleFrameApplication app) { 
     super(app); 

     initComponents(); 

     // status bar initialization - message timeout, idle icon and busy animation, etc 
     ResourceMap resourceMap = getResourceMap(); 
     int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout"); 
     messageTimer = new Timer(messageTimeout, new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       statusMessageLabel.setText(""); 
      } 
     }); 
     messageTimer.setRepeats(false); 
     int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate"); 
     for (int i = 0; i < busyIcons.length; i++) { 
      busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]"); 
     } 
     busyIconTimer = new Timer(busyAnimationRate, new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       busyIconIndex = (busyIconIndex + 1) % busyIcons.length; 
       statusAnimationLabel.setIcon(busyIcons[busyIconIndex]); 
      } 
     }); 
     idleIcon = resourceMap.getIcon("StatusBar.idleIcon"); 
     statusAnimationLabel.setIcon(idleIcon); 
     progressBar.setVisible(false); 

     // connecting action tasks to status bar via TaskMonitor 
     TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext()); 
     taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() { 
      public void propertyChange(java.beans.PropertyChangeEvent evt) { 
       String propertyName = evt.getPropertyName(); 
       if ("started".equals(propertyName)) { 
        if (!busyIconTimer.isRunning()) { 
         statusAnimationLabel.setIcon(busyIcons[0]); 
         busyIconIndex = 0; 
         busyIconTimer.start(); 
        } 
        progressBar.setVisible(true); 
        progressBar.setIndeterminate(true); 
       } else if ("done".equals(propertyName)) { 
        busyIconTimer.stop(); 
        statusAnimationLabel.setIcon(idleIcon); 
        progressBar.setVisible(false); 
        progressBar.setValue(0); 
       } else if ("message".equals(propertyName)) { 
        String text = (String)(evt.getNewValue()); 
        statusMessageLabel.setText((text == null) ? "" : text); 
        messageTimer.restart(); 
       } else if ("progress".equals(propertyName)) { 
        int value = (Integer)(evt.getNewValue()); 
        progressBar.setVisible(true); 
        progressBar.setIndeterminate(false); 
        progressBar.setValue(value); 
       } 
      } 
     }); 
    } 

    @Action 
    public void showAboutBox() { 
     if (aboutBox == null) { 
      JFrame mainFrame = TimerApp.getApplication().getMainFrame(); 
      aboutBox = new TimerAboutBox(mainFrame); 
      aboutBox.setLocationRelativeTo(mainFrame); 
     } 
     TimerApp.getApplication().show(aboutBox); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     mainPanel = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     menuBar = new javax.swing.JMenuBar(); 
     javax.swing.JMenu fileMenu = new javax.swing.JMenu(); 
     javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem(); 
     javax.swing.JMenu helpMenu = new javax.swing.JMenu(); 
     javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem(); 
     statusPanel = new javax.swing.JPanel(); 
     javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator(); 
     statusMessageLabel = new javax.swing.JLabel(); 
     statusAnimationLabel = new javax.swing.JLabel(); 
     progressBar = new javax.swing.JProgressBar(); 

     mainPanel.setName("mainPanel"); // NOI18N 

     org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(timer.TimerApp.class).getContext().getResourceMap(TimerView.class); 
     jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N 
     jLabel1.setName("jLabel1"); // NOI18N 

     javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); 
     mainPanel.setLayout(mainPanelLayout); 
     mainPanelLayout.setHorizontalGroup(
      mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(mainPanelLayout.createSequentialGroup() 
       .addGap(178, 178, 178) 
       .addComponent(jLabel1) 
       .addContainerGap(188, Short.MAX_VALUE)) 
     ); 
     mainPanelLayout.setVerticalGroup(
      mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(mainPanelLayout.createSequentialGroup() 
       .addGap(107, 107, 107) 
       .addComponent(jLabel1) 
       .addContainerGap(133, Short.MAX_VALUE)) 
     ); 

     menuBar.setName("menuBar"); // NOI18N 

     fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N 
     fileMenu.setName("fileMenu"); // NOI18N 

     javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(timer.TimerApp.class).getContext().getActionMap(TimerView.class, this); 
     exitMenuItem.setAction(actionMap.get("quit")); // NOI18N 
     exitMenuItem.setName("exitMenuItem"); // NOI18N 
     fileMenu.add(exitMenuItem); 

     menuBar.add(fileMenu); 

     helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N 
     helpMenu.setName("helpMenu"); // NOI18N 

     aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N 
     aboutMenuItem.setName("aboutMenuItem"); // NOI18N 
     helpMenu.add(aboutMenuItem); 

     menuBar.add(helpMenu); 

     statusPanel.setName("statusPanel"); // NOI18N 

     statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N 

     statusMessageLabel.setName("statusMessageLabel"); // NOI18N 

     statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); 
     statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N 

     progressBar.setName("progressBar"); // NOI18N 

     javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel); 
     statusPanel.setLayout(statusPanelLayout); 
     statusPanelLayout.setHorizontalGroup(
      statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) 
      .addGroup(statusPanelLayout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(statusMessageLabel) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE) 
       .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(statusAnimationLabel) 
       .addContainerGap()) 
     ); 
     statusPanelLayout.setVerticalGroup(
      statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(statusPanelLayout.createSequentialGroup() 
       .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(statusMessageLabel) 
        .addComponent(statusAnimationLabel) 
        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(3, 3, 3)) 
     ); 

     setComponent(mainPanel); 
     setMenuBar(menuBar); 
     setStatusBar(statusPanel); 
    }// </editor-fold> 




    public class two { 



     int delay = 1000; 
    ActionListener taskPerformer = new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 

      int Number = 0; 
      String Output; 
      Number++; 
      Output = Integer.toString(Number); 
      jLabel1.setText(Output); 
     } 
    }; 
    new Timer(1000, taskPerformer).start(); 


    } 







    // Variables declaration - do not modify 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JPanel mainPanel; 
    private javax.swing.JMenuBar menuBar; 
    private javax.swing.JProgressBar progressBar; 
    private javax.swing.JLabel statusAnimationLabel; 
    private javax.swing.JLabel statusMessageLabel; 
    private javax.swing.JPanel statusPanel; 
    // End of variables declaration 

    private final Timer messageTimer; 
    private final Timer busyIconTimer; 
    private final Icon idleIcon; 
    private final Icon[] busyIcons = new Icon[15]; 
    private int busyIconIndex = 0; 

    private JDialog aboutBox; 
} 
+1

請學習Java命名約定並嚴格遵守 – kleopatra

+0

爲了更好地幫助越早,張貼[ SSCCE](http://sscce.org/)。 –

回答

0

啓動時間在課堂上像這樣

public class two { 
    new Timer(1000, taskPerformer).start(); 
} 

,因爲你不能寫出來可執行語句的方法,這應該從任何方法而不是從類中調用,就像這樣你只需聲明或定義對象/變量。這個語句來創建新的對象,但它會調用這是無效的聲明試圖從像構造函數或任何其他方法

2

1)任何方法調用我sugget使用javax.swing.Timer,而不是java.util.Timer的start()方法,因爲輸出javax.swing.Timer總是在EDT

2)java.util.Timer輸出到GUI必須被包裹成invokeLater,否則就沒有guaranted任何東西應該是diplayed /改/你要讀Concurency in Swing更多信息粉刷

3)

4)我建議建立由手你的代碼,而使用非suppoted Java Desktop Application,並通過使用GroupLayout奠定,

相關問題