简体   繁体   中英

read actionlistener in JFrame from JPanel

in the code below i want to change the panel depending on the event of buttons situated in Panel1 and Panel2 .The Panel1 only contains a button the same goes for Panel2

public class Window extends JFrame {
 public window(){
this.setTitle("CardLayout");
this.setSize(300, 120);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
 Panel2 p2 = new Panel2();
 Panel1 p1 = new Panel1();
 this.getContentPane().add(p1);
 this.setVisible(true);
p2.getButton2().addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {

                // TODO Auto-generated method stub
                Panel1 p1 = new Panel1();
                getContentPane().removeAll();
                getContentPane().add(p1);
                getContentPane().validate();
                getContentPane().repaint();
                System.out.println("change to panel1");

    }   
});
p1.getButton1().addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
            Panel2 p2 = new Panel2();
                getContentPane().removeAll();
                getContentPane().add(p2);
                getContentPane().validate();
                getContentPane().repaint();
                System.out.println("change to panel2");


    }   
});

} }

you might be looking for this

public class testframe extends JFrame {

private JPanel contentPane;
String panel = "Card with JButtons";
String panel2 = "Card with JTextField";
JPanel cards = new JPanel();
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();
/**
 * Create the frame.
 */
public testframe() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    contentPane = new JPanel();
    setContentPane(contentPane);  

    cards = new JPanel(new CardLayout());
    card1 = new JPanel();
    card2 = new JPanel();

    JButton b1 = new JButton("button 1");
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, panel2); 
        }
    });
    card1.add(b1);

    JButton b2 = new JButton("button 2");
    b2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, panel);
        }
    });
    card2.add(b2);      

    cards.add(card1, panel);
    cards.add(card2, panel2);



    contentPane.add(cards);

}

if that is so, this will be helpful as well: CardLayout

When you switch panels, you are creating and adding new JPanel objects, without adding an ActionListener to the buttons within those new JPanel instances. So either reuse your old JPanels within the ActionListeners you create here where you do the following, with the changes here to add the final keyword to p2 and p1:

final Panel2 p2 = new Panel2();
final Panel1 p1 = new Panel1();
this.getContentPane().add(p1);
this.setVisible(true);

and in the individual ActionListeners you create, remove the lines of:

Panel1 p1 = new Panel1();

and

Panel2 p2 = new Panel2();

Removing those lines will cause p1 and p2 to refer to the original p1 and p2 panels. Although you should probably just use the card layout.

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