JAVA登錄界面可以用于學(xué)生和老師的登錄,這對于學(xué)校來說非常實用。下面將使用JAVA代碼實現(xiàn)該功能。
import java.awt.Color; import java.awt.Font; 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; public class LoginFrame extends JFrame implements ActionListener{ private JPanel panel; private JLabel userLabel, passwordLabel, msgLabel; private JTextField userText; private JPasswordField passwordText; private JButton loginButton; public LoginFrame() { initComponents(); } private void initComponents() { panel = new JPanel(); panel.setLayout(null); panel.setBackground(Color.LIGHT_GRAY); userLabel = new JLabel("用戶名:"); userLabel.setBounds(50, 50, 80, 25); panel.add(userLabel); userText = new JTextField(20); userText.setBounds(140, 50, 165, 25); panel.add(userText); passwordLabel = new JLabel("密 碼:"); passwordLabel.setBounds(50, 100, 80, 25); panel.add(passwordLabel); passwordText = new JPasswordField(20); passwordText.setBounds(140, 100, 165, 25); panel.add(passwordText); loginButton = new JButton("登錄"); loginButton.setBounds(170, 150, 80, 25); loginButton.addActionListener(this); panel.add(loginButton); msgLabel = new JLabel(""); msgLabel.setBounds(55, 200, 250, 30); msgLabel.setFont(new Font("Serif", Font.BOLD, 16)); panel.add(msgLabel); this.add(panel); this.setTitle("登錄界面"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { String userName = userText.getText(); String password = new String(passwordText.getPassword()); if (userName.equals("teacher") && password.equals("123")) { JOptionPane.showMessageDialog(this, "歡迎老師登錄!"); } else if (userName.equals("student") && password.equals("123")) { JOptionPane.showMessageDialog(this, "歡迎學(xué)生登錄!"); } else { msgLabel.setText("用戶名或密碼錯誤,請重新輸入!"); } } public static void main(String[] args) { new LoginFrame().setVisible(true); } }
在上面的代碼中,我們定義了一個LoginFrame類,這個類繼承了JFrame類,并實現(xiàn)了ActionListener接口。我們在initComponents()方法中定義了界面,并在提交按鈕上注冊了一個ActionListener監(jiān)聽器。當(dāng)用戶點擊按鈕時,執(zhí)行actionPerformed()方法,檢查用戶名和密碼是否正確,并彈出相應(yīng)的消息框。根據(jù)用戶名是“teacher”還是“student”來區(qū)分老師和學(xué)生登錄。
以上就是使用JAVA實現(xiàn)學(xué)生和老師登錄界面的示例代碼了。該功能可以在學(xué)校的網(wǎng)絡(luò)系統(tǒng)中廣泛應(yīng)用。