Java给JFrame窗口设置热键的方法实现
作者:盒子box 时间:2022-01-24 13:32:20
使用了简单的JFrame窗口,在窗口中添加菜单,菜单选项,点击鼠标右键出现右键菜单,用io流将输入文本域的内容保存。并设置热键alt+x为退出的热键
public class MyNotepad extends JFrame{
//菜单栏
private JMenuBar jmb = new JMenuBar();
//菜单
private JMenu jmFile = new JMenu("文件(F)");
private JMenu jmEdit = new JMenu("编辑(E)");
private JMenu jmFormat = new JMenu("格式(O)");
private JMenu jmFind = new JMenu("查看(V)");
private JMenu jmHelp = new JMenu("帮助(H)");
//菜单项
private JMenuItem jmiNew = new JMenuItem("新建");
private JMenuItem jmiOpen = new JMenuItem("打开");
private JMenuItem jmiSave = new JMenuItem("保存");
private JMenuItem jmiOther = new JMenuItem("另存为");
private JMenuItem jmiExit = new JMenuItem("退出(X)");
private JCheckBoxMenuItem jmiAutoLine = new JCheckBoxMenuItem("自动换行");
private JMenuItem jmiFont = new JMenuItem("字体");
private JFileChooser jfc = new JFileChooser();
private JTextArea jta = new JTextArea();
private JScrollPane jsp = new JScrollPane(jta);
//上下文菜单
private JPopupMenu jpm = new JPopupMenu();
private JMenuItem jmiCopy = new JMenuItem("复制");
private JMenuItem jmiPaste = new JMenuItem("粘贴");
public MyNotepad() {
this.setTitle("记事本");
this.setSize(600,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(jmb,BorderLayout.NORTH);
jmb.add(jmFile);
jmb.add(jmEdit);
jmb.add(jmFormat);
jmb.add(jmFind);
jmb.add(jmHelp);
jmFile.add(jmiNew);
jmFile.add(jmiOpen);
jmFile.add(jmiSave);
jmFile.add(jmiOther);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmFormat.add(jmiAutoLine);
jmFormat.add(jmiFont);
//上下文菜单
jpm.add(jmiCopy);
jpm.add(jmiPaste);
jpm.add(jmFile);
jta.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 3){
jpm.show(jta, e.getX(), e.getY());
}
}
});
//文本域自动换行
this.add(jsp,BorderLayout.CENTER);
jmiAutoLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean selected = jmiAutoLine.isSelected();
if(selected){
jta.setLineWrap(true);
}else{
jta.setLineWrap(false);
}
}
});
// 文件新建窗口
jmiNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int choose = jfc.showOpenDialog(MyNotepad.this);
switch (choose) {
case JFileChooser.APPROVE_OPTION:
System.out.println("点击了打开");
break;
case JFileChooser.CANCEL_OPTION:
System.out.println("点击了取消");
break;
}
}
});
//文件保存窗口,用随机数命名文件名随时保存进新的文件
jmiSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Random r = new Random();
int a = r.nextInt(100);
File file = new File("D:\\桌面\\b"+"r"+"a.txt");
try {
FileOutputStream fis = new FileOutputStream(file,true);
BufferedOutputStream bis = new BufferedOutputStream(fis);
byte [] b = jta.getText().getBytes();
try {
bis.write(b);
bis.flush();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
jmiExit.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.ALT_DOWN_MASK),
JComponent.WHEN_IN_FOCUSED_WINDOW);//1.设置监听事件。2.设置热键alt+x关闭窗口3.设置触发当鼠标聚焦在窗口时
jmiExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new MyNotepad();
}
}
效果如下:
来源:https://blog.csdn.net/weixin_45444295/article/details/107409217
标签:Java,JFrame,热键
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中实现可拖放图片剪裁入门教程
2022-04-23 12:11:03
SpringBoot和Swagger结合提高API开发效率
2023-11-25 01:23:16
Knife4j 3.0.3 整合SpringBoot 2.6.4的详细过程
2023-11-25 03:54:34
![](https://img.aspxhome.com/file/2023/9/59429_0s.png)
Spring5中的WebClient使用方法详解
2023-08-05 14:50:24
Spring-boot JMS 发送消息慢的解决方法
2023-02-06 07:50:54
Android 动态菜单实现实例代码
2023-05-19 16:24:58
![](https://img.aspxhome.com/file/2023/4/117094_0s.gif)
Java如何在沙箱环境中测试支付宝支付接口
2023-11-02 14:55:15
![](https://img.aspxhome.com/file/2023/5/58745_0s.png)
C#自定义处理xml数据类实例
2022-07-02 11:00:39
SeekBar拖动条的应用实例
2022-01-28 05:33:34
![](https://img.aspxhome.com/file/2023/2/106882_0s.jpg)
Android多媒体应用使用MediaPlayer播放音频
2023-07-08 15:37:52
![](https://img.aspxhome.com/file/2023/7/102607_0s.jpg)
c#生成验证码程序
2023-01-08 18:38:59
c# 配置文件App.config操作类库的方法
2023-01-19 10:34:37
WPF实现调用本机摄像头的示例代码
2023-03-15 15:40:24
![](https://img.aspxhome.com/file/2023/6/68076_0s.png)
Mybatis-Plus查询中如何排除标识字段
2023-11-23 20:38:46
![](https://img.aspxhome.com/file/2023/5/64555_0s.png)
Java设计模式中的桥接模式
2023-11-10 08:42:25
![](https://img.aspxhome.com/file/2023/8/113178_0s.png)
View触发机制API实现GestureDetector OverScroller详解
2023-01-24 00:57:08
Java实现扑克牌程序
2023-11-11 12:09:52
![](https://img.aspxhome.com/file/2023/0/59340_0s.jpg)
C#基础之数组排序、对象大小比较实现代码
2023-05-05 12:36:50
Spring超详细讲解面向对象到面向切面
2021-12-28 15:15:57
![](https://img.aspxhome.com/file/2023/2/89472_0s.png)
c# wpf使用GMap.NET类库,实现地图轨迹回放
2023-04-20 06:46:27
![](https://img.aspxhome.com/file/2023/8/88418_0s.gif)