[英]JTextField ActionListener not responding to event
I am writing a java application which displays the contents of a SQLite database. 我正在编写一个显示SQLite数据库内容的Java应用程序。
I am trying to set up an ActionListener, so that the user can click on the field and the program will access the next table in the database. 我正在尝试设置一个ActionListener,以便用户可以单击该字段,该程序将访问数据库中的下一个表。
The problem is the listener is not responding to the click. 问题是听众没有对点击做出响应。
Blow is the method I am using to create the JTextField and set up the listener: Blow是我用来创建JTextField并设置侦听器的方法:
public void text(JTextField jtf, String a, int x, int y, int wid, int hei){
Font font = new Font("Courier", Font.BOLD,12);
gbc=new GridBagConstraints();
jtf=new JTextField(a);
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth = wid;
gbc.gridheight=hei;
gbc.fill=GridBagConstraints.BOTH;
add(jtf,gbc);
jtf.setEditable(false);
jtf.setHorizontalAlignment(JTextField.CENTER);
jtf.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jtf.setFont(font);
jtf.addActionListener(new ActionListener()
{ @Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"Pressed");
}
});
}
ActionListener executes when the user has focus on the control and presses ENTER. 当用户将焦点放在控件上并按Enter时,ActionListener将执行。
You should use a mouseListener in order to listen to events involving the mouse. 您应该使用mouseListener来侦听涉及鼠标的事件。
jtf.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e) {
JOptionPane.showMessageDialog(null,"Released");
}
public void mousePressed(MouseEvent e) {
JOptionPane.showMessageDialog(null,"Pressed");
}
}
You can also use a keyListener if you want to respond to the user changing the text. 如果要响应用户更改文本,也可以使用keyListener。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.