2011-04-27 150 views
1

我如何執行Java程序時,我選擇單選按鈕當我選擇單選按鈕

class abc implements ActionListener { 
    public static void main (String args[]){ 

     JFrame frame = new JFrame("Test Frame"); 
     JPanel panel = new JPanel(); 
     Container contpane; contpane = frame.getContentPane(); 

     //added all Radio Buttons 
     JRadiobutton jb = new 
     JRadioButton("test1"); JRadiobutton 
     jb1 = new JRadioButton("test2"); 
     jb.addActionListener(this); 
     jb.addActionListener(this);//when i say "this" it is giving complie time 
        error becaus this keyword will not be accesible 

     } 

     public void actionPerformed(ActionEvent evt){ 

    How do i create the JTextArea and 
    Execute the Java class abc.java in 
    that JTextArea How do i add the 
    JTextArea to the panel and Frame 

    what should i write inorder to execute 
    the java class say abc.java 


    } 
    } 
+0

錯誤消息是正確的,因爲在靜態方法中沒有「this」。解決方案:不要在靜態main方法中做這些事情,而是在Bala顯示給你的非靜態方法或構造函數中執行它。 – 2011-04-27 02:37:26

回答

2

要回答你的第一個問題,我要如何執行Java程序,重組你的代碼來創建框架和它是你的班級的構造這樣的內容

class abc implements ActionListener { 
    public static void main(String args[]) { 

     new abc(); 
    } 

    abc() { 
     JFrame frame = new JFrame("Test Frame"); 
     JPanel panel = new JPanel(); 
     Container contpane; 
     contpane = frame.getContentPane(); 

     // added all Radio Buttons 
     JRadiobutton jb = new JRadioButton("test1"); 
     JRadiobutton jb1 = new JRadioButton("test2"); 
     jb.addActionListener(this); 
     jb.addActionListener(this); 

    } 

    public void actionPerformed(ActionEvent evt) { 

    } 
} 

我不明白其他問題。

+0

Bala R:我如何在actionPerformed()中創建JTextArea並將其添加到面板和框架中並在此方法內執行Java類 – Someone 2011-04-27 01:37:46