java實現登陸介面
阿新 • • 發佈:2018-12-25
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class T2 { /** * @param args */ private static JFrame frame; private JTextField t1,t2; private JPasswordField password; private JLabel b1,b2,b3,b4; private JButton btn1,btn2; public T2(){ frame=new JFrame(); frame.setSize(300,400); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); b1=new JLabel("使用者名稱"); b2=new JLabel("密碼"); t1=new JTextField(10); password=new JPasswordField(10); btn1=new JButton("登陸"); btn2=new JButton("取消"); frame.add(b1); frame.add(t1); frame.add(b2); frame.add(password); frame.add(btn1); frame.add(btn2); btn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if((t1.getText().trim()).equals("餘雪鵬") && (password.getText().trim()).equals("123456")) { b3=new JLabel("密碼正確,登陸成功!"); frame.add(b3); frame.setVisible(true); } else{ b4=new JLabel("密碼錯誤 ,找回密碼!"); frame.add(b4); frame.setVisible(true); } } }); btn2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(e.getSource()==btn2) { System.exit(0); } } }); } public static void main(String[] args) { // TODO Auto-generated method stub new T2(); frame.setVisible(true); } }