package test;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class StudentInfo extends Frame implements ActionListener{
static HashMap map = new HashMap();
Button bt1 = new Button();
Button bt2 = new Button();
Button bt3= new Button();
TextArea textArea1 = new TextArea(); public StudentInfo(){
try{
initialize();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StudentInfo student = new StudentInfo();
student.setBackground(Color.cyan);
student.setVisible(true);
student.setSize(new Dimension(400, 250)); //制定宽度和高度
student.setTitle("学生信息");
}
private void initialize() throws Exception {
// TODO Auto-generated method stub
this.setLayout(null); //取消布局管理器
bt1.setBackground(Color.gray);
bt1.setLabel("注册");
bt1.setBounds(new Rectangle(85, 200, 70, 25));
bt1.addActionListener(this); //本身实现监听借口
bt2.setBounds(new Rectangle(170, 200, 70, 25));
bt2.addMouseListener(new actionAdapter(this)); //外部类实现监听接口
bt2.setLabel("查看 ");
bt2.setBackground(Color.cyan);
textArea1.setEditable(false);
textArea1.setText("");
textArea1.setBackground(Color.white);
textArea1.setBounds(new Rectangle(80,40,250,150));
bt3.setLabel("关闭");
bt3.setBackground(Color.cyan);
//************内部类实现监听接口***********
bt3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){ //关闭窗口
System.exit(0);
}
});
bt3.setBounds(new Rectangle(225, 200, 70, 25));
this.setResizable(false);
this.add(textArea1);
this.add(bt2);
this.add(bt3);
this.add(bt1);
}
//***********实现监听器ActioonListener的actionPerformed方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
RegisterFrame regf = new RegisterFrame();
regf.setTitle("信息注册");
regf.setVisible(true);
regf.setSize(new Dimension(350,180));
regf.setBackground(Color.cyan);
}}class RegisterFrame implements ActionListener{ Label label1 = new Label();
TextField textField1 = new TextField();
Label label2 = new Label();
TextField textField2 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
public RegisterFrame(){
try{
register();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @throws Exception
*/
private void register() throws Exception{
// TODO Auto-generated method stub
this.setLayout(null);
label1.setText("学生学号:");
label1.setBackground(Color.cyan);
label1.setBounds(new Rectangle(50, 50, 60, 20));
textField1.setText("");
textField1.setBounds(new Rectangle(120, 50, 140, 20));
label2.setBounds(new Rectangle(50, 80, 60, 20));
label2.setText("学生姓名:");
label2.setBackground(Color.cyan);
textField2.setText("");
textField2.setBounds(new Rectangle(120, 80, 140, 20));
button1.setLabel("确定");
button1.setBackground(Color.cyan);
button1.addActionListener(this);
button1.setBounds(new Rectangle(85, 120, 70, 25));
button2.setBounds(new Rectangle(180, 120, 70, 25));
button2.setBackground(Color.cyan);
button2.addActionListener(new ActionListener(){ //内部类
public void actionPerformed(ActionEvent event){
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);
}
//********事件监听器*****************
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String id = textField1.getText();
String name = textField2.getText();
StudentInfo.map.put(id, name);
this.diapose();
}
}//************鼠标事件适配器**********************
class actionAdapter extends MouseAdapter{
StudentInfo studentAdapter;
public actionAdapter(StudentInfo studentInfo){
this.studentAdapter = studentInfo;
}
public void mouseClicked(MouseEvent evt){ //鼠标点击事件
Button button = (Button)evt.getSource();
studentAdapter.textArea1.append("学号: "+ id + " 姓名:"
+studentAdapter.map.get(id).toString() + '\n'
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class StudentInfo extends Frame implements ActionListener{
static HashMap map = new HashMap();
Button bt1 = new Button();
Button bt2 = new Button();
Button bt3= new Button();
TextArea textArea1 = new TextArea(); public StudentInfo(){
try{
initialize();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StudentInfo student = new StudentInfo();
student.setBackground(Color.cyan);
student.setVisible(true);
student.setSize(new Dimension(400, 250)); //制定宽度和高度
student.setTitle("学生信息");
}
private void initialize() throws Exception {
// TODO Auto-generated method stub
this.setLayout(null); //取消布局管理器
bt1.setBackground(Color.gray);
bt1.setLabel("注册");
bt1.setBounds(new Rectangle(85, 200, 70, 25));
bt1.addActionListener(this); //本身实现监听借口
bt2.setBounds(new Rectangle(170, 200, 70, 25));
bt2.addMouseListener(new actionAdapter(this)); //外部类实现监听接口
bt2.setLabel("查看 ");
bt2.setBackground(Color.cyan);
textArea1.setEditable(false);
textArea1.setText("");
textArea1.setBackground(Color.white);
textArea1.setBounds(new Rectangle(80,40,250,150));
bt3.setLabel("关闭");
bt3.setBackground(Color.cyan);
//************内部类实现监听接口***********
bt3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){ //关闭窗口
System.exit(0);
}
});
bt3.setBounds(new Rectangle(225, 200, 70, 25));
this.setResizable(false);
this.add(textArea1);
this.add(bt2);
this.add(bt3);
this.add(bt1);
}
//***********实现监听器ActioonListener的actionPerformed方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
RegisterFrame regf = new RegisterFrame();
regf.setTitle("信息注册");
regf.setVisible(true);
regf.setSize(new Dimension(350,180));
regf.setBackground(Color.cyan);
}}class RegisterFrame implements ActionListener{ Label label1 = new Label();
TextField textField1 = new TextField();
Label label2 = new Label();
TextField textField2 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
public RegisterFrame(){
try{
register();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @throws Exception
*/
private void register() throws Exception{
// TODO Auto-generated method stub
this.setLayout(null);
label1.setText("学生学号:");
label1.setBackground(Color.cyan);
label1.setBounds(new Rectangle(50, 50, 60, 20));
textField1.setText("");
textField1.setBounds(new Rectangle(120, 50, 140, 20));
label2.setBounds(new Rectangle(50, 80, 60, 20));
label2.setText("学生姓名:");
label2.setBackground(Color.cyan);
textField2.setText("");
textField2.setBounds(new Rectangle(120, 80, 140, 20));
button1.setLabel("确定");
button1.setBackground(Color.cyan);
button1.addActionListener(this);
button1.setBounds(new Rectangle(85, 120, 70, 25));
button2.setBounds(new Rectangle(180, 120, 70, 25));
button2.setBackground(Color.cyan);
button2.addActionListener(new ActionListener(){ //内部类
public void actionPerformed(ActionEvent event){
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);
}
//********事件监听器*****************
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String id = textField1.getText();
String name = textField2.getText();
StudentInfo.map.put(id, name);
this.diapose();
}
}//************鼠标事件适配器**********************
class actionAdapter extends MouseAdapter{
StudentInfo studentAdapter;
public actionAdapter(StudentInfo studentInfo){
this.studentAdapter = studentInfo;
}
public void mouseClicked(MouseEvent evt){ //鼠标点击事件
Button button = (Button)evt.getSource();
studentAdapter.textArea1.append("学号: "+ id + " 姓名:"
+studentAdapter.map.get(id).toString() + '\n'
}
}
然后最后studentAdapter.textArea1.append("学号: "+ id + " 姓名:"
+studentAdapter.map.get(id).toString() + '\n'
后面少东西了吧
在这行加一个extends Frame就行了, 另外dispose()写错了.下面的"鼠标点击事件"中的id不知道你是想要干什么.如果是想把写进去的学号加到文本区中的话就要申明一个全局变量 String id; 这样才能保证id唯一!
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;import javax.swing.JFrame;public class StudentInfo extends JFrame implements ActionListener {
static Map<String,String> map = new HashMap<String,String>();
Button bt1 = new Button();
Button bt2 = new Button();
Button bt3 = new Button();
TextArea textArea1 = new TextArea(); public StudentInfo() {
try {
initialize();
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
// TODO Auto-generated method stub
StudentInfo student = new StudentInfo();
student.setBackground(Color.cyan);
student.setVisible(true);
student.setSize(new Dimension(400, 250)); // 制定宽度和高度
student.setTitle("学生信息");
} private void initialize() throws Exception {
// TODO Auto-generated method stub
this.setLayout(null); // 取消布局管理器
bt1.setBackground(Color.gray);
bt1.setLabel("注册");
bt1.setBounds(new Rectangle(85, 200, 70, 25));
bt1.addActionListener(this); // 本身实现监听借口
bt2.setBounds(new Rectangle(170, 200, 70, 25));
bt2.addMouseListener(new actionAdapter(this)); // 外部类实现监听接口
bt2.setLabel("查看 ");
bt2.setBackground(Color.cyan);
textArea1.setEditable(false);
textArea1.setText("");
textArea1.setBackground(Color.white);
textArea1.setBounds(new Rectangle(80, 40, 250, 150));
bt3.setLabel("关闭");
bt3.setBackground(Color.cyan);
// ************内部类实现监听接口***********
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { // 关闭窗口
System.exit(0);
}
});
bt3.setBounds(new Rectangle(225, 200, 70, 25));
this.setResizable(false);
this.add(textArea1);
this.add(bt2);
this.add(bt3);
this.add(bt1);
} // ***********实现监听器ActioonListener的actionPerformed方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
RegisterFrame regf = new RegisterFrame();
regf.setTitle("信息注册");
regf.setVisible(true);
regf.setSize(new Dimension(350, 180));
regf.setBackground(Color.cyan);
}}class RegisterFrame extends JFrame implements ActionListener { Label label1 = new Label();
TextField textField1 = new TextField();
Label label2 = new Label();
TextField textField2 = new TextField();
Button button1 = new Button();
Button button2 = new Button(); public RegisterFrame() {
try {
register();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* @throws Exception
*/
private void register() throws Exception {
// TODO Auto-generated method stub
this.setLayout(null);
label1.setText("学生学号:");
label1.setBackground(Color.cyan);
label1.setBounds(new Rectangle(50, 50, 60, 20));
textField1.setText("");
textField1.setBounds(new Rectangle(120, 50, 140, 20));
label2.setBounds(new Rectangle(50, 80, 60, 20));
label2.setText("学生姓名:");
label2.setBackground(Color.cyan);
textField2.setText("");
textField2.setBounds(new Rectangle(120, 80, 140, 20));
button1.setLabel("确定");
button1.setBackground(Color.cyan);
button1.addActionListener(this);
button1.setBounds(new Rectangle(85, 120, 70, 25));
button2.setBounds(new Rectangle(180, 120, 70, 25));
button2.setBackground(Color.cyan);
button2.addActionListener(new ActionListener() { // 内部类
public void actionPerformed(ActionEvent event) {
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);
} // ********事件监听器*****************
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String id = textField1.getText();
String name = textField2.getText();
StudentInfo.map.put(id, name);
this.dispose();
}}// ************鼠标事件适配器**********************
class actionAdapter extends MouseAdapter {
StudentInfo studentAdapter; public actionAdapter(StudentInfo studentInfo) {
this.studentAdapter = studentInfo;
} public void mouseClicked(MouseEvent evt){ //鼠标点击事件
Button button = (Button)evt.getSource();
//Map map = 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");
}
}
}
import java.awt.event.*;
import java.util.*;public class StudentInfo extends Frame implements ActionListener {
static HashMap map = new HashMap();
Button bt1 = new Button();
Button bt2 = new Button();
Button bt3 = new Button();
TextArea textArea1 = new TextArea();
String id; public StudentInfo() {
try {
initialize();
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
// TODO Auto-generated method stub
StudentInfo student = new StudentInfo();
student.setBackground(Color.cyan);
student.setVisible(true);
student.setSize(new Dimension(400, 250)); // 制定宽度和高度
student.setTitle("学生信息");
} private void initialize() throws Exception {
// TODO Auto-generated method stub
this.setLayout(null); // 取消布局管理器
bt1.setBackground(Color.gray);
bt1.setLabel("注册");
bt1.setBounds(new Rectangle(85, 200, 70, 25));
bt1.addActionListener(this); // 本身实现监听接口
bt2.setBounds(new Rectangle(170, 200, 70, 25));
bt2.addMouseListener(new actionAdapter(this)); // 外部类实现监听接口
bt2.setLabel("查看 ");
bt2.setBackground(Color.cyan);
textArea1.setEditable(false);
textArea1.setText("");
textArea1.setBackground(Color.white);
textArea1.setBounds(new Rectangle(80, 40, 250, 150));
bt3.setLabel("关闭");
bt3.setBackground(Color.cyan);
// ************内部类实现监听接口***********
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { // 关闭窗口
System.exit(0);
}
});
bt3.setBounds(new Rectangle(225, 200, 70, 25));
this.setResizable(false);
this.add(textArea1);
this.add(bt2);
this.add(bt3);
this.add(bt1);
} // ***********实现监听器ActioonListener的actionPerformed方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
RegisterFrame regf = new RegisterFrame();
regf.setTitle("信息注册");
regf.setVisible(true);
regf.setSize(new Dimension(350, 180));
regf.setBackground(Color.cyan);
} // ************鼠标事件适配器**********************
class actionAdapter extends MouseAdapter {
StudentInfo studentAdapter; public actionAdapter(StudentInfo studentInfo) {
this.studentAdapter = studentInfo;
} public void mouseClicked(MouseEvent evt) { // 鼠标点击事件
Button button = (Button) evt.getSource();
studentAdapter.textArea1.append("学号: " + id + '\t' + " 姓名:"
+ studentAdapter.map.get(id).toString() + '\n');
}
} class RegisterFrame extends Frame implements ActionListener { Label label1 = new Label();
TextField textField1 = new TextField();
Label label2 = new Label();
TextField textField2 = new TextField();
Button button1 = new Button();
Button button2 = new Button(); public RegisterFrame() {
try {
register();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* @throws Exception
*/
private void register() throws Exception {
// TODO Auto-generated method stub
this.setLayout(null);
label1.setText("学生学号:");
label1.setBackground(Color.cyan);
label1.setBounds(new Rectangle(50, 50, 60, 20));
textField1.setText("");
textField1.setBounds(new Rectangle(120, 50, 140, 20));
label2.setBounds(new Rectangle(50, 80, 60, 20));
label2.setText("学生姓名:");
label2.setBackground(Color.cyan);
textField2.setText("");
textField2.setBounds(new Rectangle(120, 80, 140, 20));
button1.setLabel("确定");
button1.setBackground(Color.cyan);
button1.addActionListener(this);
button1.setBounds(new Rectangle(85, 120, 70, 25));
button2.setBounds(new Rectangle(180, 120, 70, 25));
button2.setBackground(Color.cyan);
button2.addActionListener(new ActionListener() { // 内部类
public void actionPerformed(ActionEvent event) {
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);
} // ********事件监听器*****************
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
id = textField1.getText();
String name = textField2.getText();
StudentInfo.map.put(id, name);
this.dispose();
} }
}以上是把"鼠标事件适配器"类放到上面.并且把所有的类都放到了StudentInfo类中.再申明一个全局变量id.和上面我有发过的改动就OK了!其他都没动.不过这样的结构有点乱,也是为了尽量不要改支LZ的源代码!