实验内容:创建一个窗体(不可改变大小),如下图所示,并添加上相应的事件监听。 实验步骤:(算法描述、源程序、操作步骤和方法) 在creatUI方法中定义,先设置Frame窗口, 新建基本容器Panel类,设置事件,添加标签用户名,密码 Panel p12= new Panel(); 继续添加Panel对象panel12,在panel12上添加组件TextField();设置长度为10个字符,并用setEchochar()方法设置回显字符为*,同时设置事件监听addActionListener(this);设置Panel类的位置, Panel p2=new Panel(); 添加按钮,Button submit=new Button(“登陆”); Button reset=new Button(“取消”);添加事件监听, 设置p1,p2的位置大小,可见性。 设置submi,clear相关事件,建立submit(),clear()方法 public void clear(){ name.setText(\"\"); password.setText(\"\"); } public void submit(){ String n = name.getText(); String psw=password.getText(); System.out.println(\"用户名:\"+n+\"\密码\"+psw); 最后mian()方法调用 实验步骤:(算法描述、源程序、操作步骤和方法) package 实验; import java.awt.*; import java.awt.event.*; public class TestFrame implements ActionListener{ TextField name; TextField password; public static void main(String args[]){ TestFrame ttf = new TestFrame(); ttf.creatUI(); } public void creatUI(){ Frame f = new Frame(\"登陆界面\"); Panel p1= new Panel(); p1.setLayout(new BorderLayout()); Panel p11=new Panel(); p11.setLayout(new GridLayout(2,1)); p11.add(new Label(\"用户名\")); p11.add(new Label(\"密码\")); Panel p12= new Panel(); p12.setLayout(new GridLayout(2,1)); name = new TextField(10); name.addActionListener(this); password = new TextField(10); password.addActionListener(this); p12.add(name); p12.add(password); p1.add(p11,\"West\"); p1.add(p12,\"Center\"); Panel p2=new Panel(); Button submit = new Button(\"登陆\"); Button reset = new Button(\"取消\"); p2.add(submit); p2.add(reset); f.add(p1,\"Center\"); f.add(p2,\"South\"); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); f.setSize(300,150); f.setLocation(300,200); f.setVisible(true); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String s= e.getActionCommand(); if(s.equals(\"取消\")){ this.clear(); } else if (s.equals(\"登陆\")||(e.getSource()==name)||(e.getSource()==password)){ this.submit(); } } public void clear(){ name.setText(\"\"); password.setText(\"\"); } public void submit(){ String n = name.getText(); String psw=password.getText(); System.out.println(\"用户名:\"+n+\"\密码\"+psw); } }