用swing写一个公告板界面
时间: 2023-12-09 22:03:10 浏览: 34
好的,以下是一个简单的公告板界面的Swing代码示例:
```java
import javax.swing.*;
import java.awt.*;
public class BulletinBoard extends JFrame {
private JTextArea bulletinTextArea;
public BulletinBoard() {
super("公告板");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new BorderLayout());
// 公告板文本区域
bulletinTextArea = new JTextArea();
bulletinTextArea.setEditable(false);
// 滚动条
JScrollPane scrollPane = new JScrollPane(bulletinTextArea);
// 添加到主面板
add(scrollPane, BorderLayout.CENTER);
// 添加一个按钮,用于发布新公告
JButton publishButton = new JButton("发布新公告");
publishButton.addActionListener(e -> {
String newBulletin = JOptionPane.showInputDialog(this, "请输入新公告内容:");
if (newBulletin != null && !newBulletin.isEmpty()) {
bulletinTextArea.append(newBulletin + "\n");
bulletinTextArea.setCaretPosition(bulletinTextArea.getDocument().getLength());
}
});
add(publishButton, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new BulletinBoard();
}
}
```
这个界面包括一个文本区域和一个发布新公告按钮。当用户点击按钮时,会弹出一个对话框,让用户输入新的公告内容。然后,这个新公告会被添加到文本区域中。
希望对你有所帮助!