当我选择单选按钮时,如何执行Java程序

时间:2011-04-27 01:22:59

标签: java

当我选择单选按钮

时,如何执行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  


   } 
  }

1 个答案:

答案 0 :(得分:2)

要回答你的第一个问题,请重构代码以创建框架,并在类的构造函数中创建内容,如下所示

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

    }
}

我不明白其他问题。