Как сделать txPanel видимым только после нажатия кнопки «Ввести пин-код»? - PullRequest
1 голос
/ 13 марта 2012
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);
    }
}

Ответы [ 3 ]

2 голосов
/ 13 марта 2012

Я не думаю, что это правильный способ реализации ActionListeners для кнопок.

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

      // Container contentPane = getContentPane();
        if (actionCommand.equals("Transactions OK"))
        else ...
    }

С помощью отметок if-else в методе actionPerformed ваша программа вынуждена проверять, какой прослушиватель вызывать, каждый раз, когда нажимается какая-либо кнопка, и, таким образом, ваш код нелегко редактировать и повторно использовать. Кроме того, GUI-контейнер действует как приемник событий, тогда вам следует избегать

pinButton.addActionListener(this);

Попробуйте реализовать свои собственные внутренние классы для каждой кнопки, например:

JButton pinButton = new JButton("Enter Pin OK");
        pinButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae){
                    //enter here your action
                                     txPanel.setVisible(true);
                }
            });

Таким образом, вам не нужно реализовывать интерфейс ActionListener для вашего класса, потому что вы реализуете внутреннюю версию интерфейса для вашего pinButton. Проверьте этот старый вопрос SO. Кроме того, вам следует избегать реализации всех ваших элементов GUI в конструкторе классов, лучше реализовать GUI в отдельном методе, таком как createAndShowGui(), и вызывать его в конструкторе, чтобы соблюдать соглашения Java Swing и для запуска компонентов Swing в другом потоке, называемом Поток диспетчеризации событий , отличном от основного потока вашего приложения. Прочитайте этот вопрос .

Затем включите txPanel.setVisible(false); в метод createAndShowGui().

Помните, что компоненты Swing не являются поточно-ориентированными.

1 голос
/ 13 марта 2012

Поскольку вставленный вами код не работает, я создал для вас небольшую программу, посмотрите и посмотрите, какие изменения вы можете внести, чтобы включить это в ваш случай:

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

public class PanelTest extends JFrame
{
    private JPanel eastPanel;

    public PanelTest()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        Container container = getContentPane();

        eastPanel = new JPanel();
        eastPanel.setBackground(Color.DARK_GRAY);

        JPanel westPanel = new JPanel();
        westPanel.setBackground(Color.YELLOW);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.BLUE);

        container.add(eastPanel, BorderLayout.LINE_START);
        container.add(centerPanel, BorderLayout.CENTER);
        container.add(westPanel, BorderLayout.LINE_END);
        eastPanel.setVisible(false);

        JButton showButton = new JButton("Click Me to Display EAST JPanel");
        showButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                eastPanel.setVisible(true);
            }
        });

        JButton hideButton = new JButton("Click Me to Hide EAST JPanel");
        hideButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                eastPanel.setVisible(false);
            }
        });

        container.add(hideButton, BorderLayout.PAGE_START);
        container.add(showButton, BorderLayout.PAGE_END);

        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PanelTest();
            }
        });
    }
}

И из будущего никогда не используйте NORTH, EAST, WEST и SOUTH для BorderLayout. Они были заменены на PAGE_START, LINE_START, LINE_END и PAGE_END соответственно.

Объект BorderLayout имеет пять областей. Эти области определяются константами BorderLayout:

  • PAGE_START
  • PAGE_END
  • LINE_START
  • LINE_END
  • CENTER

Примечание о версии: До выпуска 1.4 JDK предпочтительные имена для различных областей были разными, начиная от точек компаса (например, BorderLayout.NORTH для верхней области) до более объемных версий констант, которые мы используем в наших примерах. Константы, используемые в наших примерах, являются предпочтительными, поскольку они являются стандартными и позволяют программам настраиваться на языки с различной ориентацией.

Я изменил метод checkPin(...) класса ATMLogin, чтобы он возвращал boolean вместо void, чтобы внутри метода actionPerformed(...) класса ATMgui, если эта вещь возвращает true , затем только для установки требуемого JPanel в видимое, иначе ничего не нужно делать.

Проверьте код и посмотрите, какие изменения вы можете сделать, чтобы он заработал для вас.

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

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
    private JPanel txPanel;
    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);




        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(false);


        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());
                if(theBLU.checkPin(pin))
                    txPanel.setVisible(true);
                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());
                ex.printStackTrace();
            }
        } 
        else 
        {
            System.out.println("Error in button interface.");
        }
    }

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

class ATMbizlogic 
{

    private double totalBalance;
    private boolean rightPinEntered;

    /**
     * Creates a new instance of ATMbizlogic
     */
    public ATMbizlogic() 
    {
        totalBalance = 0.0;
        rightPinEntered =  true;
    }

    public void computeBalance(double withdraw, double deposit)
    //throws ATMexception 
    {
        if(withdraw <=0)
        {
            System.out.println("Negative withdraw not allowed");
            //throw new ATMexception("Negative withdraw not allowed");
        }   

        if(deposit <=0)
        {
            System.out.println("Negative deposit not allowed");
            //throw new ATMexception("Negative deposit not allowed");
        }   

         double balance = deposit - withdraw;

        totalBalance = totalBalance + balance;
    }

    public boolean checkPin(double pin)
    //throws ATMexception 
    {
        if(pin <=0)
        {
            System.out.println("Negative pin not allowed");
            rightPinEntered = false;
            //throw new ATMexception("Negative pin not allowed");
        }   
        /*else if(rightPinEntered == false)
        {
            System.out.println("Can not take another pin");
            rightPinEntered = false;
            //throw new ATMexception("Can not take another pin");
        }*/ 
        else if(pin<1111 || pin>9999)
        {
            System.out.println("Enter a valid pin");
            rightPinEntered = false;
            //throw new ATMexception("Enter a valid pin");
        }
        else
        {       
            rightPinEntered = true;
        }

        return rightPinEntered;
    }

    public double getBalance()
    {
        return totalBalance;
    }
}
1 голос
/ 13 марта 2012

В вызове конструктора ATMgui () введите

txPanel.setVisible(false);

, а в части actionCommand.equals ("Введите пин-код") вы можете установить значение true.

Это то, что вы хотите?

...