2013-01-11 22 views
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); 

} 
} 
+0

你在哪裏得到的錯誤? –

+0

你在哪一行出錯? –

+0

主要方法的第一行 – Jay

回答

0

在你的代碼中,你正試圖訪問靜態主方法中的靜態變量,所以會產生編譯錯誤。

試試這個

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

這將編譯並運行,但風格非常糟 – GreyBeardedGeek

+0

@GreyBeardedGeek我應該怎麼做呢? – Jay

+0

給了Class一些重構。 – NPKR

0

這woud是做的更地道的方式,但這種代碼有一些其他的問題,包括事實,你不設立BUTTON3你與按鈕1的方式, button2,以及您在使用默認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"); 
    } 
} 
+0

我原本有代碼讓它看不見,但是我覺得這樣做會是徒勞無益的,因爲它不會做太多事情。我會立即刪除它! – Jay

+0

爲什麼要延長框架?您添加或更改了哪些功能?請參閱[繼承構成](http://en.wikipedia.org/wiki/Composition_over_inheritance)(我可能會說'查看它',但您可以'按照鏈接')。 -1 –

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); 
    } 
} 
+0

+ 1爲invokeLater和實際編譯和運行的東西。 -1缺少封裝:-( – GreyBeardedGeek

+0

@GreyBeardedGeek *「缺少封裝」* Huh? –

+0

是的,正好 - 嘗試查找它 - 這是面向對象設計的基本原則之一。 – GreyBeardedGeek

相關問題