62,614
社区成员
发帖
与我相关
我的任务
分享
package 第三次;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Login extends Frame implements ActionListener,WindowListener{
private Label[] label;
private TextField[] text;
private Checkbox[] check;//单选框
private CheckboxGroup group;
private Choice choice;//下拉框
private Button[] button;
private Panel[] panel=new Panel[3];;
public Login(){
label=new Label[5];
for(int i=0;i<label.length;i++)
{
label[i]=new Label();
}
text=new TextField[4];
for(int i=0;i<text.length;i++)
{
text[i]=new TextField();
}
check=new Checkbox[2];
for(int i=0;i<check.length;i++)
{
check[i]=new Checkbox();
}
group=new CheckboxGroup();
choice=new Choice();
button=new Button[2];
for(int i=0;i<button.length;i++)
{
button[i]=new Button();
}
panel=new Panel[3];
for(int i=0;i<panel.length;i++)
{
panel[i]=new Panel();
}
this.setVisible(true);
this.setResizable(false);//固定
this.setBounds(200, 300, 200, 350);
this.setTitle("注册");
//this.setLayout(null);
this.add(getPanel());
this.addWindowListener(this);
}
private Component getPanel() {
panel[0].setBounds(0,0, 200, 350);
panel[0].setLayout(new BorderLayout());
panel[0].add(getPanel1(),BorderLayout.NORTH);
panel[0].add(getPanel2(),BorderLayout.SOUTH);
return panel[0];
}
private Component getPanel1() {
label[0].setText("用户注册");
//panel[1].setLayout(null);
panel[1].setBounds(0, 0, 200, 30);
panel[1].add(label[0]);
return panel[1];
}
private Component getPanel2() {
label[1].setText("用户名");
label[2].setText("密码");
label[3].setText("性别");
label[4].setText("生肖");
text[0].setText("");//用户名
text[1].setText("");//密码
check[0]=new Checkbox("男",group,true);
check[1]=new Checkbox("女",group,true);
choice.addItem("龙");
choice.addItem("蛇");
choice.addItem("马");
choice.addItem("羊");
choice.addItem("猴");
choice.addItem("鼠");
choice.addItem("兔");
choice.addItem("虎");
choice.addItem("猪");
choice.addItem("狗");
choice.addItem("牛");
choice.addItem("鸡");
button[0].setLabel("确定");
button[1].setLabel("取消");
for(int i=1;i <4;i++)
{
panel[2].add(label[i]);
}
for(int i=0;i <4;i++)
{
panel[2].add(text[i]);
}
for(int i=0;i <2;i++)
{
panel[2].add(check[i]);
}
for(int i=0;i <2;i++)
{
panel[2].add(button[i]);
button[i].addActionListener(this);
}
panel[2].add(choice);
label[1].setBounds(30, 10, 80, 25);//用户名
text[0].setBounds(75, 10, 100, 25);
label[2].setBounds(30, 45, 80, 25);//密码
text[1].setBounds(75, 45, 100, 25);
label[3].setBounds(30, 80, 80, 25);//性别
check[0].setBounds(75, 80, 40, 25);
check[0].setBounds(120, 80, 40, 25);
label[4].setBounds(30, 45, 80, 25);//生肖
choice.setBounds(75, 115, 50, 25);
button[0].setBounds(40, 150, 60, 30);
button[1].setBounds(110, 150, 60, 30);
return panel[2];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str=e.getActionCommand();
if(e.getSource()==button[0]){
System.out.println("注册成功");
}else{
System.out.println("取消注册");
}
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}