简体   繁体   English

在不同的类中运行JButton ActionListener

[英]Run JButton ActionListener in different class

I'm trying to run my JButton ActionListener in a different class than my GUI. 我正在尝试在与GUI不同的类中运行JButton ActionListener。 So I get the button working, but I can't "access" the TextFields, they are just "empty" when I want to access them. 这样我就可以使用按钮了,但是我无法“访问” TextField,当我想访问它们时它们只是“空”的。

So that's my GUI Class: 这就是我的GUI类:

public class CaeserGUI extends JFrame {

    JPanel contentPane;
    private JTextField txt_input;
    private JTextField number_input;
    private JButton btn_translate;
    private JTextArea txt_output;
    private JTextField txt_input2;
    private JTextField number_input2;
    private JTextArea txt_output2;

    /**
     * Launch the application.
     * Author: Paul Viehmann
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CaeserGUI frame = new CaeserGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CaeserGUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        txt_input = new JTextField();
        txt_input.setBounds(10, 38, 363, 20);
        contentPane.add(txt_input);
        txt_input.setColumns(10);

        number_input = new JTextField();
        number_input.setBounds(383, 38, 41, 20);
        contentPane.add(number_input);
        number_input.setColumns(10);

        JTextPane txtpnZubersetzenderSatz = new JTextPane();
        txtpnZubersetzenderSatz.setText("Zu \u00FCbersetzender Satz:");
        txtpnZubersetzenderSatz.setBounds(10, 11, 126, 20);
        contentPane.add(txtpnZubersetzenderSatz);

        JTextPane txtpnVerschiebung = new JTextPane();
        txtpnVerschiebung.setText("Verschiebung:");
        txtpnVerschiebung.setBounds(349, 11, 75, 20);
        contentPane.add(txtpnVerschiebung);

        txt_output = new JTextArea();
        txt_output.setBounds(10, 69, 414, 40);
        contentPane.add(txt_output);

        JTextPane txtpnZuDechiffrierenderSatz = new JTextPane();
        txtpnZuDechiffrierenderSatz.setText("Zu dechiffrierender Satz:");
        txtpnZuDechiffrierenderSatz.setBounds(10, 120, 126, 20);
        contentPane.add(txtpnZuDechiffrierenderSatz);

        txt_input2 = new JTextField();
        txt_input2.setColumns(10);
        txt_input2.setBounds(10, 147, 363, 20);
        contentPane.add(txt_input2);

        number_input2 = new JTextField();
        number_input2.setColumns(10);
        number_input2.setBounds(383, 147, 41, 20);
        contentPane.add(number_input2);

        JTextPane textPane_1 = new JTextPane();
        textPane_1.setText("Verschiebung:");
        textPane_1.setBounds(349, 120, 75, 20);
        contentPane.add(textPane_1);

        txt_output2 = new JTextArea();
        txt_output2.setBounds(10, 176, 414, 40);
        contentPane.add(txt_output2);

        ActionListener actionListener = new ButtonListener(number_input, number_input2, btn_translate, txt_output, txt_input, txt_input2, txt_output2);

        btn_translate = new JButton("\u00DCbersetzen");
        btn_translate.addActionListener(actionListener);
        btn_translate.setBounds(10, 227, 414, 23);
        contentPane.add(btn_translate);
    }
}

And that's my Listener class: 那是我的侦听器类:

    public class ButtonListener implements ActionListener{

    private JTextField txt_input;
    private JTextField number_input;
    private JButton btn_translate;
    private JTextArea txt_output;
    private JTextField txt_input2;
    private JTextField number_input2;
    private JTextArea txt_output2;

    public ButtonListener(JTextField txt_input, JTextField number_input,
                            JButton btn_translate, JTextArea txt_output, 
                            JTextField txt_input2, JTextField number_input2, 
                            JTextArea txt_output2){

        this.txt_input = txt_input;
        this.number_input = number_input;
        this.btn_translate = btn_translate;
        this.txt_output = txt_output;
        this.txt_input2 = txt_input2;
        this.number_input2 = number_input2;
        this.txt_output2 = txt_output2;
    }

    public void actionPerformed(ActionEvent e){
        if(txt_input.getText().equals("")){
            System.exit(0);
        }else{
        }
    }
}

When I press the button, my program exit even when I write something in the textField. 当我按下按钮时,即使我在textField中写了一些东西,我的程序也会退出。

I read a lot of other posts, but couldn't get it to work. 我阅读了许多其他文章,但无法正常工作。

I appreciate every answer. 我感谢每一个答案。

You seem to be passing the parameters in the incorrect order (as far as I can tell, based on your naming conventions) 您似乎在以错误的顺序传递参数(据我所知,根据您的命名约定)

+---------------------------+--------------------------+
| ActionListener Parameters | The Order You're Passing |
+---------------------------+--------------------------+
| txt_input                 | number_input             |
| number_input              | number_input2            |
| btn_translate             | btn_translate            |
| txt_output                | txt_output               |
| txt_input2                | txt_input                |
| number_input2             | txt_input2               |
| txt_output2               | txt_output2              |
+---------------------------+--------------------------+

This means, when you inspect the value of txt_input in your ActionListener , you're actually looking at the number_input value ... this allow screams issues to me 这意味着,当您在ActionListener检查txt_input的值时,实际上是在查看number_input值...这number_input我带来尖叫声

My "general" recommendation would be to create an interface which provide getters and setters, with better names than "text" and "text2", so you can better differentiate their meaning. 我的“一般”建议是创建一个提供getter和setter的interface ,其名称比“ text”和“ text2”更好,因此您可以更好地区分它们的含义。 I'd have CaeserGUI implement this interface and then pass it to the ActionListener , which took a single reference to an instance of said interface 我要让CaeserGUI实现此interface ,然后将其传递给ActionListener ,后者对所述interface的实例进行了单个引用

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

If you think I'm be over reactive, this is what happens when I run your code on my PC 如果您认为我反应过度,这就是在PC上运行代码时发生的情况

布局不良

See how the "caret" in the second field is broken? 看看第二个字段中的“插入符号”是如何断开的?

I would recommend using JLabel s to provide prompts to the fields instead of setting their text, because that's just confusing. 我建议使用JLabel来为字段提供提示,而不是设置其文本,因为这很令人困惑。 See How to Use Labels for more details 有关更多详细信息,请参见如何使用标签

Components like JTextPane really benefit from been wrapped in a JScrollPane , this will allow the user to see the text when it expands beyond the visual boundaries of the component. JTextPane这样的组件确实受益于包装在JScrollPane ,当文本扩展到组件的可视范围之外时,这将允许用户查看文本。 See How to Use Scroll Panes for more details 有关更多详细信息,请参见如何使用滚动窗格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM