編寫簡單的使用者登入介面

來源:互聯網
上載者:User

標籤:

//1、如果使用者名稱和密碼不填寫//彈出訊息對話方塊提示進行輸入使用者名稱和密碼//2、如果填寫了使用者名稱和密碼//(1)、如果使用者名稱和密碼的填寫都是admin彈出訊息對話方塊,提示登陸成功//(2)、如果使用者名稱和密碼的填寫的兩個都不是admin彈出對話方塊提示登入名稱或密碼錯誤import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.event.AncestorListener;public class  UserTest extends JFrame implements ActionListener(){//添加標籤JLable ul  = new JLable(使用者名稱);JLable pl  = new JLable(密碼);//文字框 ,長度為13JTextFiled ut = new JTextField(13);//密碼框,長度為13JTexedField pt = new JTextField(13);//按鈕JButton login = new JButton("登入");JButton cancel = new JButton("取消 ");//面板JPanel p1 = new JPanel();Jpanel p2 = new JPanel();JPanel p3 = new JPanel();//構造方法public UserTest(){this.setTitle("使用者登入測試“);//把控制項添加到面板p1.add(ul);p1.add(ut);p2.add(pl);p2.add(ut);p3.add(login);p3.add(cancel);//設定表單大小this.setSize(200,200);//設定表單布局方式 網格布局2行3列this.setLayout(new GridLayout(2,3));//把面板放到表單this.add(p1);this.add(p2);this.add(p3);//表單可見this.setVisible(true);
//表單可關閉this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @Override //重寫監聽方法public void actionPerformed(ActionEvent e) {if(e.getSource()== login){//監聽登入按鈕if(ut.getText().equals("admin")&&pt.getText().equals("admin")){//不覆蓋表單JOptionPane.showMessageDialog(null, "登入成功");}else if(ut.getText().equals("")||pt.getText().equals("")){JOptionPane.showMessageDialog(null, "使用者名稱或密碼不可為空");}else{JOptionPane.showMessageDialog(null, "使用者名稱或密碼錯誤");}}}}public static void main(String[] args){ new UserTest();//調用無參構造方法測試程式}



 

 

編寫簡單的使用者登入介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.