用java做一个图书馆ui界面
时间: 2023-07-18 17:11:33 浏览: 48
以下是一个简单的 Java 图书馆 UI 界面示例,包含登录窗口和图书管理窗口:
Library.java
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Library {
private JFrame loginFrame;
private JFrame libraryFrame;
public static void main(String[] args) {
Library library = new Library();
library.showLogin();
}
public void showLogin() {
loginFrame = new JFrame("Library Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setSize(400, 300);
loginFrame.setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10, 10, 10, 10);
JLabel usernameLabel = new JLabel("Username:");
panel.add(usernameLabel, c);
c.gridx = 1;
JTextField usernameField = new JTextField(20);
panel.add(usernameField, c);
c.gridx = 0;
c.gridy = 1;
JLabel passwordLabel = new JLabel("Password:");
panel.add(passwordLabel, c);
c.gridx = 1;
JPasswordField passwordField = new JPasswordField(20);
panel.add(passwordField, c);
c.gridx = 1;
c.gridy = 2;
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
// TODO: Check username and password with database
if (username.equals("admin") && password.equals("admin")) {
showLibrary();
loginFrame.dispose();
} else {
JOptionPane.showMessageDialog(loginFrame, "Incorrect username or password!");
}
}
});
panel.add(loginButton, c);
loginFrame.add(panel);
loginFrame.setVisible(true);
}
public void showLibrary() {
libraryFrame = new JFrame("Library Management");
libraryFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
libraryFrame.setSize(600, 400);
libraryFrame.setLocationRelativeTo(null);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel bookPanel = new JPanel(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.insets = new Insets(10, 10, 10, 10);
JLabel bookLabel = new JLabel("Book Title:");
bookPanel.add(bookLabel, c1);
c1.gridx = 1;
JTextField bookField = new JTextField(20);
bookPanel.add(bookField, c1);
c1.gridx = 0;
c1.gridy = 1;
JLabel authorLabel = new JLabel("Author:");
bookPanel.add(authorLabel, c1);
c1.gridx = 1;
JTextField authorField = new JTextField(20);
bookPanel.add(authorField, c1);
c1.gridx = 0;
c1.gridy = 2;
JLabel yearLabel = new JLabel("Year:");
bookPanel.add(yearLabel, c1);
c1.gridx = 1;
JTextField yearField = new JTextField(20);
bookPanel.add(yearField, c1);
c1.gridx = 1;
c1.gridy = 3;
JButton addButton = new JButton("Add Book");
bookPanel.add(addButton, c1);
tabbedPane.addTab("Books", bookPanel);
JPanel userPanel = new JPanel(new GridBagLayout());
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 0;
c2.gridy = 0;
c2.insets = new Insets(10, 10, 10, 10);
JLabel userLabel = new JLabel("Username:");
userPanel.add(userLabel, c2);
c2.gridx = 1;
JTextField userField = new JTextField(20);
userPanel.add(userField, c2);
c2.gridx = 1;
c2.gridy = 1;
JButton addUserButton = new JButton("Add User");
userPanel.add(addUserButton, c2);
tabbedPane.addTab("Users", userPanel);
libraryFrame.add(tabbedPane);
libraryFrame.setVisible(true);
}
}
```
在该示例中,我们使用 `JFrame`、`JPanel` 和 `JTabbedPane` 来创建 UI 界面。登录窗口包含用户名和密码输入框以及登录按钮;图书管理窗口包含两个选项卡,分别为图书和用户的管理。在图书管理选项卡中,我们使用 `JLabel` 和 `JTextField` 创建输入框,使用 `JButton` 创建添加图书按钮。在用户管理选项卡中,我们只创建了一个添加用户按钮。
请注意,该示例只是一个简单的示例,如果需要实现更复杂的图书馆管理系统,还需要进行更多的开发工作。