问题描述
packagetest;importjava.awt.*;importjava.awt.event.*;importjava.util.*;publicclassStudentInfoextendsFrameimplementsActionListener{staticHashMapmap=newHashMap();Buttonbt1=newButton();Buttonbt2=newButton();Buttonbt3=newButton();TextAreatextArea1=newTextArea();publicStudentInfo(){try{initialize();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubStudentInfostudent=newStudentInfo();student.setBackground(Color.cyan);student.setVisible(true);student.setSize(newDimension(400,250));//制定宽度和高度student.setTitle("学生信息");}privatevoidinitialize()throwsException{//TODOAuto-generatedmethodstubthis.setLayout(null);//取消布局管理器bt1.setBackground(Color.gray);bt1.setLabel("注册");bt1.setBounds(newRectangle(85,200,70,25));bt1.addActionListener(this);//本身实现监听借口bt2.setBounds(newRectangle(170,200,70,25));bt2.addMouseListener(newactionAdapter(this));//外部类实现监听接口bt2.setLabel("查看");bt2.setBackground(Color.cyan);textArea1.setEditable(false);textArea1.setText("");textArea1.setBackground(Color.white);textArea1.setBounds(newRectangle(80,40,250,150));bt3.setLabel("关闭");bt3.setBackground(Color.cyan);//************内部类实现监听接口***********bt3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){//关闭窗口System.exit(0);}});bt3.setBounds(newRectangle(225,200,70,25));this.setResizable(false);this.add(textArea1);this.add(bt2);this.add(bt3);this.add(bt1);}//***********实现监听器ActioonListener的actionPerformed方法publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubRegisterFrameregf=newRegisterFrame();regf.setTitle("信息注册");regf.setVisible(true);regf.setSize(newDimension(350,180));regf.setBackground(Color.cyan);}}classRegisterFrameimplementsActionListener{Labellabel1=newLabel();TextFieldtextField1=newTextField();Labellabel2=newLabel();TextFieldtextField2=newTextField();Buttonbutton1=newButton();Buttonbutton2=newButton();publicRegisterFrame(){try{register();}catch(Exceptione){e.printStackTrace();}}/***@throwsException*/privatevoidregister()throwsException{//TODOAuto-generatedmethodstubthis.setLayout(null);label1.setText("学生学号:");label1.setBackground(Color.cyan);label1.setBounds(newRectangle(50,50,60,20));textField1.setText("");textField1.setBounds(newRectangle(120,50,140,20));label2.setBounds(newRectangle(50,80,60,20));label2.setText("学生姓名:");label2.setBackground(Color.cyan);textField2.setText("");textField2.setBounds(newRectangle(120,80,140,20));button1.setLabel("确定");button1.setBackground(Color.cyan);button1.addActionListener(this);button1.setBounds(newRectangle(85,120,70,25));button2.setBounds(newRectangle(180,120,70,25));button2.setBackground(Color.cyan);button2.addActionListener(newActionListener(){//内部类publicvoidactionPerformed(ActionEventevent){System.exit(0);}});button2.setLabel("取消");this.setResizable(false);this.add(button2);this.add(label1);this.add(label2);this.add(textField1);this.add(textField2);this.add(button1);}//********事件监听器*****************publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubStringid=textField1.getText();Stringname=textField2.getText();StudentInfo.map.put(id,name);this.diapose();}}//************鼠标事件适配器**********************classactionAdapterextendsMouseAdapter{StudentInfostudentAdapter;publicactionAdapter(StudentInfostudentInfo){this.studentAdapter=studentInfo;}publicvoidmouseClicked(MouseEventevt){//鼠标点击事件Buttonbutton=(Button)evt.getSource();studentAdapter.textArea1.append("学号:"+id+"姓名:"+studentAdapter.map.get(id).toString()+'n'}}
解决方案
解决方案二:
StudentInfo是什么,类?
解决方案三:
看错了
解决方案四:
RegisterFrame应该要继承frame类吧然后最后studentAdapter.textArea1.append("学号:"+id+"姓名:"+studentAdapter.map.get(id).toString()+'n'后面少东西了吧
解决方案五:
id,name变量定义错误this.diapose();这个书写错误!类直接加变量应该要定义为static吧!!像这个studentAdapter.textArea1
解决方案六:
classRegisterFrameextendsFrameimplementsActionListener在这行加一个extendsFrame就行了,另外dispose()写错了.下面的"鼠标点击事件"中的id不知道你是想要干什么.如果是想把写进去的学号加到文本区中的话就要申明一个全局变量Stringid;这样才能保证id唯一!
解决方案七:
HashMap后面要有泛型吧
解决方案八:
起码把出错信息也打出来吧
解决方案九:
LZ这个代码就有错啊,setLayout(null)里面不能为空啊,actionPerformed()里面应该用this,label,button,textfield应该放到画布里面吧
解决方案十:
该回复于2011-03-03 12:49:41被版主删除
解决方案十一:
该回复于2011-03-03 14:26:34被版主删除
解决方案十二:
帮你稍微改了下,OK了packagecom.xxl.swing;importjava.awt.Button;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Label;importjava.awt.Rectangle;importjava.awt.TextArea;importjava.awt.TextField;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importjava.util.Set;importjavax.swing.JFrame;publicclassStudentInfoextendsJFrameimplementsActionListener{staticMap<String,String>map=newHashMap<String,String>();Buttonbt1=newButton();Buttonbt2=newButton();Buttonbt3=newButton();TextAreatextArea1=newTextArea();publicStudentInfo(){try{initialize();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubStudentInfostudent=newStudentInfo();student.setBackground(Color.cyan);student.setVisible(true);student.setSize(newDimension(400,250));//制定宽度和高度student.setTitle("学生信息");}privatevoidinitialize()throwsException{//TODOAuto-generatedmethodstubthis.setLayout(null);//取消布局管理器bt1.setBackground(Color.gray);bt1.setLabel("注册");bt1.setBounds(newRectangle(85,200,70,25));bt1.addActionListener(this);//本身实现监听借口bt2.setBounds(newRectangle(170,200,70,25));bt2.addMouseListener(newactionAdapter(this));//外部类实现监听接口bt2.setLabel("查看");bt2.setBackground(Color.cyan);textArea1.setEditable(false);textArea1.setText("");textArea1.setBackground(Color.white);textArea1.setBounds(newRectangle(80,40,250,150));bt3.setLabel("关闭");bt3.setBackground(Color.cyan);//************内部类实现监听接口***********bt3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){//关闭窗口System.exit(0);}});bt3.setBounds(newRectangle(225,200,70,25));this.setResizable(false);this.add(textArea1);this.add(bt2);this.add(bt3);this.add(bt1);}//***********实现监听器ActioonListener的actionPerformed方法publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubRegisterFrameregf=newRegisterFrame();regf.setTitle("信息注册");regf.setVisible(true);regf.setSize(newDimension(350,180));regf.setBackground(Color.cyan);}}classRegisterFrameextendsJFrameimplementsActionListener{Labellabel1=newLabel();TextFieldtextField1=newTextField();Labellabel2=newLabel();TextFieldtextField2=newTextField();Buttonbutton1=newButton();Buttonbutton2=newButton();publicRegisterFrame(){try{register();}catch(Exceptione){e.printStackTrace();}}/***@throwsException*/privatevoidregister()throwsException{//TODOAuto-generatedmethodstubthis.setLayout(null);label1.setText("学生学号:");label1.setBackground(Color.cyan);label1.setBounds(newRectangle(50,50,60,20));textField1.setText("");textField1.setBounds(newRectangle(120,50,140,20));label2.setBounds(newRectangle(50,80,60,20));label2.setText("学生姓名:");label2.setBackground(Color.cyan);textField2.setText("");textField2.setBounds(newRectangle(120,80,140,20));button1.setLabel("确定");button1.setBackground(Color.cyan);button1.addActionListener(this);button1.setBounds(newRectangle(85,120,70,25));button2.setBounds(newRectangle(180,120,70,25));button2.setBackground(Color.cyan);button2.addActionListener(newActionListener(){//内部类publicvoidactionPerformed(ActionEventevent){System.exit(0);}});button2.setLabel("取消");this.setResizable(false);this.add(button2);this.add(label1);this.add(label2);this.add(textField1);this.add(textField2);this.add(button1);}//********事件监听器*****************publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubStringid=textField1.getText();Stringname=textField2.getText();StudentInfo.map.put(id,name);this.dispose();}}//************鼠标事件适配器**********************classactionAdapterextendsMouseAdapter{StudentInfostudentAdapter;publicactionAdapter(StudentInfostudentInfo){this.studentAdapter=studentInfo;}publicvoidmouseClicked(MouseEventevt){//鼠标点击事件Buttonbutton=(Button)evt.getSource();//Mapmap=this.studentAdapter.map;//shit,这里把map的类型给丢了,导致后面for出错this.studentAdapter.textArea1.setText("");for(Map.Entry<String,String>m:this.studentAdapter.map.entrySet()){studentAdapter.textArea1.append("学号:"+m.getKey()+"姓名:"+m.getValue()+"n");}}}
解决方案十三:
楼上的都改的像自己做的了,我在LZ的代码上改动后也行.代码如下:packagetest;importjava.awt.*;importjava.awt.event.*;importjava.util.*;publicclassStudentInfoextendsFrameimplementsActionListener{staticHashMapmap=newHashMap();Buttonbt1=newButton();Buttonbt2=newButton();Buttonbt3=newButton();TextAreatextArea1=newTextArea();Stringid;publicStudentInfo(){try{initialize();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubStudentInfostudent=newStudentInfo();student.setBackground(Color.cyan);student.setVisible(true);student.setSize(newDimension(400,250));//制定宽度和高度student.setTitle("学生信息");}privatevoidinitialize()throwsException{//TODOAuto-generatedmethodstubthis.setLayout(null);//取消布局管理器bt1.setBackground(Color.gray);bt1.setLabel("注册");bt1.setBounds(newRectangle(85,200,70,25));bt1.addActionListener(this);//本身实现监听接口bt2.setBounds(newRectangle(170,200,70,25));bt2.addMouseListener(newactionAdapter(this));//外部类实现监听接口bt2.setLabel("查看");bt2.setBackground(Color.cyan);textArea1.setEditable(false);textArea1.setText("");textArea1.setBackground(Color.white);textArea1.setBounds(newRectangle(80,40,250,150));bt3.setLabel("关闭");bt3.setBackground(Color.cyan);//************内部类实现监听接口***********bt3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){//关闭窗口System.exit(0);}});bt3.setBounds(newRectangle(225,200,70,25));this.setResizable(false);this.add(textArea1);this.add(bt2);this.add(bt3);this.add(bt1);}//***********实现监听器ActioonListener的actionPerformed方法publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubRegisterFrameregf=newRegisterFrame();regf.setTitle("信息注册");regf.setVisible(true);regf.setSize(newDimension(350,180));regf.setBackground(Color.cyan);}//************鼠标事件适配器**********************classactionAdapterextendsMouseAdapter{StudentInfostudentAdapter;publicactionAdapter(StudentInfostudentInfo){this.studentAdapter=studentInfo;}publicvoidmouseClicked(MouseEventevt){//鼠标点击事件Buttonbutton=(Button)evt.getSource();studentAdapter.textArea1.append("学号:"+id+'t'+"姓名:"+studentAdapter.map.get(id).toString()+'n');}}classRegisterFrameextendsFrameimplementsActionListener{Labellabel1=newLabel();TextFieldtextField1=newTextField();Labellabel2=newLabel();TextFieldtextField2=newTextField();Buttonbutton1=newButton();Buttonbutton2=newButton();publicRegisterFrame(){try{register();}catch(Exceptione){e.printStackTrace();}}/***@throwsException*/privatevoidregister()throwsException{//TODOAuto-generatedmethodstubthis.setLayout(null);label1.setText("学生学号:");label1.setBackground(Color.cyan);label1.setBounds(newRectangle(50,50,60,20));textField1.setText("");textField1.setBounds(newRectangle(120,50,140,20));label2.setBounds(newRectangle(50,80,60,20));label2.setText("学生姓名:");label2.setBackground(Color.cyan);textField2.setText("");textField2.setBounds(newRectangle(120,80,140,20));button1.setLabel("确定");button1.setBackground(Color.cyan);button1.addActionListener(this);button1.setBounds(newRectangle(85,120,70,25));button2.setBounds(newRectangle(180,120,70,25));button2.setBackground(Color.cyan);button2.addActionListener(newActionListener(){//内部类publicvoidactionPerformed(ActionEventevent){System.exit(0);}});button2.setLabel("取消");this.setResizable(false);this.add(button2);this.add(label1);this.add(label2);this.add(textField1);this.add(textField2);this.add(button1);}//********事件监听器*****************publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubid=textField1.getText();Stringname=textField2.getText();StudentInfo.map.put(id,name);this.dispose();}}}以上是把"鼠标事件适配器"类放到上面.并且把所有的类都放到了StudentInfo类中.再申明一个全局变量id.和上面我有发过的改动就OK了!其他都没动.不过这样的结构有点乱,也是为了尽量不要改支LZ的源代码!
解决方案十四:
不知道你的程序是在什么环境下编写的,如果有错的话一般编译环境都会给你错误信息,然后你再根据错误信息来逐个查找,这样会很有针对性。你现在只是将一大段代码粘贴上来,也许有人为你解决了问题,但你自己又学到了什么东西呢?以后再遇到别的错误怎么办?