本帖最后由 sd110572 于 2013-12-19 18:13 编辑
- package demo;
- import java.awt.Button;
- import java.awt.EventQueue;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.JFrame;
- public class abc {
- private JFrame frame;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- abc window = new abc();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public abc() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new FlowLayout());
- Button b = new Button("我是按钮");
- frame.add(b);
- b.addKeyListener(new KeyAdapter() {
- //按下键盘
- public void keyPressed(KeyEvent e) {
- System.out.println("按下键盘");
- }
- });
- b.addActionListener(new ActionListener() {
- //按钮被触发
- public void actionPerformed(ActionEvent arg0) {
- System.out.println("触发按钮");
- }
- });
- }
- }
复制代码
我按下空格,按钮就会触发。可是我不想让空格操作这个按钮。
我想设置字母W键来触发, 用过JButton来设置了,可是空格照样能触发这个按钮。
2个问题,怎么让某个键触发这个Button,怎么取消空格触发Button |