java中的接口是类吗
290
2022-11-30
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("自动换行");http://
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);
jtahttp://.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();
}
}
效果如下:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~