我正在尝试让按钮响应按下并输出到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);
}
}
答案 0 :(得分:2)
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)
检查将源与实例变量进行比较。
答案 1 :(得分:1)
你的问题是你通过声明 两次 来隐藏withdrawButton变量,一次在类中,它是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);
}
}
}
// ...
}