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(); } }
效果如下:
到此这篇关于Java给JFrame窗口设置热键的方法实现的文章就介绍到这了,更多相关Java JFrame窗口热键内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)