请帮我用java编写出一个笔记本界面
时间: 2024-02-29 11:52:15 浏览: 20
好的,我可以给您提供一个简单的Java Swing代码,用于创建一个基本的笔记本界面。以下是代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class NotebookUI extends JFrame {
private static final long serialVersionUID = 1L;
// Constructor
public NotebookUI() {
setTitle("Notebook");
setSize(600, 400); // set the size of the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit program when frame is closed
setLocationRelativeTo(null); // center the frame on the screen
setLayout(new BorderLayout()); // set the layout
// Create the top panel
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.LIGHT_GRAY);
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
JLabel titleLabel = new JLabel("Notebook");
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
topPanel.add(titleLabel);
add(topPanel, BorderLayout.NORTH);
// Create the center panel
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridBagLayout());
centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.WEST;
JLabel nameLabel = new JLabel("Name:");
nameLabel.setFont(new Font("Arial", Font.BOLD, 14));
centerPanel.add(nameLabel, gbc);
gbc.gridx++;
JTextField nameField = new JTextField(20);
nameField.setFont(new Font("Arial", Font.PLAIN, 14));
centerPanel.add(nameField, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel descriptionLabel = new JLabel("Description:");
descriptionLabel.setFont(new Font("Arial", Font.BOLD, 14));
centerPanel.add(descriptionLabel, gbc);
gbc.gridx++;
JTextArea descriptionTextArea = new JTextArea(5, 20);
descriptionTextArea.setFont(new Font("Arial", Font.PLAIN, 14));
descriptionTextArea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
centerPanel.add(descriptionTextArea, gbc);
add(centerPanel, BorderLayout.CENTER);
// Create the bottom panel
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 2, 10, 10));
JButton cancelButton = new JButton("Cancel");
cancelButton.setFont(new Font("Arial", Font.PLAIN, 14));
cancelButton.setPreferredSize(new Dimension(100, 30));
cancelButton.addActionListener(e -> System.exit(0)); // close the program when cancel button is clicked
bottomPanel.add(cancelButton);
JButton saveButton = new JButton("Save");
saveButton.setFont(new Font("Arial", Font.PLAIN, 14));
saveButton.setPreferredSize(new Dimension(100, 30));
saveButton.addActionListener(e -> {
// save the note
String name = nameField.getText();
String description = descriptionTextArea.getText();
System.out.println("Name: " + name);
System.out.println("Description: " + description);
// clear the fields
nameField.setText("");
descriptionTextArea.setText("");
});
bottomPanel.add(saveButton);
add(bottomPanel, BorderLayout.SOUTH);
// Set the frame visible
setVisible(true);
}
// Main method
public static void main(String[] args) {
// Set the look and feel to the system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// Create an instance of the NotebookUI class
new NotebookUI();
}
}
```
这个界面包括一个标题 ("Notebook"), 一个表单,包含一个笔记名称文本框和一个笔记描述文本框,并带有一个取消按钮和一个保存按钮。当点击保存按钮时,它将读取笔记名称和笔记描述文本框中的值并将它们打印到控制台。