有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

调用非静态类java

我试图注意到,当我的java应用程序中发生特别的事情时,我试图让窗口闪烁(在mac和windows上)。然而,我是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()


共 (1) 个答案

  1. # 1 楼答案

    停止不必要地使用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,^ ^ {CD4}},因为前者假设将GUI相关的任务自动放在^ {< CD5> },尽管后者的责任纯粹是与程序员有关。p>

    下面是一个使用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 );
        }
    }