【问题标题】:How to Switch between Panels in CardLayout from ActionListeners in external classes如何从外部类中的 ActionListener 在 CardLayout 中的面板之间切换
【发布时间】:2016-03-29 11:54:36
【问题描述】:

我在主类中有一个 cardLayout,它通过面板将 Gui 类添加到布局中,当按下 Room1Button 时,它将如何将 main 方法中的卡切换到 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

 }

【问题讨论】:

标签: java swing actionlistener cardlayout


【解决方案1】:

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

【讨论】:

  • 所以这个例子中的父级是指在主类中制作的面板
  • 本例中有两个父面板。第一个是包含按钮的面板。第二种是使用 CardLayout 的面板。
【解决方案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);
 }
  });

【讨论】:

  • How to Use CardLayout 上的 Swing 教程有演示代码,展示了在同一类中定义所有代码时如何交换面板。教程代码将是一个更好的示例,因为它还展示了如何更好地构建代码,以便在 EDT 上创建 GUI。但是,本教程或此代码示例都没有回答这个问题。 OP 想知道当面板在单独的类中定义时如何执行此操作,而不是在主类中,因此您无法直接访问 CardLayout..
  • 你为我节省了很多时间,谢谢兄弟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-08
  • 2020-09-19
相关资源
最近更新 更多