2011-04-08 82 views
1

我工作的一个任务,我需要在文本框输入SQL查询。用户可以按自定义的“执行查询”按钮,也可以按回车键。当使用其中任何一个时,它将触发一个ActionListener(不允许其他侦听器)。是否像写作一样简单:一个简单的关于问题的ActionListener与“ENTER”键

if (e.getSource()=='querybutton' || e.getSource=='enter') 

还是有没有比这更多?

正如我所说,这是一个简单的问题(我知道)。

编辑:

我会在我的actionPerformed写此位为:

 public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource()==gui.executeQueryButton || e.getSource()==gui.enter) 
     { 
     String query = gui.queryText.getText(); 

     //more code to follow 
     } 
    } 
+2

你应该这样做“someString” .equals代替(“someString”)“==” – 2011-04-08 04:20:30

回答

2

e.getSource()实际上返回负责触发事件(而不是创建该控件时,你所使用的变量的名称)的对象。在这种情况下,你的按钮。您原则上可以将e.getSource()与实际的按钮实例进行比较。然而,你是否真的将这个动作监听器添加到这两个按钮之外?大概你只需要把这个监听器添加到你想要这种行为的两个按钮 - 在这种情况下,你不需要检查这个if

2

“是写作一样简单:

if (e.getSource()=='querybutton' || e.getSource=='enter')" 

这不是简单的写这个,但而这是错误的写。

对于一个你不想用==比较字符串,另一方面,你没有用单引号声明字符串,第三,不是以这种方式获得回车键,而是通过向JTextField本身添加适当的ActionListener对象,最后应该有一个处理这个动作的ActionListener类,所以if块是完全不必要的。这可能最好用一个小的内部专用ActionListener类完成。然后,您将创建该类的一个对象,并将其添加为查询按钮和JTextField的ActionListener。

编辑1:

我的意思如下图所示,具有专用内部处理类演示类更完整的示例:

import java.awt.event.*; 
import javax.swing.*; 

public class ActionListenerEg extends JPanel { 
    private JButton queryButton = new JButton("Query"); 
    private JTextField textField = new JTextField("hello", 20); 


    public ActionListenerEg() { 
     QueryListener qListener = new QueryListener(); 
     queryButton.addActionListener(qListener); 
     textField.addActionListener(qListener); 

     add(queryButton); 
     add(textField); 
    } 

    private class QueryListener implements ActionListener { 
     public void actionPerformed(ActionEvent arg0) { 
     String textInField = textField.getText(); 
     System.out.println("Use text in field, \"" + textInField + "\" to call SQL query in a background SwingWorker thread."); 
     } 
    } 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("ActionListenerEg"); 
     frame.getContentPane().add(new ActionListenerEg()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 

的的ActionListener要么通过按下按钮激发或通过按下从JTextField中输入。然后,我会在我的控制类中使用在actinoPerformed方法内部调用的代码。

编辑2:在自己的Handler或Control类中拥有大多数处理程序或“控制”代码可能是一个好主意,但它不必实现ActionListener接口本身,而只需要将调用的代码来自ActionListener代码。例如,在这里我尝试将所有的处理程序代码放在它自己的类中。它会有不同的方法,被称为各种情况。例如,

import java.awt.Component; 
import java.awt.event.*; 

import javax.swing.*; 

public class ActionListenerEg extends JPanel { 
    private ActionListenerHandler handler; 

    private JButton queryButton = new JButton("Query"); 
    private JButton displayButton = new JButton("Display"); 
    private JTextField textField = new JTextField("hello", 20); 

    // pass in handler or handler 
    public ActionListenerEg(final ActionListenerHandler handler) { 
     this.handler = handler; 
     QueryListener qListener = new QueryListener(); 
     queryButton.addActionListener(qListener); 
     textField.addActionListener(qListener); 
     displayButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (handler != null) { 
       handler.displayActionPerformed(e); 
      } 
     } 
     }); 

     add(queryButton); 
     add(textField); 
     add(displayButton); 
    } 

    private class QueryListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
     if (handler != null) { 
      String textInField = textField.getText(); 
      handler.doQueryAction(e, textInField); 
     } 
     } 
    } 

    private static void createAndShowUI() { 
     ActionListenerHandler handler = new ActionListenerHandler(); 
     JFrame frame = new JFrame("ActionListenerEg"); 
     frame.getContentPane().add(new ActionListenerEg(handler)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 

class ActionListenerHandler { 

    public void displayActionPerformed(ActionEvent e) { 
     JOptionPane.showMessageDialog((Component) e.getSource(), "Display things!"); 

    } 

    public void doQueryAction(ActionEvent e, String textInField) { 
     String text = "We will use \"" + textInField + "\" to help create and run the SQL Query"; 
     JOptionPane.showMessageDialog((Component) e.getSource(), text); 
    } 

} 

如果问题很清楚,或者有任何问题,请提问。

+0

对不起,这个片段将在ActionListener的类。我在说,如果actionPerformed是'querybutton'或'enter'。它不会是一个字符串。道歉我没有更清楚。 – unit 2011-04-08 04:21:21

+2

你很清楚,并且因上述所有原因你仍然错。 ActionListener类应该只处理这个动作和这个动作,所以不需要if条件,无论它们是正确还是错误。 – 2011-04-08 04:22:53

+0

每当我问一个问题,我就开始认为我的教育严重缺乏。我们正在展示处理单独处理程序类中的所有事件,就像我在这里一样。 – unit 2011-04-08 04:26:12