JMenuBar(菜单条),这一栏可以装菜单。

JMenu(菜单),只有菜单才能有分支

JMenuItem(菜单项目),最终选项

菜单和菜单条能用add中嵌套菜单,和菜单项目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import javax.swing.JFrame;

import java.awt.FlowLayout;

import javax.swing.*;

public class Menu extends JFrame {
//声明组件
JMenuBar m;
JMenu menu, subMenu, save_as,option;
JMenuItem item1, item2, save, desktop, document;


public Menu(){
init();
this.setLayout(new FlowLayout());
this.setTitle("Menu");
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
//新建组件
m=new JMenuBar();
menu=new JMenu("菜单");
subMenu=new JMenu("新建");
item1=new JMenuItem("文件");
item2=new JMenuItem("文件夹");
save=new JMenuItem("保存");
save_as=new JMenu("保存到");
desktop=new JMenuItem("桌面", new ImageIcon("a.jpg"));//加载图片
document=new JMenuItem("文档");
option=new JMenu("选项");
//添加组件
add(m); //添加菜单条
m.add(menu);//将菜单添加到菜单条中
menu.add(subMenu);//将菜单添加到菜单中,菜单里还有菜单(菜单才能出现分支)

subMenu.add(item1);//将菜单项目添加到菜单中
subMenu.add(item2);
menu.add(save);
menu.add(save_as);
save_as.add(desktop);
save_as.add(document);
m.add(option);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Menu m=new Menu();
}
}

  • 效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.*;

public class Menu extends JFrame implements ActionListener{
JMenuBar m;
JMenu m1,m2;
JMenuItem i1,i2,i3,i4,i5;
JTextArea t;
String name="file.txt";

Menu(){
init();
this.setTitle("菜单");
this.setSize(300,300);
this.setLayout(new FlowLayout());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

void init(){
m=new JMenuBar();
m1=new JMenu("新建");
m2=new JMenu("文件操作");
i1=new JMenuItem("文件");
i2=new JMenuItem("文件夹");
i3=new JMenuItem("写");
i4=new JMenuItem("读");
t=new JTextArea(5,20);
i5=new JMenuItem("删除");

add(m);
m.add(m1);
m.add(m2);
m1.add(i1);
m1.add(i2);
m2.add(i3);
m2.add(i4);
m2.add(i5);
add(t);


i1.addActionListener(this);
i2.addActionListener(this);
i3.addActionListener(this);
i4.addActionListener(this);
i5.addActionListener(this);

t.setEditable(false);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(i1)) {
File f=new File(name);
try {
if(f.createNewFile()) {
t.append("File created:" + f.getName()+"\n");
}else {
t.append("The file already exists\n");
}
}catch(IOException ee) {
ee.printStackTrace();
}
}
if(e.getSource().equals(i2)) {
File f=new File("New Floader");
if(f.mkdir()) {
t.append("Successfully make a directory\n");
}else {
t.append("The directory already exsits\n");
}
}
if(e.getSource().equals(i3)) {
try {
FileWriter f=new FileWriter(name);
f.write("Some text...");
f.close();
} catch (IOException e1) {
e1.printStackTrace();
}

}
if(e.getSource().equals(i4)) {
File f =new File(name);
try {
Scanner read=new Scanner(f);
while(read.hasNextLine()) {
String s=read.nextLine();
t.append(s);
}
read.close();

} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(e.getSource().equals(i5)) {
File f=new File(name);
if(f.delete()) {
t.append("Successfully delete the file\n");
}else {
t.append("Failed to delete the file\n");
}
}



}

public static void main(String[] args) {
// TODO Auto-generated method stub
Menu m =new Menu();
}
}