嵌套操作侦听器不工作



我正在尝试让按钮响应被按下并输出到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);
}

}

JButton withdrawalButton = new JButton("Withdraw");

删除"JButton",您要分配给实例变量,而不是局部变量。

查看您的操作侦听器实现,如下所示:

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)检查将源与实例变量进行比较。

您的问题是,您通过声明两次来隐藏 withdrawButton 变量,一次在类中,其中它是空的,另一次是在 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);
}
}
}
// ...
}

最新更新