首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使txPanel仅在单击“enter pin ok”按钮后可见?

如何使txPanel仅在单击“enter pin ok”按钮后可见?
EN

Stack Overflow用户
提问于 2012-03-13 15:36:39
回答 3查看 518关注 0票数 1
代码语言:javascript
复制
public class ATMgui extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 500;
    public static final int HEIGHT = 200;
    private ATMbizlogic theBLU;// short for the Business Logic Unit
    public JLabel totalBalanceLabel;
    public JTextField withdrawTextField;
    public JTextField depositTextField;
    public JTextField pinTextField;

    /**
     * Creates a new instance of ATMgui
     */
    public ATMgui() {
        setTitle("ATM Transactions");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Container contentPane = getContentPane();
        contentPane.setBackground(Color.BLACK);
        contentPane.setLayout(new BorderLayout());


        // Do the panel for the rest stop
        JLabel start = new JLabel("Welcome To Your Account", JLabel.CENTER);
        Font curFont = start.getFont();
        start.setFont(new Font(curFont.getFontName(), curFont.getStyle(), 25));
        start.setForeground(Color.BLUE);
        start.setOpaque(true);
        start.setBackground(Color.BLACK);


        pinTextField = new JTextField();
        JLabel pinLabel = new JLabel("Enter your PIN below:", JLabel.CENTER);
        pinLabel.setForeground(Color.RED);
        pinLabel.setOpaque(true);
        pinLabel.setBackground(Color.WHITE);



        JButton pinButton = new JButton("Enter Pin OK");
        pinButton.addActionListener(this);
        pinButton.setBackground(Color.red);

        JPanel pinPanel = new JPanel();
        pinPanel.setLayout(new GridLayout(3, 1, 100, 0));

        pinPanel.add(pinLabel);
        pinPanel.add(pinTextField);
        pinPanel.add(pinButton);

        contentPane.add(pinPanel, BorderLayout.WEST);


        JPanel headingPanel = new JPanel();
        headingPanel.setLayout(new GridLayout());
        headingPanel.add(start);

        contentPane.add(headingPanel, BorderLayout.NORTH);


        // Do the panel for the amount & type of transactions
        withdrawTextField = new JTextField();
        JLabel withdrawLabel = new JLabel("Withdraw (0.00)", JLabel.CENTER);
        withdrawLabel.setForeground(Color.RED);
        withdrawLabel.setOpaque(true);
        withdrawLabel.setBackground(Color.WHITE);



        depositTextField = new JTextField();
        JLabel depositLabel = new JLabel("Deposit (0.00)", JLabel.CENTER);
        depositLabel.setForeground(Color.RED);
        depositLabel.setOpaque(true);
        depositLabel.setBackground(Color.WHITE);



        JButton txButton = new JButton("Transactions OK");
        txButton.addActionListener(this);
        txButton.setBackground(Color.red);




        JPanel txPanel = new JPanel();
        txPanel.setLayout(new GridLayout(5, 1, 30, 0));

        txPanel.add(withdrawLabel);
        txPanel.add(withdrawTextField);
        txPanel.add(depositLabel);
        txPanel.add(depositTextField);
        txPanel.add(txButton);

        contentPane.add(txPanel, BorderLayout.EAST);
        txPanel.setVisible(true);


        totalBalanceLabel = new JLabel("Your balance after transactions: ", JLabel.CENTER);
        totalBalanceLabel.setForeground(Color.BLUE);
        totalBalanceLabel.setOpaque(true);
        totalBalanceLabel.setBackground(Color.BLACK);


        contentPane.add(totalBalanceLabel, BorderLayout.SOUTH);


        theBLU = new ATMbizlogic();
    }

    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();

        // Container contentPane = getContentPane();

        if (actionCommand.equals("Transactions OK")) {
            try {
                double deposit = Double.parseDouble(depositTextField.getText().trim());
                double withdraw = Double.parseDouble(withdrawTextField.getText().trim());
                theBLU.computeBalance(withdraw, deposit);
                totalBalanceLabel.setText("Your balance after transactions: " + theBLU.getBalance());

            } catch (ATMexception ex) {
                totalBalanceLabel.setText("Error: " + ex.getMessage());
            } catch (Exception ex) {
                totalBalanceLabel.setText("Error in deposit or withdraw amount: " + ex.getMessage());
            }
        } else if (actionCommand.equals("Enter Pin OK")) {

            try {

                double pin = Double.parseDouble(pinTextField.getText().trim());
                theBLU.checkPin(pin);

                totalBalanceLabel.setText("Your balance after transactions: " + theBLU.getBalance());



            } catch (ATMexception ex) {
                totalBalanceLabel.setText("Error: " + ex.getMessage());
            } catch (Exception ex) {
                totalBalanceLabel.setText("Error in pin: " + ex.getMessage());

            }
        } else {
            System.out.println("Error in button interface.");
        }
    }

    public static void main(String[] args) {
        ATMgui gui = new ATMgui();
        gui.setVisible(true);
    }
}
EN

Stack Overflow用户

发布于 2012-03-13 16:22:02

在对构造函数ATMgui()的调用中,将

代码语言:javascript
复制
txPanel.setVisible(false);

在actionCommand.equals("Enter Pin OK")部分,您可以将其设置为true。

这是你想要的吗?

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9680017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档