【问题标题】:Run JButton ActionListener in different class在不同的类中运行 JButton ActionListener
【发布时间】:2017-11-27 18:57:12
【问题描述】:

我正在尝试在与我的 GUI 不同的类中运行我的 JButton ActionListener。所以我让按钮工作,但我不能“访问”文本字段,当我想访问它们时它们只是“空的”。

这就是我的 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);
    }
}

这就是我的 Listener 类:

    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{
        }
    }
}

当我按下按钮时,即使我在 textField 中写了一些东西,我的程序也会退出。

我阅读了很多其他帖子,但无法正常工作。

感谢每一个回答。

【问题讨论】:

    标签: java swing jbutton actionlistener jtextfield


    【解决方案1】:

    您似乎以错误的顺序传递参数(据我所知,根据您的命名约定)

    +---------------------------+--------------------------+
    | 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              |
    +---------------------------+--------------------------+
    

    这意味着,当您在 ActionListener 中检查 txt_input 的值时,您实际上是在查看 number_input 值...这让我感到尖叫问题

    我的“一般”建议是创建一个interface,它提供 getter 和 setter,其名称比“text”和“text2”更好,因此您可以更好地区分它们的含义。我会让CaeserGUI 实现这个interface,然后将它传递给ActionListener,它只引用了一个interface 的实例

    避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

    如果您认为我反应过度,那么当我在 PC 上运行您的代码时会发生这种情况

    看看第二个字段中的“插入符号”是如何被破坏的?

    我建议使用JLabels 为字段提供提示,而不是设置它们的文本,因为这只会造成混淆。详情请见How to Use Labels

    像JTextPane 这样的组件真正受益于被包裹在JScrollPane 中,这将允许用户在文本扩展超出组件的视觉边界时看到文本。详情请见How to Use Scroll Panes

    【讨论】:

    • ty,是的,我把它们传错了...... D:但我也会尝试界面:D
    • 这不会是你最后一次混淆参数:P
    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多