无法从静态上下文引用非静态方法和变量

时间:2013-12-06 07:51:30

标签: java swing awt

//我在.setlocation和.getlocation方法中遇到错误请帮忙。

import java.awt.*;    
import java.awt.geom.AffineTransform;    
import javax.swing.*;    
import javax.swing.Timer;    
import static java.awt.GraphicsDevice.WindowTranslucency.*;    
public class random extends JFrame {    
    public random() {    
        super("Buzzer");    
        setLayout(new GridBagLayout());    
        getContentPane().setBackground(Color.BLACK);    
        setSize(512,512);    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        JLabel background=new JLabel(new ImageIcon("image url"));    
         add(background);
   }        

    public static void main(String[] args) {    
        JFrame.setDefaultLookAndFeelDecorated(true);    
            random tw = new random();    
        tw.setOpacity(0.6f);    
            tw.setVisible(true);    
        Buzz buzzer = new Buzz();    
        buzzer.buzz();    
    }    
}

 class Buzz {        
        private int iDisplaceXBy = 5;        
        private int iDisplaceYBy = -10;

        public static void buzz(){        
            Point position1 = new Point( JFrame.getLocationOnScreen().x + 
    iDisplaceXBy , JFrame.getLocationOnScreen().y + iDisplaceYBy );        
            Point position2 = new Point( JFrame.getLocationOnScreen().x - iDisplaceXBy 
    , JFrame.getLocationOnScreen().y - iDisplaceYBy );

                    for (int i = 0; i < 1000 ; i++){
                        JFrame.setLocation(position1);        
                        JFrame.setLocation(position2);        
                    }        
                    JFrame.setLocation(JFrame.getLocationOnScreen());        
        }        
    }

1 个答案:

答案 0 :(得分:4)

setLocation的{​​p> JFrame不是静态的。您不能使用JFrame.setLocation(..)。您应该在setLocation(position1)实例上使用JFrame

  JFrame fr=new JFrame();
  fr.setLocation(position1);