2015-05-04 136 views
0

我想特別注意到在我的Java應用程序中發生什麼事情時,我試圖使窗口閃爍(在Mac和Windows上)。但是,我是新來的Java和鬱悶的靜態/非靜態方法爵士樂。調用非靜態類java

但是我的挫折一邊,我怎麼稱呼我的toggleVisible類?

我已經刪除了不必要的代碼:

public static void main(String args[]) { 

Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 
      checkAlerts(); 
     } 
     }, 30000, 30000); 
    } 

public static Boolean checkAlerts(){ 

      if(count == 0){ 
       JOptionPane.showMessageDialog(null, "No results"); 
      } else { 
       toggleVisible(); 
       JOptionPane.showMessageDialog(null, "Some results back"); 
      } 
    } 

public void toggleVisible() { 
     setVisible(!isVisible()); 
     if (isVisible()) { 
     toFront(); 
     requestFocus(); 
     setAlwaysOnTop(true); 
     try { 
      //remember the last location of mouse 
      final Point oldMouseLocation = MouseInfo.getPointerInfo().getLocation(); 

      //simulate a mouse click on title bar of window 
      Robot robot = new Robot(); 
      robot.mouseMove(mainFrame.getX() + 100, mainFrame.getY() + 5); 
      robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 
      robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 

      //move mouse to old location 
      robot.mouseMove((int) oldMouseLocation.getX(), (int) oldMouseLocation.getY()); 
     } catch (Exception ex) { 
      //just ignore exception, or you can handle it as you want 
     } finally { 
      setAlwaysOnTop(false); 
     } 
     } 
    } 

toggleVisible給我的錯誤:非靜態方法toggleVisible()不能從靜態上下文引用。

+0

請提供完整的類。 –

+1

對不起,能否詳細說明一下?你需要所有的課程? – TMB87

+0

當我將其聲明爲靜態時,會出現很多錯誤 – TMB87

回答

2

停止使用不必要的使用static關鍵字。相反,只需在main方法內創建類的對象並調用一個方法,您將利用該方法來完成應用程序流向的任務。

在簡單的話,可以考慮下面的例子:

public class Example { 

    private void performTask() { 
     /* 
     * Now start wriitng code from here. 
     * Stop using main, for performing 
     * the tasks, which belongs to the 
     * application. 
     */ 
    } 

    public static void main (String[] args) { 
     new Example().performTask(); 
    } 
} 

而且,看來,你處理Swing。出於這個原因,考慮javax.swing.Timer,超過java.util.Timer,因爲前者假設自動將GUI相關的任務放在Event Disptacher Thread - EDT上,儘管後者責任完全在於程序員。

這裏是一個小例子,使用SwingTimer類:

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

public class LabelColourExample { 

    private Timer timer; 
    private JLabel label; 
    private Color [] colours = { 
     Color.red, 
     Color.blue, 
     Color.green, 
     Color.cyan, 
     Color.yellow, 
     Color.magenta, 
     Color.black, 
     Color.white 
    }; 
    private int counter; 

    private static final int GAP = 5; 

    private ActionListener timerActions = new ActionListener() { 
     @Override 
     public void actionPerformed (ActionEvent ae) { 
      label.setForeground (colours [ counter++ ]); 
      counter %= colours.length; 
     } 
    }; 

    public LabelColourExample() { 
     counter = 0; 
    } 

    private void displayGUI() {   
     JFrame frame = new JFrame ("Label Colour Example"); 
     frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout (new BorderLayout (GAP, GAP)); 

     label = new JLabel ("MyName", JLabel.CENTER); 
     label.setOpaque (true); 
     contentPane.add (label, BorderLayout.CENTER); 

     JButton button = new JButton ("Stop"); 
     button.addActionListener (new ActionListener() { 
      @Override 
      public void actionPerformed (ActionEvent ae) { 
       JButton button = (JButton) ae.getSource(); 
       if (timer.isRunning()) { 
        timer.stop(); 
        button.setText ("Start"); 
       } else { 
        timer.start(); 
        button.setText ("Stop"); 
       } 
      } 
     }); 
     contentPane.add (button, BorderLayout.PAGE_END); 

     frame.setContentPane (contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform (true); 
     frame.setVisible (true); 

     timer = new Timer (1000, timerActions); 
     timer.start(); 
    } 

    public static void main (String[] args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       new LabelColourExample().displayGUI(); 
      } 
     }; 
     EventQueue.invokeLater (runnable); 
    } 
} 
+0

這是完美的,絕對有意義。非常感謝 – TMB87