java里選項菜單如何構(gòu)建

在Java中構(gòu)建選項菜單,通常有幾種方式,包括使用Swing庫中的`JMenuBar`、`JMenu`和`JMenuItem`類。以下是一個簡單的示例,展示了如何使用S...
在Java中構(gòu)建選項菜單,通常有幾種方式,包括使用Swing庫中的`JMenuBar`、`JMenu`和`JMenuItem`類。以下是一個簡單的示例,展示了如何使用Swing來構(gòu)建一個包含幾個選項的菜單:
```java
import javax.swing.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MenuExample {
public static void main(String[] args) {
// 創(chuàng)建 JFrame 實例
JFrame frame = new JFrame("菜單示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
// 創(chuàng)建菜單欄
JMenuBar menuBar = new JMenuBar();
// 創(chuàng)建菜單
JMenu fileMenu = new JMenu("文件");
// 創(chuàng)建菜單項
JMenuItem openItem = new JMenuItem("打開");
JMenuItem saveItem = new JMenuItem("保存");
JMenuItem exitItem = new JMenuItem("退出");
// 添加動作監(jiān)聽器
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "打開文件");
本文鏈接:http://www.resource-tj.com/bian/349577.html
上一篇:宣威東升中學(xué)怎么樣