当前位置: 技术问答>java相关
如何设置JButton的快捷键???
来源: 互联网 发布时间:2014-12-29
本文导语: 如何为JButton设置快捷键?象CTRL+A等。 若不能设置,KeyListener放什么地方?? 请高手指点!! | //希望对你有帮助 //方法1: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class QuickB...
如何为JButton设置快捷键?象CTRL+A等。
若不能设置,KeyListener放什么地方??
请高手指点!!
若不能设置,KeyListener放什么地方??
请高手指点!!
|
//希望对你有帮助
//方法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[]) {
new QuickButton().show();
}
}
//方法2:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MnemonicButton extends JButton {
public MnemonicButton(String string)
{
super(string);
ActionListener F1Listener = new ActionListener() { public void actionPerformed(ActionEvent evt)
{
System.out.println("F1 key pressed while button focused");
}};
KeyStroke strokeF1 = KeyStroke.getKeyStroke(KeyEvent.VK_F1,0,false);
registerKeyboardAction(F1Listener, strokeF1, JComponent.WHEN_FOCUSED);
}
public static void main(String[] 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[]) {
new QuickButton().show();
}
}
//方法2:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MnemonicButton extends JButton {
public MnemonicButton(String string)
{
super(string);
ActionListener F1Listener = new ActionListener() { public void actionPerformed(ActionEvent evt)
{
System.out.println("F1 key pressed while button focused");
}};
KeyStroke strokeF1 = KeyStroke.getKeyStroke(KeyEvent.VK_F1,0,false);
registerKeyboardAction(F1Listener, strokeF1, JComponent.WHEN_FOCUSED);
}
public static void main(String[] 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);
}
}