Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从静态上下文引用非静态方法和变量_Java_Swing_Awt - Fatal编程技术网

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

Java 无法从静态上下文引用非静态方法和变量,java,swing,awt,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 exte

//我在.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{
私有int iDisplaceXBy=5;
private int iDisplaceYBy=-10;
公共静态无效buzz(){
点位置1=新点(JFrame.getLocationOnScreen().x+
iDisplaceXBy,JFrame.getLocationOnScreen().y+IDisplaceby);
点位置2=新点(JFrame.getLocationOnScreen().x-iDisplaceXBy
,JFrame.getLocationOnScreen().y-IDisplaceby);
对于(int i=0;i<1000;i++){
JFrame.设置位置(位置1);
JFrame.设置位置(位置2);
}        
setLocation(JFrame.getLocationOnScreen());
}        
}

JFrame
的设置位置不是静态的。不能使用as
JFrame.setLocation(..)
。您应该在
JFrame
实例上使用
setLocation(position1)

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

不要只是把整个程序粘贴到这里。写一个小的,可运行的例子来说明你的问题,并包括你在问题中得到的例外!这是一个非常常见的错误。@Trojan.ZBOT我在询问之前尝试过,但仍然无法解决问题。
  JFrame fr=new JFrame();
  fr.setLocation(position1);