无法从静态上下文错误中引用Button变量

时间:2013-01-11 04:53:31

标签: java static compiler-errors

如何使用当前代码防止此错误发生?我为我的逻辑非常业余而道歉。

 public class jButExmp {    
 JFrame exmpFrame;
 JButton Button1, Button2, Button3;    
 public jButExmp ()  {
     exmpFrame.setLayout(new FlowLayout());
     exmpFrame.setSize(250,150);
     exmpFrame.setVisible(true);
     exmpFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
     exmpFrame.add(Button1);
     exmpFrame.add(Button2);
     exmpFrame.add(Button3);
 }
 public static void main(String[] args)  {
 exmpFrame = new JFrame ("Example Frame");
 Button1 = new JButton ("1");
 Button2 = new JButton ("2");
 Button3 = new JButton ("3");

 Button1.setSize(80, 30);  //set size of button
 Button1.setLocation(0,0);
 Button1.setEnabled(true);

 Button2.setSize(80,30);
 Button2.setLocation(90, 0);
 Button2.setEnabled(false);

 }
}

3 个答案:

答案 0 :(得分:1)

ExmpFile screen shot

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

public class ExmpFile {

    JFrame exmpFrame;
    JButton Button1, Button2, Button3;

    public ExmpFile()  {
        exmpFrame = new JFrame ("Example Frame");
        Button1 = new JButton ("1");
        Button2 = new JButton ("2");
        Button3 = new JButton ("3");

        Button2.setEnabled(false);

        exmpFrame.setLayout(new FlowLayout(FlowLayout.CENTER));

        // better to pack() to the size of content..  BNI
        exmpFrame.setSize(250,150);
        exmpFrame.setVisible(true);
        exmpFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        exmpFrame.add(Button1);
        exmpFrame.add(Button2);
        exmpFrame.add(Button3);

        exmpFrame.setVisible(true);
    }

    public static void main(String[] args)  {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new ExmpFile();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

答案 1 :(得分:0)

在你的代码中,你试图在静态main方法中访问非静态变量,因此它会编译错误。

试试这个

   import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class jButExmp {
    JFrame exmpFrame = new JFrame("Example Frame");
    JButton button1, button2, button3;

    public JButton getButton1() {
        return button1;
    }

    public void setButton1(JButton button1) {
        this.button1 = button1;
    }

    public JButton getButton2() {
        return button2;
    }

    public void setButton2(JButton button2) {
        this.button2 = button2;
    }

    public JButton getButton3() {
        return button3;
    }

    public void setButton3(JButton button3) {
        this.button3 = button3;
    }

    public jButExmp() {
        exmpFrame = new JFrame("Example Frame");
        exmpFrame.setLayout(new FlowLayout());
        exmpFrame.setSize(250, 150);
        exmpFrame.setVisible(true);
        button1 = new JButton("1");
        button2 = new JButton("2");
        button3 = new JButton("3");
        exmpFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        exmpFrame.add(button1);
        exmpFrame.add(button2);
        exmpFrame.add(button3);
    }

    public static void main(String[] args) {
        jButExmp jButExmpRef = new jButExmp();

        jButExmpRef.getButton1().setSize(80, 30); // set size of button
        jButExmpRef.getButton1().setLocation(0, 0);
        jButExmpRef.getButton1().setEnabled(true);

        jButExmpRef.getButton2().setSize(80, 30);
        jButExmpRef.getButton2().setLocation(90, 0);
        jButExmpRef.getButton2().setEnabled(false);

    }
}

答案 2 :(得分:0)

这是更惯用的方法,虽然这段代码有一些其他的问题,包括你没有像按钮1和按钮2那样设置button3,而且事实上你正在设置使用默认FlowLayout(不支持设置位置)时按钮的位置。

import javax.swing.JButton;
import javax.swing.JFrame;


    public class JButExmp extends JFrame {  

    JButton button1;
    JButton button2;
    JButton button3;

     public JButExmp (String title)  {

         super(title);

         button1 = new JButton("1");
         button2 = new JButton("2");
         button3 = new JButton("3");

         add(button1);
         add(button2);
         add(button3);

         button1.setSize(80, 30);  //set size of button
         button1.setLocation(0,0);
         button1.setEnabled(true);

         button2.setSize(80,30);
         button2.setLocation(90, 0);
         button2.setEnabled(false);

         setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
         setLayout(new FlowLayout());
         setSize(250,150);
         setVisible(true);
     }

     public static void main(String[] args)  {
         JButExmp jButExmp = new JButExmp("Example Frame");
     }
}