java 可视化界面编程

2024-12-26 02:23:22
推荐回答(1个)
回答1:

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class awttest
{
TextField id,pw;
TextField printid,printpw;

public awttest(){
Frame f=new Frame("用户登录");
f.setLayout(new GridLayout(4,2));
id=new TextField("输入用户名",10);
pw=new TextField(10);
pw.setEchoChar('*');
f.add(new Label("用户名:",Label.CENTER));
f.add(id);
f.add(new Label("密码:",Label.CENTER));
f.add(pw);
Button b1=new Button("登陆");
Button b2=new Button("取消");
f.add(b1);
f.add(b2);
String str1=id.getText();
String str2=pw.getText();
printid=new TextField(str1,10);
printpw=new TextField(str2,10);
f.add(printid);
f.add(printpw);
f.pack();

printid.setBackground(new Color(220,0,0));
printpw.setBackground(new Color(220,0,0));
f.setSize(250,120);
f.setVisible(true);

b1.addActionListener(new ActionListener() { // 点击“显示窗口”菜单后将窗口显示出来
public void actionPerformed(ActionEvent e) {
printid.setText(id.getText());
printpw.setText(pw.getText());

}
});

f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent args)
{
System.exit(0);
}
}
);
}

public static void main(String[] args){
new awttest();

}

}

OK!!可以运行