Java 第二次作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 第二次作业相关的知识,希望对你有一定的参考价值。
package javazuoye;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class denglujiemian {
public static void main(String[] args) {
// TODO Auto-generated method stub
LoginDialog lo = new LoginDialog();
lo.setVisible(true);
}
}
public class MainWindow extends JFrame{
JLabel but1;
MainWindow(){
setTitle("系统登录");
setBounds(400,200,300,180);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// setLayout(null);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
but1 = new JLabel("登录成功!");
this.add(but1);
}
}
public class LoginDialog extends JDialog {
JLabel name;
JLabel a2;
JComboBox a3;
JPasswordField password;
JButton shi;
JButton fou;
LoginDialog(){
method1();
}
private void method1() {
// TODO Auto-generated method stub
setTitle("登录系统");
setBounds(500,250,220,140);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
setLayout(null);
setModal(true);
// this.setResizable(false);
name = new JLabel("用户名:");
name.setBounds(20,10,50,20);
this.add(name);
a2 = new JLabel("密码:");
a2.setBounds(20,32,50,20);
this.add(a2);
a3 = new JComboBox();
a3.addItem("huakaihualuo");
a3.addItem("hellow");
a3.setBounds(75,10,105,20);
this.add(a3);
password = new JPasswordField("666666");
password.setBounds(75,33,105,20);
password.setEchoChar(‘*‘);
this.add(password);
shi = new JButton("登录");
shi.setBounds(25,60,60,20);
add(shi);
shi.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
login();
}
});
fou =new JButton("退出");
fou.setBounds(110,60,60,20);
add(fou);
fou.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
logout();
}
});
}
void login(){
String accout = a3.getSelectedItem().toString();
String pwd = new String(password.getPassword());
if(accout.equals("huakaihualuo")&&pwd.equals("666666")){
setVisible(false);
showMainWindow();
}else{
JOptionPane.showMessageDialog(this, "用户名是"+accout + ",密码是" + pwd);
}
}
void logout(){
int ret = JOptionPane.showConfirmDialog(this,"退出登录?","取消登录",JOptionPane.YES_NO_OPTION);
if(ret ==JOptionPane.YES_NO_OPTION){
dispose();
}
}
void showMainWindow(){
MainWindow frm = new MainWindow();
frm.setVisible(true);
}
}
以上是关于Java 第二次作业的主要内容,如果未能解决你的问题,请参考以下文章