浏览量 4577
2013/04/24 18:27
package mmm;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Label;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.Button;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class LayoutDemo extends Frame {
private static final long serialVersionUID = 1L;
private Panel panel1 = null;
private Label label = null;
private TextField textField_username = null;
private Panel panel2 = null;
private Label label1_password = null;
private TextField textField_password = null;
private Panel panel = null;
private Button button_login = null;
private Button button_register = null;
private Panel getPanel1() {
if (panel1 == null) {
FlowLayout flowLayout1 = new FlowLayout();
flowLayout1.setAlignment(FlowLayout.LEFT);
label = new Label();
label.setText("用户名称:");
panel1 = new Panel();
panel1.setLayout(flowLayout1);
panel1.add(label, null);
panel1.add(getTextField_username(), null);
}
return panel1;
}
private TextField getTextField_username() {
if (textField_username == null) {
textField_username = new TextField();
textField_username.setColumns(20);
}
return textField_username;
}
private Panel getPanel2() {
if (panel2 == null) {
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
label1_password = new Label();
label1_password.setText("用户密码:");
panel2 = new Panel();
panel2.setLayout(flowLayout);
panel2.add(label1_password, null);
panel2.add(getTextField_password(), null);
}
return panel2;
}
private TextField getTextField_password() {
if (textField_password == null) {
textField_password = new TextField();
textField_password.setColumns(20);
}
return textField_password;
}
private Panel getPanel() {
if (panel == null) {
FlowLayout flowLayout2 = new FlowLayout();
flowLayout2.setHgap(20);
panel = new Panel();
panel.setLayout(flowLayout2);
panel.add(getButton_login(), null);
panel.add(getButton_register(), null);
}
return panel;
}
private Button getButton_login() {
if (button_login == null) {
button_login = new Button();
button_login.setLabel("登陆");
button_login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//获取文本框的内容
String name=textField_username.getText();
String password=textField_password.getText();
if("admin".equals(name)&&"password".equals(password)){
System.out.println("登陆成功");
}else{
System.out.println("用户名或密码错误");
}
}
});
}
return button_login;
}
private Button getButton_register() {
if (button_register == null) {
button_register = new Button();
button_register.setLabel("注册");
}
return button_register;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new LayoutDemo();
}
public LayoutDemo() {
super();
initialize();
// this.addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
private void initialize() {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(3);
gridLayout.setHgap(0);
gridLayout.setColumns(1);
this.setLayout(gridLayout);
this.setSize(300, 200);
this.setResizable(false);
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/source/1.jpg")));
this.setTitle("登陆");
Toolkit tool=Toolkit.getDefaultToolkit();
Dimension d=tool.getScreenSize();
double h=d.getHeight();
double w=d.getWidth();
int x=(int)(w-300)/2;
int y=(int)(h-200)/2;
this.setLocation(x, y);
this.setVisible(true);
this.add(getPanel1(), null);
this.add(getPanel2(), null);
this.add(getPanel(), null);
}
} // @jve:decl-index=0:visual-constraint="12,16"
//class MyWindowListener implements WindowListener{
//
// @Override
// public void windowOpened(WindowEvent e) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void windowClosing(WindowEvent e) {
// // TODO Auto-generated method stub
// System.exit(0);//quit
// }
//
// @Override
// public void windowClosed(WindowEvent e) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void windowIconified(WindowEvent e) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void windowDeiconified(WindowEvent e) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void windowActivated(WindowEvent e) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void windowDeactivated(WindowEvent e) {
// // TODO Auto-generated method stub
//
// }
//
//}
上一篇 搜索 下一篇