Java给JFrame窗口设置热键的方法实现
盒子box 人气:0本文着重讲解了Java给JFrame窗口设置热键的方法实现,文中通过代码实例以及图文讲解的非常细致,对大家的工作和学习具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
使用了简单的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(); } }
效果如下:
加载全部内容