简体   繁体   English

如何从外部类中的ActionListeners在CardLayout中的面板之间切换

[英]How to Switch between Panels in CardLayout from ActionListeners in external classes

I have a cardLayout in the main class that the Gui classes are added to layout via panels and when the Room1Button is pressed how would it switch the card in the main method to the Gui2 card 我在主类中有一个cardLayout,该Gui类通过面板添加到布局中,当按下Room1Button时,它将如何将主方法中的卡切换为Gui2卡

is this the best way to go about this any help would be apreatiated 这是解决此问题的最佳方法吗?

Main Method 主要方法

  import javax.swing.*;
  import java.awt.*;
  class Main
  {

CardLayout cl=new CardLayout();
GridBagConstraints gb=new GridBagConstraints();
JFrame frame=new JFrame("Frame");
JPanel panel =new JPanel();


Gui1 g1= Gui1();
Gui2 g2= Gui2();


public Main()
{
   panel.setLayout(cl);
   panel.add(g1, "1");
   panel.add(g2, "2");

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);

    cl.show(panel,"1");

    //how would the actionlistner in the Gui1 class switch the layout to "2"

    cl.show(panel, "2");


}


public static void main(String[]param)
{
    new Main();


}


}

The gui1 class gui1类

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
 import java.util.*;


 public class Gui1 extends JPanel implements ActionListener{


private JButton room1Button;

JPanel panel=new JPanel();

{



    setSize(1000,1000);

    panel.setVisible(true);

    room1Button=new JButton("Go the next Panel");

    this.setVisible(true);
    room1Button.addActionListener(this);
    add(room1Button);


}

@Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource()==room1Button){
       Window w = SwingUtilities.getWindowAncestor(R0.this);
       w.setVisible(false);

    }

}
}

The Gui2 class Gui2班

 public class Gui2 extends JPanel implements Actionlistener
 {
        // some code

 }

The ActionEvent will contain the source object that generated the event. ActionEvent将包含生成事件的源对象。 In this case the JButton. 在这种情况下是JButton。 So generic code for the ActionListener in your GUI1 class would be something like: 因此,GUI1类中ActionListener的通用代码如下所示:

JButton button = (JButton)e.getSource();
JPanel buttonPanel = (JPanel)button.getParent();
JPanel cardLayoutPanel = (JPanel)buttonPanel.getParent();
CardLayout layout = (CardLayout)cardLayoutPanel.getLayout();
layout.show(cardLayoutPanel, "2");

Hope this helps. 希望这可以帮助。

package cardlayoutsample;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CardLayoutSample {
    JFrame frame = new JFrame("CardLayout Demo");
    JPanel panelCont = new JPanel();
    JPanel panelFirst = new JPanel();
    JPanel panelSecond = new JPanel();
    JButton btnOne = new JButton("Switch");
    JButton btnTwo = new JButton("Back");

    CardLayout cl = new CardLayout();

    public CardLayoutSample(){
        panelCont.setLayout(cl);

        panelFirst.add(btnOne);
        panelSecond.add(btnTwo);
        panelFirst.setBackground(Color.red);
        panelSecond.setBackground(Color.blue);

        panelCont.add(panelFirst,"1");
        panelCont.add(panelSecond,"2");
        cl.show(panelCont, "1");

        btnOne.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0){
            cl.show(panelCont, "2");
        }
    });

        btnTwo.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0){
            cl.show(panelCont, "1");
        }
    }); 

        frame.add(panelCont);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
    CardLayoutSample a = new CardLayoutSample();
    }
}

Try to play this button you can see it's switching the panels when you clicked the buttons. 尝试播放此按钮,您可以在单击按钮时看到它正在切换面板。

Syntax for ActionListener ActionListener的语法

Component.addActionListener(new ActionListener (){
@Override
  public void actionPerformed(ActionEvent e){
  //do this
 }
});

Example

LogoutButton.addActionListener(new ActionListener (){
@Override
   public void actionPerformed(ActionEvent e){
   System.exit(0);
 }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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