【问题标题】:Nested Action Listener not working嵌套动作侦听器不起作用
【发布时间】:2018-11-26 13:47:36
【问题描述】:

我试图让按钮响应被按下并输出到 Java Swing 中的文本字段。一旦我执行代码,GUI 就会显示出来,但是它非常小,我必须扩展窗口。主要问题是监听器不工作。我不理解为什么。它是一个嵌套的 ActionListener。任何帮助将不胜感激。

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import java.awt.event.*;

public class MyAtm extends JPanel {
private final ButtonGroup buttonGroup = new ButtonGroup();
JTextField inputField;
JText text = null;
JButton withdrawalButton;
JButton depositButton;
JButton transferButton;
JButton balanceButton;
JRadioButton checkingAccountRadioButton;
JRadioButton savingAccountRadioButton;

/**
 * Create the panel.
 */
public MyAtm() {
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 173, 0, 0, 172, 0, 0, 0, 27, -21, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 36, 0, 0, 31, 30, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JButton withdrawalButton = new JButton("Withdraw");
    withdrawalButton.setName("Withdraw");
    //buttonGroup.add(withdrawalButton);
    GridBagConstraints gbc_withdrawalButton = new GridBagConstraints();
    gbc_withdrawalButton.fill = GridBagConstraints.BOTH;
    gbc_withdrawalButton.insets = new Insets(0, 0, 5, 5);
    gbc_withdrawalButton.gridx = 5;
    gbc_withdrawalButton.gridy = 2;
    add(withdrawalButton, gbc_withdrawalButton);

    JButton depositButton = new JButton("Deposit");
    GridBagConstraints gbc_depositButton = new GridBagConstraints();
    gbc_depositButton.fill = GridBagConstraints.BOTH;
    gbc_depositButton.insets = new Insets(0, 0, 5, 5);
    gbc_depositButton.gridx = 8;
    gbc_depositButton.gridy = 2;
    add(depositButton, gbc_depositButton);

    JButton transferButton = new JButton("Transfer");
    GridBagConstraints gbc_transferButton = new GridBagConstraints();
    gbc_transferButton.fill = GridBagConstraints.BOTH;
    gbc_transferButton.insets = new Insets(0, 0, 5, 5);
    gbc_transferButton.gridx = 5;
    gbc_transferButton.gridy = 5;
    add(transferButton, gbc_transferButton);

    JButton balanceButton = new JButton("Balance");
    GridBagConstraints gbc_balanceButton = new GridBagConstraints();
    gbc_balanceButton.fill = GridBagConstraints.BOTH;
    gbc_balanceButton.insets = new Insets(0, 0, 5, 5);
    gbc_balanceButton.gridx = 8;
    gbc_balanceButton.gridy = 5;
    add(balanceButton, gbc_balanceButton);

    JRadioButton checkingAccountRadioButton = new JRadioButton("Checking");
    buttonGroup.add(checkingAccountRadioButton);
    GridBagConstraints gbc_checkingAccountRadioButton = new GridBagConstraints();
    gbc_checkingAccountRadioButton.anchor = GridBagConstraints.WEST;
    gbc_checkingAccountRadioButton.fill = GridBagConstraints.VERTICAL;
    gbc_checkingAccountRadioButton.insets = new Insets(0, 0, 5, 5);
    gbc_checkingAccountRadioButton.gridx = 5;
    gbc_checkingAccountRadioButton.gridy = 7;
    add(checkingAccountRadioButton, gbc_checkingAccountRadioButton);

    JRadioButton savingAccountRadioButton = new JRadioButton("Saving");
    buttonGroup.add(savingAccountRadioButton);
    GridBagConstraints gbc_savingAccountRadioButton = new GridBagConstraints();
    gbc_savingAccountRadioButton.fill = GridBagConstraints.HORIZONTAL;
    gbc_savingAccountRadioButton.insets = new Insets(0, 0, 5, 5);
    gbc_savingAccountRadioButton.gridx = 8;
    gbc_savingAccountRadioButton.gridy = 7;
    add(savingAccountRadioButton, gbc_savingAccountRadioButton);

    JTextField inputField = new JTextField();
    inputField.setToolTipText("Enter Your amount");
    GridBagConstraints gbc_inputField = new GridBagConstraints();
    gbc_inputField.gridwidth = 6;
    gbc_inputField.insets = new Insets(0, 0, 5, 5);
    gbc_inputField.fill = GridBagConstraints.BOTH;
    gbc_inputField.gridx = 4;
    gbc_inputField.gridy = 8;
    add(inputField, gbc_inputField);
    //pack();
    //text = new JText();
    withdrawalButton.addActionListener(new JText());



}

private class JText implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            if(e.getSource() == (withdrawalButton)) {
                System.out.println(withdrawalButton.getName());
            }
        }catch(Exception ex) {
            System.out.println(ex);
        }
    }
}
public static void main(String[] args) {
    JFrame frame = new JFrame();
    MyAtm atm = new MyAtm();
    frame.getContentPane().add(atm);
    frame.setVisible(true);

}

}

【问题讨论】:

  • “GUI 会在我执行代码后显示,但是它非常小”frame.getContentPane().add(atm); frame.setVisible(true); 更改为frame.getContentPane().add(atm); frame.pack(); frame.setVisible(true);。对 pack 的调用将导致各种事情发生,包括将框架设置为显示atm 所需的完全正确的大小。在pack() 之后,我通常会立即调用frame.setMinimumSize(frame.getSize()); 以确保虽然它可以被拖得更大,但它永远不会被拖得太小。

标签: java swing actionlistener inner-classes anonymous-inner-class


【解决方案1】:
JButton withdrawalButton = new JButton("Withdraw");

去掉“JButton”,你要赋值给一个实例变量,而不是一个局部变量。

看你的action listener实现,如下:

private class JText implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (e.getSource() == (withdrawalButton)) {
                    System.out.println(withdrawalButton.getName());
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }

e.getSource() == (withdrawalButton) 检查将源与实例变量进行比较。

【讨论】:

  • 好收获,和我同时制作的。 1+
【解决方案2】:

您的问题是,您通过在类中声明它两次 来隐藏drawingButton 变量,一次在类中,它为null,然后再次在MyAtm 构造函数中。第一个从未分配对象,null 也是如此,而第二个是添加到 GUI 并被赋予 ActionListener 的对象。在您的侦听器中,您测试源是否等于第一个 null 变量。

解决方案:不要这样做,不要声明变量两次,而是一次,仅在类中。例如,改变这个:

JButton withdrawalButton = new JButton("Withdraw");

到这里:

withdrawalButton = new JButton("Withdraw");

更详细地说,这是您正在做的事情:

public class MyAtm extends JPanel {

    // ....

    // alternatively, you can assign the object here
    JButton withdrawalButton;

    // ....

    public MyAtm() {

        // .....

        // creating a **local** variable, one that shadows the class field
        JButton withdrawalButton = new JButton("Withdraw");

        // ....

        withdrawalButton.addActionListener(new JText());

    }

    private class JText implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (e.getSource() == (withdrawalButton)) {
                    System.out.println(withdrawalButton.getName());
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }

    // ...
}

这是你想要做的:

public class MyAtm extends JPanel {

    // ....

    // class field is never initialized
    JButton withdrawalButton;

    // ....

    public MyAtm() {

        // .....

        // initialize the field
        withdrawalButton = new JButton("Withdraw");

        // ....

        withdrawalButton.addActionListener(new JText());

    }

    private class JText implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                // here withdrawalButton is the null field
                if (e.getSource() == (withdrawalButton)) {
                    System.out.println(withdrawalButton.getName());
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }

    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多