Java 无法从静态上下文引用非静态方法和变量
//我在.setlocation和.getlocation方法中遇到错误,请帮助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
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
的设置位置不是静态的。不能使用asJFrame.setLocation(..)
。您应该在JFrame
实例上使用setLocation(position1)
JFrame fr=new JFrame();
fr.setLocation(position1);
不要只是把整个程序粘贴到这里。写一个小的,可运行的例子来说明你的问题,并包括你在问题中得到的例外!这是一个非常常见的错误。@Trojan.ZBOT我在询问之前尝试过,但仍然无法解决问题。
JFrame fr=new JFrame();
fr.setLocation(position1);