3 回答

TA貢獻1909條經驗 獲得超7個贊
public void Log()
建議把這一句改成類的構造函數Login(){。。。。。。},如果對布局有要求,那么加個布局管理器c.setLayout(new FlowLayout());
setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3,Toolkit.getDefaultToolkit().getScreenSize().width/3);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
開頭加個this.

TA貢獻1780條經驗 獲得超5個贊
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Login extends JFrame implements ActionListener{
JLabel accountLabel=new JLabel("賬號:");
JLabel pwdLabel=new JLabel("密碼:");
JTextField name=new JTextField(10);
JPasswordField password=new JPasswordField();
JButton butnSure=new JButton("確定");
JButton butnCancel= new JButton("取消");;
public Login(String s) {
super(s);
setLayout(null);
add(accountLabel);
accountLabel.setBounds(45, 20, 100, 25);
add(pwdLabel);
pwdLabel.setBounds(45, 60, 100, 25);
add(name);
name.setBounds(105, 20, 110, 25);
add(password);
password.setBounds(105, 60, 110, 25);
add(butnSure);
butnSure.setBounds(45, 100, 80, 25);
add(butnCancel);
butnCancel.setBounds(135, 100, 80, 25);
butnSure.addActionListener(this);
butnCancel.addActionListener(this);
setVisible(true);
setSize(300, 200);
setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 3,
Toolkit.getDefaultToolkit().getScreenSize().width / 6);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();//刷新
}
public static void main(String[] args) {
Login log = new Login("用戶登陸");
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() ==butnSure){
System.out.println("用戶名:"+name.getText());
System.out.println("密碼:"+name.getText());
if("wyjwsj".equals(name.getText().trim())&&"12345".equals(password.getText().trim())){
this.dispose();
new MainFrm("用戶界面",name.getText().trim(),password.getText().trim());
}else {
JOptionPane.showMessageDialog(this, "用戶名或密碼不對,用戶名為 wyjwsj,密碼為 12345");
}
}else if(e.getSource()==butnCancel){
System.exit(1);
}
}
}
class MainFrm extends JFrame{
private JLabel info;
public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陸成功,用戶名:"+name+",密碼:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
添加回答
舉報