0

我正在构建一个需要用户请求的应用程序。该应用程序的第一个版本我自己制作了一个输入窗口,但我发现对 showInputDialog 进行了更好的更改,因为它是 JOptionFrame 的预成型工具。现在我遇到了事件触发器的问题;查看下面的代码:

搜索屏幕:

public class SearchScreen extends EventSearch{
    ...
    public SearchScreen(){
        userQuery = (String) JOptionPane.showInputDialog("Type Keywords in english to be requested below:");    
    }
    ...
}

事件搜索:

public class EventSearch extends TabBuilder{

    public EventSearch() {

    }

    public void actionPerformed(ActionEvent Ev) {
        try {
            System.out.println("worked");
        } catch (IOException e1) {
            e1.printStackTrace(); //print failure
            JOptionPane.showMessageDialog(null, "FAIL");
        }
    };
}

选项卡生成器:

public class TabBuilder implements ActionListener {
    .....
}

然后我问,我应该如何通过 showInputDialog 调用事件?是否可以?谁来当听众?提前致谢

4

1 回答 1

0

我找到了自己的答案——它确实是继承事件搜索类的代码并将触发器拉到这样的一个动作,相反,最好这样做:

public SearchScreen(){

        userQuery = (String) JOptionPane.showInputDialog("Type Keywords in english to be requested below:");

            try {

                 //Your Action with the String  

            } catch (IOException e1) {

                e1.printStackTrace(); //print failure
                    JOptionPane.showMessageDialog(null, "FAILURE");
            }

    }
于 2013-07-15T10:04:36.000 回答