日期:2014-05-20 浏览次数:20765 次
import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; class Welcome extends Frame implements ActionListener { Welcome(String s) { setBounds(100, 100, 200, 150); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } } class MyWindow extends Frame implements ActionListener { Label L1, L2; TextField t1, t2; Button b1, b2; MenuBar menubar; Menu menu; MyWindow(String s) { // 窗口设置 super(s); setTitle(s); setLayout(new FlowLayout()); setBounds(100, 100, 200, 150); setVisible(true); setResizable(true); // 添加组件 menubar = new MenuBar(); menu = new Menu("黄海波1040610109"); menubar.add(menu); setMenuBar(menubar); L1 = new Label("输入用户名"); L2 = new Label("输入密码"); t1 = new TextField(10); t2 = new TextField(10); t2.setEchoChar('*'); Button b1 = new Button("OK"); Button b2 = new Button("Exit"); add(L1); add(t1); add(L2); add(t2); add(b1); add(b2); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String user = t1.getText(); String password = t2.getText(); if (user.equals("huanghaibo") && password.equals("123456")) { Welcome welcome = new Welcome("Welcome " + user); closeLoginFrame(); //this.setVisible(false);/*这里想在Welcome弹出后将MyWindow隐藏,可是报错。*/ } else { JOptionPane.showMessageDialog(null, "用户名或密码错误"); } } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(1); } }); validate(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } private void closeLoginFrame() { this.setVisible(false); //建议你用下面这个方法比较好 //this.dispose(); } } public class j411040610109 { public static void main(String[] args) { MyWindow win = new MyWindow("登陆"); } }