简体   繁体   中英

CardLayout - Won't switch back to the first Panel

I make'd a simple program, almost works everything, except the back switch to the first panel. So, if I click on the "Druck" button, it will transfer money from a bank account to another bank account, then it switch to the second panel, where is showing up the balance of both account, and also on the second panel is a button, which should take care to switching back to the first panel, but it nothings happen, when click'd it. There is also two other classes, "Bank" and "Customer", but I didn't paste the code, because I think that is not necessary. My english isn't the best, so if I wasn't clear, let me know! Thanks in advance!

public class View extends JFrame {
private Container c;
private JPanel oben, unten, firstPanel, secondPanel;
private JLabel kontoInhaber, empfanger, bildBehalter, secondJLabel;
private JTextField jtextKontoinhaber, jtextempfanger, menge;
private JButton button, backButton;
private ImageIcon image;
private CardLayout cl;

public View (Bank bank){
    c = getContentPane();
    cl = new CardLayout();
    oben = new JPanel();
    unten = new JPanel();
    firstPanel = new JPanel();
    secondPanel = new JPanel();

    kontoInhaber = new JLabel("Sender");
    empfanger = new JLabel("Empfanger");
    secondJLabel = new JLabel();

    image = new ImageIcon("/Users/jetser/Desktop/ProgramozasKepek/bank.jpg");
    bildBehalter = new JLabel(image);

    jtextKontoinhaber = new JTextField(10);
    jtextempfanger = new JTextField(10);
    menge = new JTextField(10);

    button = new JButton("Druck");
    button.setActionCommand("uberweisen");
    backButton = new JButton("Back");
    backButton.setActionCommand("back");

    //------------HOZZAADNI A DOLGOKAT-------------//

    oben.add(bildBehalter);
    oben.setBackground(Color.PINK);
    unten.setLayout(new GridLayout(3,2));
    unten.add(kontoInhaber);
    unten.add(jtextKontoinhaber);
    unten.add(empfanger);
    unten.add(jtextempfanger);
    unten.add(button);
    unten.add(menge);
    unten.setBackground(Color.PINK);

    firstPanel.setLayout(new BoxLayout(firstPanel,BoxLayout.Y_AXIS));
    firstPanel.add(oben);
    firstPanel.add(unten);

    secondPanel.add(secondJLabel);
    secondPanel.add(backButton);

    c.setLayout(cl);
    c.add(firstPanel,"1");
    c.add(secondPanel,"2");
    cl.show(c,"1");

    ButtonListener listener = new ButtonListener(bank,jtextKontoinhaber,jtextempfanger,menge,secondJLabel,c);
    button.addActionListener(listener);
}}

public class ButtonListener implements ActionListener {
private Bank bank;
private JTextField einhalter, empfanger, geldMenge;
private JLabel secondLabel;
private Container c;

public ButtonListener(Bank bank, JTextField einhalter, JTextField empfanger, JTextField geldMenge, JLabel secondLabel, Container c) {
    this.bank = bank;
    this.einhalter = einhalter;
    this.empfanger = empfanger;
    this.geldMenge = geldMenge;
    this.secondLabel = secondLabel;
    this.c = c;
}

@Override
public void actionPerformed(ActionEvent e) {

    CardLayout cl = (CardLayout) (c.getLayout());
    if(e.getActionCommand().equals("uberweisen")){
        cl.show(c,"2");
        for(int i=0; i<bank.getKunden().length; i++){
            if(einhalter.getText().equals(bank.getKunden()[i].getName())){
                for(int j=0; j<bank.getKunden().length; j++){
                    if(empfanger.getText().equals(bank.getKunden()[j].getName())){
                        String geld = geldMenge.getText();
                        double geldMenge = Double.valueOf(geld);

                        bank.getKunden()[i].geldUberweisen(geldMenge);
                        bank.getKunden()[j].geldErhalten(geldMenge);

                        String output = bank.getKunden()[i].getName() + ": " + bank.getKunden()[i].getKontostand() + "\n" +
                                bank.getKunden()[j].getName() + ": " + bank.getKunden()[j].getKontostand();
                        secondLabel.setText(output);
                        System.out.println(output);
                    }
                }
            }
        }
    }
    else if(e.getActionCommand().equals("back")) {
        cl.show(c,"1");
    }

}}

I recreated your GUI and fixed the problem with your CardLayout .

Here are the major points I want to emphasize.

  1. You must always start a Swing application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread (EDT).

  2. I separated the creation of the JFrame , the CardLayout , the FirstPanel , and the SecondPanel in their own classes. That way, I could focus on one part of the GUI at a time.

  3. When I created the FirstPanel class, I created the Swing components in row, column order. I also put all of the method calls related to one Swing component together. This makes testing and debugging so much easier.

Anyway, here's the code I ran. You'll need to add that unimportant Bank and Customer information in the action listener.

You can access the JTextField s from the FirstPanel by calling the appropriate methods in the CardLayoutPanel class and the FirstPanel class. This way, I only had to pass one instance to the ButtonListener class.

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SimpleBankProgram 
        implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                new SimpleBankProgram());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);

        CardLayoutPanel panel = 
                new CardLayoutPanel();
        frame.add(panel.getPanel());

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class CardLayoutPanel {

        private CardLayout cardLayout;

        private FirstPanel firstPanel;

        private JPanel panel;

        private SecondPanel secondPanel;

        public CardLayoutPanel() {
            createPartControl();
        }

        private void createPartControl() {
            panel = new JPanel();
            cardLayout = new CardLayout();
            panel.setLayout(cardLayout);

            firstPanel = new FirstPanel(this);
            panel.add(firstPanel.getPanel(), "1"); 

            secondPanel = new SecondPanel(this);
            panel.add(secondPanel.getPanel(), "2"); 

            showFirstPanel();
        }

        public void showFirstPanel() {
            cardLayout.show(panel, "1");
        }

        public void showSecondPanel() {
            cardLayout.show(panel, "2");
        }

        public JPanel getPanel() {
            return panel;
        }

        public FirstPanel getFirstPanel() {
            return firstPanel;
        }

        public SecondPanel getSecondPanel() {
            return secondPanel;
        }

    }

    public class FirstPanel {

        private JPanel panel;

        private JTextField jtextKontoinhaber;
        private JTextField jtextempfanger;
        private JTextField menge;

        public FirstPanel(CardLayoutPanel 
                cardLayoutPanel) {
            createPartControl(cardLayoutPanel);
        }

        private void createPartControl(CardLayoutPanel
                cardLayoutPanel) {
            panel = new JPanel();
            panel.setLayout(new BoxLayout(
                    panel, BoxLayout.Y_AXIS));

            JPanel oben = new JPanel();
            oben.setBackground(Color.PINK);

            // An image goes here
            JLabel bildBehalter = new JLabel(" ");
            oben.add(bildBehalter);

            JPanel unten = new JPanel();
            unten.setBackground(Color.PINK);
            unten.setLayout(new GridLayout(3,2));

            JLabel kontoInhaber = new JLabel("Sender");
            unten.add(kontoInhaber);

            jtextKontoinhaber = new JTextField(10);
            unten.add(jtextKontoinhaber);

            JLabel empfanger = new JLabel("Empfanger");
            unten.add(empfanger);

            jtextempfanger = new JTextField(10);
            unten.add(jtextempfanger);

            ButtonListener listener = 
                    new ButtonListener(cardLayoutPanel);

            JButton button = new JButton("Druck");
            button.setActionCommand("uberweisen");
            button.addActionListener(listener);
            unten.add(button);

            menge = new JTextField(10);
            unten.add(menge);

            panel.add(oben);
            panel.add(unten);
        }

        public JPanel getPanel() {
            return panel;
        }

        public JTextField getJtextKontoinhaber() {
            return jtextKontoinhaber;
        }

        public JTextField getJtextempfanger() {
            return jtextempfanger;
        }

        public JTextField getMenge() {
            return menge;
        }

    }

    public class SecondPanel {

        private JLabel secondJLabel;

        private JPanel panel;

        public SecondPanel(CardLayoutPanel 
                cardLayoutPanel) {
            createPartControl(cardLayoutPanel);
        }

        private void createPartControl(CardLayoutPanel 
                cardLayoutPanel) {
            panel = new JPanel();

            secondJLabel = new JLabel(" ");
            panel.add(secondJLabel);

            ButtonListener listener = 
                    new ButtonListener(cardLayoutPanel);

            JButton backButton = new JButton("Back");
            backButton.setActionCommand("back");
            backButton.addActionListener(listener);
            panel.add(backButton);

        }

        public JPanel getPanel() {
            return panel;
        }

        public void setJLabel(String text) {
            secondJLabel.setText(text);
        }
    }

    public class ButtonListener implements ActionListener {

        private CardLayoutPanel cardLayoutPanel;

        public ButtonListener(CardLayoutPanel 
                cardLayoutPanel) {
            this.cardLayoutPanel = cardLayoutPanel;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            if (event.getActionCommand()
                    .equals("uberweisen")) {
                String output = "Some bank text I "
                        + "can't duplicate";
                cardLayoutPanel.getSecondPanel()
                        .setJLabel(output);
                cardLayoutPanel.showSecondPanel();
            } else if (event.getActionCommand()
                    .equals("back")) {
                cardLayoutPanel.showFirstPanel();
            }
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM