日期:2014-05-20 浏览次数:20959 次
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){ 触发事件 } }
------解决方案--------------------
设置快捷键
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 stub new MnemonicButton(); } }
------解决方案--------------------
JB.setMnemonic(java.awt.event.KeyEvent.VK_F1);
------解决方案--------------------