我用的是eclipse3.1 swt-designer4.1 在做swing applet时,加了个JComboBox,在左边的事件中双击了keyPressed事件,但无论我按什么键都不触发此事件.  public void keyPressed(KeyEvent e) {
      System.out.println("asdf");
  }(这个JComboBox 的editable属性为true的)

解决方案 »

  1.   

    楼主把代码贴出来吧,可以copy调试
      

  2.   

    package dep.kil.tickets;import java.awt.event.InputMethodEvent;
    import java.awt.event.InputMethodListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.CompoundBorder;public class A_Tickets_sale extends JApplet { private JComboBox comboBox;
    private JTextField textField;
    public A_Tickets_sale() {
    super();
    getContentPane().setLayout(null); final JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setBounds(10, 5, 482, 85);
    getContentPane().add(panel); textField = new JTextField();
    textField.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e){
    System.out.println("asdfasdfasdf");
    }
    });
    textField.setBounds(6, 3, 63, 21);
    panel.add(textField); final JLabel label = new JLabel();
    label.setText("New JLabel");
    label.setBounds(100, 10, 75, 25);
    panel.add(label); final JButton button = new JButton();
    button.setText("New JButton");
    button.setBounds(35, 45, 90, 25);
    panel.add(button); comboBox = new JComboBox();
    comboBox.setSelectedIndex(0);
    comboBox.setBorder(new CompoundBorder(null, null));
    comboBox.addInputMethodListener(new InputMethodListener() {
    public void caretPositionChanged(InputMethodEvent e) {
    }
    public void inputMethodTextChanged(InputMethodEvent e) {
    System.out.println("asldkf");
    }
    });
    comboBox.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    System.out.println("asdf"); }
    public void keyTyped(KeyEvent e) {
    System.out.println("type");
    }
    public void keyReleased(KeyEvent e) {
    System.out.println("release");
    }
    });
    comboBox.setModel(new DefaultComboBoxModel(new String[] {"天河", "芳村", "海珠", "东山", "越秀"}));
    comboBox.setEditable(true);
    comboBox.setBounds(195, 10, 95, 30);
    panel.add(comboBox);
    //
    }}
      

  3.   

    comboBox.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    System.out.println("asdf"); }
    public void keyTyped(KeyEvent e) {
    System.out.println("type");
    }
    public void keyReleased(KeyEvent e) {
    System.out.println("release");
    }
    });
    里的事件全没反应
      

  4.   

    楼主,applet的初始化不需要构造函数
    applet有它自己的一些方法
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.CompoundBorder;public class A_Tickets_sale extends JApplet { private JComboBox comboBox; private JTextField textField; public void init() {
    getContentPane().setLayout(null); final JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setBounds(10, 5, 482, 85);
    getContentPane().add(panel); textField = new JTextField();
    textField.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    System.out.println("asdfasdfasdf");
    }
    });
    textField.setBounds(6, 3, 63, 21);
    panel.add(textField); final JLabel label = new JLabel();
    label.setText("New JLabel");
    label.setBounds(100, 10, 75, 25);
    panel.add(label); final JButton button = new JButton();
    button.setText("New JButton");
    button.setBounds(35, 45, 90, 25);
    panel.add(button); comboBox = new JComboBox();
    comboBox.setBorder(new CompoundBorder(null, null));
    comboBox.addInputMethodListener(new InputMethodListener() {
    public void caretPositionChanged(InputMethodEvent e) {
    } public void inputMethodTextChanged(InputMethodEvent e) {
    System.out.println("asldkf");
    }
    });
    comboBox.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    System.out.println("asdf"); } public void keyTyped(KeyEvent e) {
    System.out.println("type");
    } public void keyReleased(KeyEvent e) {
    System.out.println("release");
    }
    });
    comboBox.setModel(new DefaultComboBoxModel(new String[] { "天河", "芳村",
    "海珠", "东山", "越秀" }));
    comboBox.setEditable(true);
    comboBox.setBounds(195, 10, 95, 30);
    panel.add(comboBox);
    //
    }}
      

  5.   

    to believefym(feng):
      首先,它怎样初始化我没留意到.因为我是用eclipse+swt-designer新建一个JApplet出来,相信它建出来的APPLET的初始化方法不会有什么问题.
      另外,我把你帖出来的代放回去.当然,初始化方法是不同,但都一样能正确运行起来.但我之前所提出的combox监听不到任何键盘事件的问题还是没有解决.