完成用户登陆
package GUI;
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class LoginUI {
public JFrame jf; public JPanel jp; public JLabel jLabel1; public JLabel jLabel2; public JLabel jLabel3; public JTextField jUserName; public JPasswordField jPassWord; public JButton post; public JButton reset; public LoginUI() { jf = new JFrame("Login"); jf.setSize(400, 175); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setLocationRelativeTo(null); jf.setResizable(false); jp = new JPanel(new FlowLayout()); jLabel1 = new JLabel(); jLabel1.setText("Account : "); jLabel1.setFont(new Font(null, Font.PLAIN, 18)); jLabel2 =new JLabel(); jLabel2.setText("Password : "); jLabel2.setFont(new Font(null, Font.PLAIN, 18)); jLabel3 =new JLabel(); jLabel3.setText("Please input your account and password"); jLabel3.setFont(new Font(null, Font.PLAIN, 18)); jUserName = new JTextField(16); jUserName.setFont(new Font(null, Font.PLAIN, 18)); jPassWord = new JPasswordField(16); jPassWord.setFont(new Font(null, Font.PLAIN, 18)); post = new JButton(" L o g i n "); post.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { String account = new String(jUserName.getText()); String psw = new String(jPassWord.getPassword()); if(account.equals("silver") && psw.equals("moon")){ jLabel3.setText("Login successful (。・ω・。)ノ♡"); }else{ jLabel3.setText("Account or password input error"); } } }); reset = new JButton(" R e s e t "); reset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { jUserName.setText(""); jPassWord.setText(""); } }); jp.add(jLabel1); jp.add(jUserName); jp.add(jLabel2); jp.add(jPassWord); jp.add(post); jp.add(reset); jp.add(jLabel3); jf.setContentPane(jp); jf.setVisible(true); } public static void main (String[] args){ new LoginUI(); }
}
这次作业和上周的作业有点像, 就只是加了个if, 比较简单, 就不加注释了