java设置快捷键
比如有个JButton按钮,怎么在能给这个按钮设置一个快捷键呢?比如1,还有如何给JMenuItem设置快捷键呢?...
比如有个JButton按钮,怎么在能给这个按钮设置一个快捷键呢?比如1,
还有如何给JMenuItem设置快捷键呢? 展开
还有如何给JMenuItem设置快捷键呢? 展开
展开全部
JButton的setMnemonic()方法里面设置的值是要和alt一起按下去才会有效果.
还有中方法是用在JMenuItem中的,比如menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));这里参数的含义是当按下ctrl和s是所发生的动作,你这里替换成ALT_MASK就变成了alt + s,当然s也能换,把VK_S换成VK_3就变成了alt + 3了,这个比较强大我觉得
还有中方法是用在JMenuItem中的,比如menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));这里参数的含义是当按下ctrl和s是所发生的动作,你这里替换成ALT_MASK就变成了alt + s,当然s也能换,把VK_S换成VK_3就变成了alt + 3了,这个比较强大我觉得
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//希望对你有帮助
//方法1:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QuickButton extends JFrame{
int i;
public QuickButton(){
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
JPanel panel = new JPanel();
final JLabel label = new JLabel();
JButton quicka = new JButton("alt-a");
quicka.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i++;
label.setText("alt-a have been pressed"+"\n"+i+"times");
}
});
panel.add(quicka);
panel.add(label);
quicka.setMnemonic(KeyEvent.VK_A);
setVisible(true);
pack();
getContentPane().add(panel);
}
public static void main(String args args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton jbutton = new JButton("Nothing");
MnemonicButton button = new MnemonicButton("Press F1 here");
panel.add(jbutton);
panel.add(button);
frame.getContentPane().add(panel);
frame.setSize(200,200);
frame.setVisible(true);
}
}
//方法1:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QuickButton extends JFrame{
int i;
public QuickButton(){
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
JPanel panel = new JPanel();
final JLabel label = new JLabel();
JButton quicka = new JButton("alt-a");
quicka.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i++;
label.setText("alt-a have been pressed"+"\n"+i+"times");
}
});
panel.add(quicka);
panel.add(label);
quicka.setMnemonic(KeyEvent.VK_A);
setVisible(true);
pack();
getContentPane().add(panel);
}
public static void main(String args args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton jbutton = new JButton("Nothing");
MnemonicButton button = new MnemonicButton("Press F1 here");
panel.add(jbutton);
panel.add(button);
frame.getContentPane().add(panel);
frame.setSize(200,200);
frame.setVisible(true);
}
}
参考资料: http://topic.csdn.net/t/20010413/08/96187.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询