fork download
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. class LoginFrame extends JFrame {
  7. private JTextField usernameField;
  8. private JPasswordField passwordField;
  9. private JButton loginButton;
  10.  
  11. public LoginFrame() {
  12. setTitle("智慧校园登录");
  13. setSize(300, 200);
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. setLocationRelativeTo(null);
  16.  
  17. JPanel panel = new JPanel(new GridLayout(3, 2));
  18.  
  19. JLabel usernameLabel = new JLabel("用户名:");
  20. usernameField = new JTextField();
  21. JLabel passwordLabel = new JLabel("密码:");
  22. passwordField = new JPasswordField();
  23. loginButton = new JButton("登录");
  24.  
  25. panel.add(usernameLabel);
  26. panel.add(usernameField);
  27. panel.add(passwordLabel);
  28. panel.add(passwordField);
  29. panel.add(new JLabel());
  30. panel.add(loginButton);
  31.  
  32. loginButton.addActionListener(new ActionListener() {
  33. @Override
  34. public void actionPerformed(ActionEvent e) {
  35. String username = usernameField.getText();
  36. char[] passwordChars = passwordField.getPassword();
  37. String password = new String(passwordChars);
  38. // 这里可以添加真正的登录验证逻辑,比如和数据库比对等,暂时简单判断
  39. if ("admin".equals(username) && "123456".equals(password)) {
  40. JOptionPane.showMessageDialog(LoginFrame.this, "登录成功");
  41. // 登录成功后显示主界面,隐藏登录界面
  42. MainFrame mainFrame = new MainFrame();
  43. mainFrame.setVisible(true);
  44. setVisible(false);
  45. } else {
  46. JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误");
  47. }
  48. }
  49. });
  50.  
  51. add(panel);
  52. }
  53. }
  54.  
  55. class MainFrame extends JFrame {
  56. public MainFrame() {
  57. setTitle("智慧校园主界面");
  58. setSize(600, 400);
  59. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60. setLocationRelativeTo(null);
  61.  
  62. JMenuBar menuBar = new JMenuBar();
  63.  
  64. JMenu studentMenu = new JMenu("学生管理");
  65. JMenuItem addStudentItem = new JMenuItem("添加学生");
  66. JMenuItem viewStudentItem = new JMenuItem("查看学生");
  67. studentMenu.add(addStudentItem);
  68. studentMenu.add(viewStudentItem);
  69.  
  70. JMenu teacherMenu = new JMenu("教师管理");
  71. JMenuItem addTeacherItem = new JMenuItem("添加教师");
  72. JMenuItem viewTeacherItem = new JMenuItem("查看教师");
  73. teacherMenu.add(addTeacherItem);
  74. teacherMenu.add(viewTeacherItem);
  75.  
  76. menuBar.add(studentMenu);
  77. menuBar.add(teacherMenu);
  78.  
  79. setJMenuBar(menuBar);
  80.  
  81. // 主界面中间区域可以添加更多具体展示组件,比如表格等展示数据,暂留空白
  82. JPanel contentPanel = new JPanel();
  83. add(contentPanel);
  84. }
  85. }
  86. public class Main {
  87. public static void main(String[] args) {
  88. SwingUtilities.invokeLater(() -> {
  89. LoginFrame loginFrame = new LoginFrame();
  90. loginFrame.setVisible(true);
  91. });
  92. }
  93. }
Success #stdin #stdout #stderr 0.34s 67388KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:197)
	at java.desktop/java.awt.Window.<init>(Window.java:538)
	at java.desktop/java.awt.Frame.<init>(Frame.java:423)
	at java.desktop/java.awt.Frame.<init>(Frame.java:388)
	at java.desktop/javax.swing.JFrame.<init>(JFrame.java:180)
	at LoginFrame.<init>(Main.java:11)
	at Main.lambda$main$0(Main.java:89)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)