我正在尝试创建和假设的ATM GUI界面,通过键盘输入几个数字。在用户单击任何按钮后,我无法让程序显示数字。我只是为了时间而创建了一个按钮:
public JButton jbtOne = new JButton(STANDARD_BTN_TEXTS[0][0]);
因此,如果用户点击了' jbtOne'说4次。 JTextField应该显示1111.我的问题是按钮没有响应代码行:
addActionListener(listener)
如何让JButton在JTextField中打印整数?我之前已经开始使用它了,但是在使用这行代码添加更加用户友好的外观之后,它已经无法再次工作了:
private static final String[][] STANDARD_BTN_TEXTS =
{
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{ "0" }
有人能指出我正确的方向吗?任何帮助将不胜感激!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPasswordField;
public class TerminalATM extends JFrame
{
private JPanel panel;
public final JPasswordField passwordField = new JPasswordField(2);
private static final String[][] STANDARD_BTN_TEXTS =
{
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{ "0" }
};
private static final int GAP = 5;
private static final Font BTN_FONT = new Font(Font.DIALOG, Font.BOLD, 20);
public JButton jbtOne = new JButton(STANDARD_BTN_TEXTS[0][0]);
private JTextField jtfNumber1 = new JTextField(8);//Define Number Field
public TerminalATM()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel standardPanel = createBtnPanel(STANDARD_BTN_TEXTS, "KeyPad");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0, 1));
buttonPanel.add(jtfNumber1, BorderLayout.NORTH);
buttonPanel.add(standardPanel, BorderLayout.SOUTH);
BtnListener listener = new BtnListener();
jbtOne.addActionListener(listener);
TextFieldHandler handler = new TextFieldHandler();
passwordField.addActionListener(handler);
add(buttonPanel, BorderLayout.LINE_START);
setSize(450, 500);
setVisible(true);
}
//Create Unique Rows of Buttons
private JPanel createBtnPanel(String[][] texts, String title) {
JPanel btnPanel = new JPanel();
int rows = texts.length;
int cols = texts[0].length;
btnPanel.setLayout(new GridLayout(rows, cols, GAP, GAP));
for (int row = 0; row < texts.length; row++) {
for (int col = 0; col < texts[row].length; col++) {
JButton btn = new JButton(texts[row][col]);
btn.setFont(BTN_FONT);
btnPanel.add(btn);
}
}
btnPanel.setBorder(BorderFactory.createTitledBorder(title));
return btnPanel;
}
private class TextFieldHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
String string = "";
if(event.getSource()==passwordField)
string = String.format("textField1: %s", event.getActionCommand());
}
}
/**** Create Button Listener and Action Listener ****/
class BtnListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
/* This is where we would set each button to the action event */
/* Only Button one for brevity */
int int1=0;
if(e.getSource().equals(jbtOne))
{
int1 = 1;
passwordField.setText(String.valueOf(int1));
}
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TerminalATM();
}
});
}
}//EndTerminalATM
答案 0 :(得分:2)
我认为您没有将createBtnPanel
添加到可见面板中。您的createBtnPanel
方法会创建自己的按钮并将其添加到面板中,而不会有任何动作侦听器。
尝试更改for (int row = 0; row < texts.length; row++) {
for (int col = 0; col < texts[row].length; col++) {
final String text = texts[row][col];
final JButton btn = new JButton(text);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
passwordField.setText(text);
}
});
btn.setFont(BTN_FONT);
btnPanel.add(btn);
}
}
的内部循环的以下行:
POST
如果你不明白这是做什么或有任何问题,请告诉我。
如果您使用的是Java 8,也可以大大简化此代码,但我认为您不能直接回答。
答案 1 :(得分:0)
您的侦听器正在使用passwordField,但您似乎已在面板中添加了一个文本字段,请使用passwordField或jtfNumber1。