📄 jmenudemo.java
字号:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JMenuDemo extends JFrame
{
private JTextArea editor;
private JMenuBar menuBar;
private JMenu fileMenu, editMenu, helpMenu, helpsubjectMenu;
private JMenuItem fileMenuLoad, fileMenuSave, fileMenuExit, editMenuCut,
editMenuCopy, editMenuPaste, helpsubjectMenuNote,
helpsubjectMenuCal;
public JMenuDemo()
{
super("菜单栏");
setSize(400,300);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) { System.err.println("Can't set look and feel: "+e);}
//获取内容面板
Container container = getContentPane();
//创建菜单栏
menuBar=new JMenuBar();
menuBar.setBackground(Color.cyan);
addFileMenu();
addEditMenu();
addHelpMenu();
setJMenuBar(menuBar);
editor = new JTextArea();
container.add(new JScrollPane(editor));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//创建文件菜单
private void addFileMenu()
{
fileMenu=new JMenu("文件(F)");
fileMenu.setBackground(Color.cyan); //设置菜单背景颜色
fileMenu.setForeground(Color.black);
Font f=new Font("sanserif",Font.PLAIN,12);
fileMenu.setFont(f); //设置菜单中文本的字体
//打开子菜单
fileMenuLoad=new JMenuItem("打开(O)... Ctrl+O");
fileMenuLoad.setFont(f);
fileMenuLoad.setForeground(Color.black);
fileMenuLoad.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ loadFile(); }
});
fileMenu.add(fileMenuLoad);
//保存子菜单
fileMenuSave=new JMenuItem("保存(S)... Ctrl+S");
fileMenuSave.setFont(f);
fileMenuSave.setForeground(Color.black);
fileMenuSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ saveFile(); }
});
fileMenu.add(fileMenuSave); //向菜单JMenu组建对象中,加入菜单项JMenuItem的组建对象
fileMenu.addSeparator();
//退出子菜单
fileMenuExit=new JMenuItem("退出(X)");
fileMenuExit.setFont(f);
fileMenuExit.setForeground(Color.black);
fileMenuExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ System.exit(0); }
});
fileMenu.add(fileMenuExit); //向菜单JMenu组建对象中,加入菜单项JMenuItem组建对象
menuBar.add(fileMenu); //向菜单栏JMenuBar组建对象中,加入菜单JMenu组建对象
}
//创建编辑菜单
private void addEditMenu()
{
editMenu=new JMenu("编辑(E)");
Font f=new Font("sanserif",Font.PLAIN,12);
editMenu.setFont(f);
editMenu.setBackground(Color.cyan);
editMenu.setForeground(Color.black);
//剪切子菜单
editMenuCut=new JMenuItem("剪切(T) Ctrl+X");
editMenuCut.setFont(f);
editMenuCut.setForeground(Color.black);
editMenuCut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ cut(); }
});
editMenu.add(editMenuCut);
//复制子菜单
editMenuCopy=new JMenuItem("复制(C) Ctrl+C");
editMenuCopy.setFont(f);
editMenuCopy.setForeground(Color.black);
editMenuCopy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ copy();}
});
editMenu.add(editMenuCopy);
//粘贴子菜单
editMenu.addSeparator();
editMenuPaste=new JMenuItem("粘贴(P) Ctrl+V");
editMenuPaste.setFont(f);
editMenuPaste.setForeground(Color.black);
editMenuPaste.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ paste(); }
});
editMenu.add(editMenuPaste);
menuBar.add(editMenu);
}
//创建帮助菜单
private void addHelpMenu()
{
helpMenu=new JMenu("帮助(H)");
Font f=new Font("sanserif",Font.PLAIN,12);
helpMenu.setFont(f);
helpMenu.setBackground(Color.cyan);
helpMenu.setForeground(Color.black);
helpsubjectMenu=new JMenu("帮助主题(H) Ctrl+H");
helpsubjectMenu.setFont(f);
helpsubjectMenu.setBackground(Color.cyan);
helpsubjectMenu.setForeground(Color.black);
helpsubjectMenuNote=new JMenuItem("关于记事本(A) Ctrl+A");
helpsubjectMenuNote.setFont(f);
helpsubjectMenuNote.setForeground(Color.black);
helpsubjectMenuCal=new JMenuItem("关于计算器(C) Ctrl+R");
helpsubjectMenuCal.setFont(f);
helpsubjectMenuCal.setForeground(Color.black);
helpsubjectMenu.add(helpsubjectMenuNote);
helpsubjectMenu.addSeparator();
helpsubjectMenu.add(helpsubjectMenuCal);
helpMenu.add(helpsubjectMenu);
menuBar.add(helpMenu);
}
//打开文件
private void loadFile() {
JFileChooser fc=new JFileChooser();
int returnVal=fc.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{
File file=fc.getSelectedFile();
try
{
editor.read(new FileReader(file),null);
}catch(IOException exp){}
}
}
//保存文件
private void saveFile()
{
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{
File file=fc.getSelectedFile();
try
{
editor.write(new FileWriter(file));
}catch(IOException exp){}
}
}
//实现复制功能
private void copy()
{
editor.copy();
editor.requestFocus();
}
//实现粘贴功能
private void paste()
{
editor.paste();
editor.requestFocus();
}
//实现剪切功能
private void cut()
{
editor.cut();
editor.requestFocus();
}
public static void main(String[] args)
{
JMenuDemo application = new JMenuDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -