ç¨java设计ä¸ä¸ªç®åççé¢å¯ä»¥åèå¦ä¸å®ä¾ï¼
import javax.swing.JFrame;//æ¡æ¶
import javax.swing.JPanel;//é¢æ¿
import javax.swing.JButton;//æé®
import javax.swing.JLabel;//æ ç¾
import javax.swing.JTextField;//ææ¬æ¡
import java.awt.Font;//åä½
import java.awt.Color;//é¢è²
import javax.swing.JPasswordField;//å¯ç æ¡
import java.awt.event.ActionListener;//äºä»¶çå¬
import java.awt.event.ActionEvent;//äºä»¶å¤ç
import javax.swing.JOptionPane;//æ¶æ¯çªå£public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;
public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}
public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置å
³éæ¡æ¶çåæ¶ç»æç¨åº
this.setSize(300,200);//设置æ¡æ¶å¤§å°ä¸ºé¿300,宽200
this.setResizable(false);//设置æ¡æ¶ä¸å¯ä»¥æ¹å大å°
this.setTitle("ç¨æ·ç»å½");//设置æ¡æ¶æ é¢
this.pnluser.setLayout(null);//设置é¢æ¿å¸å±ç®¡ç
this.pnluser.setBackground(Color.cyan);//设置é¢æ¿èæ¯é¢è²
this.lbluserLogIn.setText("ç¨æ·ç»å½");//设置æ ç¾æ é¢
this.lbluserLogIn.setFont(new Font("å®ä½",Font.BOLD | Font.ITALIC,14));//设置æ ç¾åä½
this.lbluserLogIn.setForeground(Color.RED);//设置æ ç¾åä½é¢è²
this.lbluserName.setText("ç¨æ·å:");
this.lbluserPWD.setText("å¯ ç :");
this.btnSub.setText("ç»å½");
this.btnReset.setText("éç½®");
this.lbluserLogIn.setBounds(120,15,60,20);//设置æ ç¾xåæ 120,yåæ 15,é¿60,宽20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//å¿åç±»å®ç°ActionListeneræ¥å£
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//å¿åç±»å®ç°ActionListeneræ¥å£
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//å è½½æ ç¾å°é¢æ¿
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//å è½½é¢æ¿å°æ¡æ¶
this.setVisible(true);//设置æ¡æ¶å¯æ¾
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"è´¦å·ä¸è½ä¸ºç©º","é误",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"å¯ç ä¸è½ä¸ºç©º","é误",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"è´¦å·æå¯ç é误","é误",JOptionPane.ERROR_MESSAGE);
return;
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}
public static void main(String[] args){
new UserLogIn();
}
}