此系列將記錄本人從開始到結束做物料管理系統的過程 登錄界面的設計 此博客將實現如下界面: 當用戶名或密碼沒輸入時將顯示相應的提示信息,採用java swing實現 代碼: ...
此系列將記錄本人從開始到結束做物料管理系統的過程
登錄界面的設計
此博客將實現如下界面:
當用戶名或密碼沒輸入時將顯示相應的提示信息,採用java swing實現
代碼:
import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JRadioButton; import java.awt.Color; public class login { private JFrame frame; private JTextField textField; private JPasswordField passwordField; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { login window = new login(); window.frame.setLocationRelativeTo(null); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public login() { initialize(); } private void initialize() { frame = new JFrame(); frame.setTitle("\u7269\u6599\u7BA1\u7406\u7CFB\u7EDF"); frame.setBounds(100, 100, 360, 260); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JLabel usernameLabel = new JLabel("\u7528\u6237\u540D\uFF1A"); usernameLabel.setFont(new Font("宋體", Font.PLAIN, 14)); usernameLabel.setBounds(90, 66, 72, 28); frame.getContentPane().add(usernameLabel); JLabel passwordLabel = new JLabel("\u5BC6 \u7801\uFF1A"); passwordLabel.setFont(new Font("宋體", Font.PLAIN, 14)); passwordLabel.setBounds(90, 104, 72, 33); frame.getContentPane().add(passwordLabel); textField = new JTextField(); textField.setForeground(Color.BLACK); textField.setBounds(147, 70, 110, 21); frame.getContentPane().add(textField); textField.setColumns(10); textField.addActionListener(new action()); passwordField = new JPasswordField(); passwordField.setBounds(147, 110, 110, 21); frame.getContentPane().add(passwordField); passwordField.addActionListener(new action()); JButton loginButton = new JButton("\u767B\u5F55"); loginButton.setBounds(90, 157, 72, 23); frame.getContentPane().add(loginButton); loginButton.addActionListener(new action()); ButtonGroup group = new ButtonGroup(); JRadioButton button1 = new JRadioButton("\u7BA1\u7406\u54581"); button1.setBounds(64, 18, 72, 23); frame.getContentPane().add(button1); group.add(button1); JRadioButton button2 = new JRadioButton("\u7BA1\u7406\u54582"); button2.setBounds(138, 18, 72, 23); frame.getContentPane().add(button2); group.add(button2); JRadioButton button3 = new JRadioButton("\u7BA1\u7406\u54582"); button3.setBounds(210, 18, 80, 23); frame.getContentPane().add(button3); group.add(button3); JLabel label = new JLabel(""); label.setBounds(267, 73, 54, 15); frame.getContentPane().add(label); JLabel label_1 = new JLabel(""); label_1.setBounds(267, 113, 54, 15); frame.getContentPane().add(label_1); } class action implements ActionListener{ @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e) { String buttonName = e.getActionCommand(); if(buttonName.equals("登錄")) { if(textField.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "賬號不能為空"); }else if(passwordField.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "密碼不能為空"); } } } } }