JAVA 为JMenuItem添加快捷键
importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;importjav...
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
//简单的文本编辑器
public class EditorDemo extends JFrame {
JTextPane textPane = new JTextPane(); //文本窗格,编辑窗口
JLabel statusBar = new JLabel(); //状态栏
JFileChooser filechooser = new JFileChooser(); //文件选择器
public EditorDemo() { //构造函数
super("简单的文本编辑器"); //调用父类构造函数
Action[] actions = //Action数组,各种操作命令
{
new NewAction(),
new OpenAction(),
new SaveAction(),
new CutAction(),
new CopyAction(),
new PasteAction(),
new AboutAction(),
new ExitAction()};
setJMenuBar(createJMenuBar(actions)); //设置菜单栏
Container container = getContentPane(); //得到容器
container.add(createJToolBar(actions), BorderLayout.NORTH); //增加工具栏
container.add(textPane, BorderLayout.CENTER); //增加文本窗格
container.add(statusBar, BorderLayout.SOUTH); //增加状态栏
setSize(330, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
private JMenuBar createJMenuBar(Action[] actions) { //创建菜单栏
JMenuBar menubar = new JMenuBar(); //实例化菜单栏
JMenu menuFile = new JMenu("文件"); //实例化菜单
JMenu menuEdit = new JMenu("编辑");
JMenu menuAbout = new JMenu("帮助");
menuFile.add(new JMenuItem(actions[0])); //增加新菜单项
menuFile.add(new JMenuItem(actions[1]));
menuFile.add(new JMenuItem(actions[2]));
menuFile.add(new JMenuItem(actions[7]));
menuEdit.add(new JMenuItem(actions[3]));
menuEdit.add(new JMenuItem(actions[4]));
menuEdit.add(new JMenuItem(actions[5]));
menuAbout.add(new JMenuItem(actions[6]));
menubar.add(menuFile); //增加菜单
menubar.add(menuEdit);
menubar.add(menuAbout);
return menubar; //返回菜单栏
}
private JToolBar createJToolBar(Action[] actions) { //创建工具条
JToolBar toolBar = new JToolBar(); //实例化工具条
for (int i = 0; i < actions.length; i++) {
JButton bt = new JButton(actions[i]); //实例化新的按钮
bt.setRequestFocusEnabled(false); //设置不需要焦点
toolBar.add(bt); //增加按钮到工具栏
}
return toolBar; //返回工具栏
}
class NewAction extends AbstractAction { //新建文件命令
public NewAction() {
super("新建");
}
public void actionPerformed(ActionEvent e) {
textPane.setDocument(new DefaultStyledDocument()); //清空文档
}
}
public static void main(String[] args) {
new EditorDemo();
}
}
怎么给上面的MenuItem 添加快捷键 展开
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
//简单的文本编辑器
public class EditorDemo extends JFrame {
JTextPane textPane = new JTextPane(); //文本窗格,编辑窗口
JLabel statusBar = new JLabel(); //状态栏
JFileChooser filechooser = new JFileChooser(); //文件选择器
public EditorDemo() { //构造函数
super("简单的文本编辑器"); //调用父类构造函数
Action[] actions = //Action数组,各种操作命令
{
new NewAction(),
new OpenAction(),
new SaveAction(),
new CutAction(),
new CopyAction(),
new PasteAction(),
new AboutAction(),
new ExitAction()};
setJMenuBar(createJMenuBar(actions)); //设置菜单栏
Container container = getContentPane(); //得到容器
container.add(createJToolBar(actions), BorderLayout.NORTH); //增加工具栏
container.add(textPane, BorderLayout.CENTER); //增加文本窗格
container.add(statusBar, BorderLayout.SOUTH); //增加状态栏
setSize(330, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
private JMenuBar createJMenuBar(Action[] actions) { //创建菜单栏
JMenuBar menubar = new JMenuBar(); //实例化菜单栏
JMenu menuFile = new JMenu("文件"); //实例化菜单
JMenu menuEdit = new JMenu("编辑");
JMenu menuAbout = new JMenu("帮助");
menuFile.add(new JMenuItem(actions[0])); //增加新菜单项
menuFile.add(new JMenuItem(actions[1]));
menuFile.add(new JMenuItem(actions[2]));
menuFile.add(new JMenuItem(actions[7]));
menuEdit.add(new JMenuItem(actions[3]));
menuEdit.add(new JMenuItem(actions[4]));
menuEdit.add(new JMenuItem(actions[5]));
menuAbout.add(new JMenuItem(actions[6]));
menubar.add(menuFile); //增加菜单
menubar.add(menuEdit);
menubar.add(menuAbout);
return menubar; //返回菜单栏
}
private JToolBar createJToolBar(Action[] actions) { //创建工具条
JToolBar toolBar = new JToolBar(); //实例化工具条
for (int i = 0; i < actions.length; i++) {
JButton bt = new JButton(actions[i]); //实例化新的按钮
bt.setRequestFocusEnabled(false); //设置不需要焦点
toolBar.add(bt); //增加按钮到工具栏
}
return toolBar; //返回工具栏
}
class NewAction extends AbstractAction { //新建文件命令
public NewAction() {
super("新建");
}
public void actionPerformed(ActionEvent e) {
textPane.setDocument(new DefaultStyledDocument()); //清空文档
}
}
public static void main(String[] args) {
new EditorDemo();
}
}
怎么给上面的MenuItem 添加快捷键 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询