0

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

 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);

 }
}
4

3 回答 3

1

ExmpFile 屏幕截图

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);
    }
}
于 2013-01-11T05:15:00.593 回答
0

这将是更惯用的方法,尽管此代码还有一些其他问题,包括您没有像使用 button1 和 button2 那样设置 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");
     }
}
于 2013-01-11T05:09:37.613 回答
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);

    }
}
于 2013-01-11T04:58:20.570 回答