执行的操作不会更改文本

时间:2016-04-11 13:53:53

标签: java swing user-interface

我有actionPerformed,其中一个if / else是如果用户按下“A”键,它会将文本设置为不同的。程序跳过设置文本并向下移动到下面的if语句,而不是在按“A”后设置文本。我的问题是,如何让我的程序在if语句之前设置文本?我的代码在下面,谢谢!

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;

public class ATMProject extends JPanel implements ActionListener {
    private JPanel mainPanel = null;
    private JPanel btnPanel = null;
    private JPanel userBtns = null;
    private JTextArea textArea = null;
    private JPanel keyPanel = null;
    private JTextField numField = null;
    private JPanel numpadPanel = null;
    private JButton[] userButtons = null;
    private JButton[] keypadButtons = null;
    private String[] btnPanelbtns = { "A", "B", "C" };
    private String[] numpadPanelbtns = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "CE" };
    private boolean isuserButtonPressed = false;
    private boolean isAccEntered = true;
    private FileReader file = null;
    private String custAcc;
    private String custPin;
    private String userInputAcc;
    private String userInputPin;

    public ATMProject() {
        super();
        mainPanel = new JPanel();
        this.setLayout(new BorderLayout());
        this.add(mainPanel);

        btnPanel = new JPanel();
        btnPanel.setLayout(new GridLayout(3, 1));
        this.add(btnPanel, BorderLayout.EAST);

        textArea = new JTextArea();
        this.add(textArea, BorderLayout.CENTER);

        keyPanel = new JPanel();
        keyPanel.setLayout(new BorderLayout());
        this.add(keyPanel, BorderLayout.WEST);

        numpadPanel = new JPanel();
        numpadPanel.setLayout(new GridLayout(4, 3));
        keyPanel.add(numpadPanel, BorderLayout.CENTER);

        numField = new JTextField();
        keyPanel.add(numField, BorderLayout.NORTH);

        userButtons = new JButton[btnPanelbtns.length];
        for (int i = 0; i < userButtons.length; i++) {
            userButtons[i] = new JButton(btnPanelbtns[i]);
            userButtons[i].addActionListener(this);
            btnPanel.add(userButtons[i]);
        }

        keypadButtons = new JButton[numpadPanelbtns.length];
        for (int i = 0; i < numpadPanelbtns.length; i++) {
            keypadButtons[i] = new JButton(numpadPanelbtns[i]);
            keypadButtons[i].addActionListener(this);
            numpadPanel.add(keypadButtons[i]);
        }

        textArea.setText("Enter customer number: --> \nPress A when done \nA = OK");
        userInputAcc = numField.getText();

        // reading
        try {
            file = new FileReader("ConfidentialCustInfo.txt");
            Scanner input = new Scanner(file);
            custAcc = input.next();
            custPin = input.next();
            System.out.println(custAcc);
            System.out.println(custPin);
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("CE")) {
            numField.setText("");
        } else if (e.getActionCommand().equals("1")) {
            numField.setText(numField.getText() + "1");
        } else if (e.getActionCommand().equals("2")) {
            numField.setText(numField.getText() + "2");
        } else if (e.getActionCommand().equals("3")) {
            numField.setText(numField.getText() + "3");
        } else if (e.getActionCommand().equals("4")) {
            numField.setText(numField.getText() + "4");
        } else if (e.getActionCommand().equals("5")) {
            numField.setText(numField.getText() + "5");
        } else if (e.getActionCommand().equals("6")) {
            numField.setText(numField.getText() + "6");
        } else if (e.getActionCommand().equals("7")) {
            numField.setText(numField.getText() + "7");
        } else if (e.getActionCommand().equals("8")) {
            numField.setText(numField.getText() + "8");
        } else if (e.getActionCommand().equals("9")) {
            numField.setText(numField.getText() + "9");
        } else if (e.getActionCommand().equals("A")) {
            if (numField.getText().isEmpty()) {
                textArea.setText("error");
            } else {
                numField.setText("");
                textArea.setText("Enter PIN: \n A = OK");
                userInputPin = (numField.getText());

            }
        }
    }

    public static void main(String[] args) {
        MyFrame mf = new MyFrame();
    }

}

2 个答案:

答案 0 :(得分:2)

你需要给你的程序一个状态变量,它会告诉它它处于什么状态,然后允许监听器根据该状态做出不同的响应。

例如,你可以给它一个布尔变量或变量,比如

private boolean enteringAcctNumber = true;

然后在ActionListener中放置一个if块,检查此布尔值并做出相应的响应。

例如

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ATMProj2 extends JPanel {
    private static final int COLS = 30;
    private static final String ACCOUNT_PROMPT = "Enter Account Number";
    protected static final String ENTER_PIN = "Account number accepted. Enter PIN";
    protected static final String PIN_ACCEPTED = "PIN has been accepted. Have fun.";
    private boolean enteringAcctNumber = true;
    private JTextField textField = new JTextField(COLS);
    private JTextArea textArea = new JTextArea(6, COLS);
    private JButton okButton = new JButton("OK");

    public ATMProj2() {
        textArea.setText(ACCOUNT_PROMPT);
        textArea.setFocusable(false); // so user can't mess with it
        okButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // test state variable, and base behavior on its state
                if (enteringAcctNumber) {
                    textArea.setText(ENTER_PIN);
                    textField.setText("");
                    enteringAcctNumber = false; // ok now not entering account
                } else {
                    textArea.setText(PIN_ACCEPTED);
                }
            }
        });

        JPanel btnPanel = new JPanel();
        btnPanel.add(okButton);

        setLayout(new BorderLayout());
        add(textField, BorderLayout.PAGE_START);
        add(new JScrollPane(textArea), BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);
    }

    private static void createAndShowGui() {
        ATMProj2 mainPanel = new ATMProj2();

        JFrame frame = new JFrame("ATMProj2");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}

答案 1 :(得分:2)

这是您的其他帖子的评论:

  

当我使用userInputAcc = numField.getText()时,我看到了它没有从JTextField()获取文本。我认为这是因为我将JTextArea的文本设置为userInputAcc并且屏幕变为空白&gt;

textArea.setText("Enter customer number: --> \nPress A when done \nA = OK");
userInputAcc = numField.getText();

当然它是空白的,因为在构建GUI时执行该语句并且用户还没有机会在文本字段中键入任何内容。执行上述语句时,框架甚至不可见。

这很容易验证。只需将您的代码更改为:

userInputAcc = numField.getText();
System.out.println( "(" + userINputAcc + ")" );

这是在为变量赋值时显示变量值的基本调试。

生成事件时,您只能从文本字段中获取文本,并且您可以编写代码来处理事件。因此userInputAcc变量将始终为空。

另外,为什么你的代码仍然在ActionListener中有丑陋的嵌套if语句?