展开全部
在对象上注册一个监听
在监听处理程序中 设置快捷键
在监听处理程序中 设置快捷键
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
那你就要写一个监听的方法了,监听你的button,监听方法都继承自actionListener
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-03-30
展开全部
写了一个例子 你测试下吧import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JComponent;
import javax.swing.KeyStroke;public class TestJButton extends JFrame {
public TestJButton() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} private void jbInit() throws Exception {
jB1.setText("jButton1");
jB1.setMnemonic(KeyEvent.VK_F4);
jB1.addActionListener(new JbtAction());
jB1.registerKeyboardAction(new JbtAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
jTextField1.setText("jTextField1");
this.getContentPane().add(jB1, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jTextField1, java.awt.BorderLayout.NORTH);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setSize(200, 200);
this.setTitle("JButtonTest");
this.setVisible(true);
} JButton jB1 = new JButton();
JTextField jTextField1 = new JTextField();
public static void main(String[] args) {
new TestJButton();
}
}class JbtAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("按钮事件!!!");
}
}
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JComponent;
import javax.swing.KeyStroke;public class TestJButton extends JFrame {
public TestJButton() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} private void jbInit() throws Exception {
jB1.setText("jButton1");
jB1.setMnemonic(KeyEvent.VK_F4);
jB1.addActionListener(new JbtAction());
jB1.registerKeyboardAction(new JbtAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
jTextField1.setText("jTextField1");
this.getContentPane().add(jB1, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jTextField1, java.awt.BorderLayout.NORTH);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setSize(200, 200);
this.setTitle("JButtonTest");
this.setVisible(true);
} JButton jB1 = new JButton();
JTextField jTextField1 = new JTextField();
public static void main(String[] args) {
new TestJButton();
}
}class JbtAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("按钮事件!!!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询