import java.awt.*;
import javax.swing.*;
import java.awt.event.*;class Framewithbutton extends JFrame{
Framewithbutton(String title){
super (title);
JButton button1,button2;
JPanel panel1,panel2;
JTextField tf;
tf=new JTextField(20);
button1=new JButton("1");
button2=new JButton("2");
panel1=new JPanel();
panel1.add(tf);
panel2=new JPanel();
panel2.add(button1);
panel2.add(button2);
    panel1.setBackground(Color.CYAN);
    panel2.setLayout(new FlowLayout());
    Container cp=getContentPane();
    AL listener=new AL();
    button1.addActionListener(listener);
    button2.addActionListener(listener);
    cp.add(panel1,BorderLayout.CENTER);
    cp.add(panel2,BorderLayout.SOUTH);
    setSize(350,240);
    setVisible(true);
    setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
  }
}
   class AL implements ActionListener{
   public  void actionPerformed(ActionEvent e){
     if((JButton)e.getSource()==button1)
     tf.setText("you clicked 1");
     else if((JButton)e.getSource()==button2)
     tf.setText("you clicked 2");
     }
    }public class Test3 {
public static void main(String args[]){
JFrame abc=new Framewithbutton("123");
}
}
代码中的借口为什么会报错呢???请指教一下

解决方案 »

  1.   

    我是这样写的
    /**
     * @(#)MyForm.java
     *
     *
     * @author
     * @version 1.00 2011/2/4
     */import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;public class MyForm implements ActionListener {
        JButton sure=new JButton("提交");
        JTextField txtName=new JTextField(12);                            //创建姓名文本框
        JPasswordField txtPassword=new JPasswordField(12);    //创建密码文本框
        //创建性别单选按钮
        JRadioButton rabM=new JRadioButton("男",true);
        JRadioButton rabF=new JRadioButton("女",false);
        //添加爱好复选框成员
        JCheckBox c1=new JCheckBox("逛街",true);
        JCheckBox c2=new JCheckBox("旅游",false);
        JCheckBox c3=new JCheckBox("运动",false);
        JCheckBox c4=new JCheckBox("看书",false);
        JCheckBox c5=new JCheckBox("上网",false);
        //创建学历列表框
        String[] s1={"博士","硕士","本科","专科","其他"};
        JList list1=new JList(s1);
        //创建出生年份组合框
        JComboBox cobY=new JComboBox();
        //创建出生月份组合框
        JComboBox cobM=new JComboBox();
        //创建简历文本区
        JTextArea txt=new JTextArea("请输入简历");    public MyForm() {
         JFrame jfrm=new JFrame("会员信息采集");
         jfrm.setSize(360,300);
         jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jfrm.setVisible(true);
         jfrm.setResizable(false);  //面板的大小不能变化
         Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();  //获取屏幕尺寸
         int w=jfrm.getSize().width;       //获取窗体大小
         int h=jfrm.getSize().height;
         int x=(dim.width-w)/2;     //取窗体左上角坐标
         int y=(dim.height-h)/2;
         jfrm.setLocation(x,y);     //将窗体移到屏幕中间
         String title[]={"姓名","密码","性别","出生年月","爱好","简历","学历","   ","   "};
         JLabel jb[]= new JLabel[9];    //批量创建标签
         for(int i=0;i<9;i++)
         {
         jb[i]=new JLabel(title[i]);
         }
         txtName.setBackground(Color.pink);    //设置文本框的背景色
         txtPassword.setBackground(Color.pink);
         ButtonGroup group=new ButtonGroup();   //为单选按钮定义组,实现单选
            group.add(rabM);                //将按钮加入到组中
            group.add(rabF);
            JPanel sex=new JPanel();        //建立面板sex,用于放置单选按钮
            sex.add(rabM);
            sex.add(rabF);
            //建立面板hobby,用于放置复选按钮
            JPanel hobby=new JPanel();
            hobby.add(c1);
            hobby.add(c2);
            hobby.add(c3);
            hobby.add(c4);
            hobby.add(c5);
            //设置学历列表框
            list1.setSelectedIndex(2);
            list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list1.setBackground(Color.white);
            list1.setSelectionBackground(Color.red);
            list1.setBorder(BorderFactory.createLineBorder(Color.black));
            JScrollPane lp=new JScrollPane(list1);
            lp.setPreferredSize(new Dimension(80,100));
            //设置出生年月,份组合框,加入年,月份
            for(int j=1960;j<2000;j++)
            cobY.addItem(Integer.toString(j));      //为组合框添加选项
            cobY.setSelectedIndex(20);              //设置默认选项
            cobY.setBackground(Color.white);
            for(int j=1;j<13;j++)
            cobM.addItem(Integer.toString(j));
            cobM.setSelectedIndex(0);
            cobM.setBackground(Color.white);
            JPanel birth=new JPanel();
            birth.add(cobY);
            birth.add(new JLabel("年"));
            birth.add(cobM);
            birth.add(new JLabel("月"));
            txt.setBackground(Color.orange);
            //文本区实现滚动
            JScrollPane jp=new JScrollPane(txt,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            jp.setPreferredSize(new Dimension(150,100));
            //将标签,独立组件和包含组件的面板进行布局,并添加到框架的适当位置
            JPanel jNorth=new JPanel();
            jNorth.add(jb[0]);  jNorth.add(txtName);
            jNorth.add(jb[1]);  jNorth.add(txtPassword);
            JPanel jCenter=new JPanel();
            jCenter.add(jb[2]); jCenter.add(sex);    //sex为性别面板
            jCenter.add(jb[3]); jCenter.add(birth);  //birth为生日面板
            jCenter.add(jb[4]); jCenter.add(hobby);  //hobby为爱好面板
            jCenter.add(jb[5]); jCenter.add(jp);     //jp为简历滚动面板
            jCenter.add(jb[6]); jCenter.add(lp);     //lp为学历滚动面板
            JPanel jSouth=new JPanel();
            jSouth.add(jb[7]);
            jSouth.add(sure);
            jSouth.add(jb[8]);
            jfrm.add(jNorth,BorderLayout.NORTH);     //将已经布局好的北,中,南三块面板添加到框架的内容面板中
            jfrm.add(jCenter,BorderLayout.CENTER);
            jfrm.add(jSouth,BorderLayout.SOUTH);
         sure.addActionListener(this);
        }
        //按钮的事件处理方法
        public void actionPerformed(ActionEvent e)
        {
         if(e.getActionCommand().equals("提交"))
         {
         sure.setEnabled(false);
         sure.setText("数据采集完毕!");
         JFrame newf=new JFrame("新窗口");
             JTextArea info=new JTextArea("您输入的信息是:"+"\n"); //定义文本域用来显示用户提交的信息
                info.append("您的姓名:"+txtName.getText()+"\n");
                info.append("您的密码:"+txtPassword.getText()+"\n");
                if(rabM.isSelected())
                 info.append("您的性别:"+rabM.getLabel()+"\n");
                 if(rabF.isSelected())
                     info.append("您的性别:"+rabF.getLabel()+"\n");
                info.append("您的出生年月:"+cobY.getSelectedItem()+"年"+cobM.getSelectedItem()+"月"+"\n");
                info.append("您的学历:"+list1.getSelectedValue().toString()+"\n");
                String fun=null;
                if(c1.isSelected()) fun=c1.getLabel()+" ,";
                if(c2.isSelected()) fun+=c2.getLabel()+" ,";
                if(c3.isSelected()) fun+=c3.getLabel()+" ,";
                if(c4.isSelected()) fun+=c4.getLabel()+" ,";
                if(c5.isSelected()) fun+=c5.getLabel();
                info.append("您的爱好:"+fun+"\n");
                info.append("您的简历:"+txt.getText());
                info.setEditable(false);    //设置文本域的可编辑性
                info.setBackground(Color.white);
         newf.add(info);
         newf.setSize(220,202);
         newf.setResizable(false);
         newf.show();     }
        } public static void main (String[] args) {
      JFrame.setDefaultLookAndFeelDecorated(true);  //采用最新的SWING外观
       new MyForm();    //在主函数中实例化MyForm对象
     }}
      

  2.   

    楼主,你的代码的层次有问题啊。我改了改,可以运行了,你自己看看吧import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;class Framewithbutton extends JFrame{

    private JButton button1;
    private JButton button2;
    private JPanel panel1,panel2;
    JTextField tf; class AL implements ActionListener{
      public void actionPerformed(ActionEvent e){
      if((JButton)e.getSource()==button1)
      tf.setText("you clicked 1");
      else if((JButton)e.getSource()==button2)
      tf.setText("you clicked 2");
      }
      }
    Framewithbutton(String title){
    super (title);

    tf=new JTextField(20);
    button1=new JButton("1");
    button2=new JButton("2");
    panel1=new JPanel();
    panel1.add(tf);
    panel2=new JPanel();
    panel2.add(button1);
    panel2.add(button2);
    panel1.setBackground(Color.CYAN);
    panel2.setLayout(new FlowLayout());
    Container cp=getContentPane();
    AL listener=new AL();
    button1.addActionListener(listener);
    button2.addActionListener(listener);
    cp.add(panel1,BorderLayout.CENTER);
    cp.add(panel2,BorderLayout.SOUTH);
    setSize(350,240);
    setVisible(true);
    setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
     }
    }public class Test3 {
    public static void main(String args[]){
    JFrame abc=new Framewithbutton("123");
    }
    }
      

  3.   

    运行就说过是,点击button1,text显示you clicked 1;类似的结果如button2
      

  4.   

    我把class AL implements ActionListener改成为了Framewithbutton的内部类,于是它就可以访问Framewithbutton的成员变量,如button1和button2等;