1. 程式人生 > 其它 >java swing 建立登入視窗

java swing 建立登入視窗

技術標籤:javaswing

public static void CreateJFrame(){
		final JFrame jFrame = new JFrame("標題");
		jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		jFrame.setBounds(600, 200, 300, 220);
		jFrame.setLocationRelativeTo(null);//居中
		Container c = jFrame.getContentPane();

		/*標題部分--North*/
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new FlowLayout());
		titlePanel.add(new JLabel("XXX登入系統"));
		c.add(titlePanel, "North");

		/*輸入部分--Center*/
		JPanel jp=new JPanel();    //建立面板
		jp.setLayout(null);

		JLabel lableUser = new JLabel("使用者名稱");
		lableUser.setBounds(50, 20, 50, 20);
		JLabel lablePwd = new JLabel("密   碼");
		lablePwd.setBounds(50, 60, 50, 20);
		jp.add(lableUser);
		jp.add(lablePwd);

		final JTextField txtUser = new JTextField();
		final JPasswordField  txtPwd = new JPasswordField ();
		txtUser.setBounds(110, 20, 120, 20);
		txtPwd.setBounds(110, 60, 120, 20);
		jp.add(txtUser);
		jp.add(txtPwd);
		c.add(jp, "Center");

		/*按鈕部分--South*/
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		JButton okbtn = new JButton("確定");
		buttonPanel.add(okbtn);
		c.add(buttonPanel, "South");

		jFrame.setVisible(true);    //設定視窗是否可見
		okbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String username = txtUser.getText();
				String pwd = String.valueOf(txtPwd.getPassword());
				if(username.equals("admin") && pwd.equals("123")){
					
					jFrame.dispose();//銷燬本窗體

				}
			}
		});
	}
public static void main(String[] a){
		CreateJFrame();
}