在java程序中,怎样将一个按键的热键(快捷键)设置为F1到F12 ?
谢谢了
------解决方案--------------------
- Java code
JButton JB = new JButton(); JB.setMnemonic(java.awt.event.KeyEvent.VK_F1);、JB.setVisiable(false);JB.addActionListener(this);public void actionPerformed(ActionEvent e){ if(e.getSource() == b){ 触发事件 } }
------解决方案--------------------
设置快捷键
- Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MnemonicButton implements ActionListener{ public MnemonicButton(){ JFrame f=new JFrame("MnemonicButton"); Container contentPane=f.getContentPane(); JButton b=new JButton("Open new window"); b.setMnemonic('o'); b.addActionListener(this); contentPane.add(b); f.pack(); f.show(); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } public void actionPerformed(ActionEvent e){ JFrame newF=new JFrame("新窗口"); JLabel label=new JLabel("这是新窗口"); label.setHorizontalAlignment(JLabel.CENTER); newF.getContentPane().add(label); newF.setSize(100,100); newF.show(); } public static void main(String[] args) {// TODO Auto-generated method stubnew MnemonicButton();}}
------解决方案--------------------
- Java code
JB.setMnemonic(java.awt.event.KeyEvent.VK_F1);
------解决方案--------------------