簡體   English   中英

如何從外部類中的ActionListeners在CardLayout中的面板之間切換

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

我在主類中有一個cardLayout,該Gui類通過面板添加到布局中,當按下Room1Button時,它將如何將主方法中的卡切換為Gui2卡

這是解決此問題的最佳方法嗎?

主要方法

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


}


}

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

    }

}
}

Gui2班

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

 }

ActionEvent將包含生成事件的源對象。 在這種情況下是JButton。 因此,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");

希望這可以幫助。

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

嘗試播放此按鈕,您可以在單擊按鈕時看到它正在切換面板。

ActionListener的語法

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

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