JButton不显示JPanel

时间:2014-09-29 03:00:17

标签: java swing cardlayout

我想用按钮调用我的JPanel。我的Jpanel实际上是主面板的子JPanel,带有卡片布局。

要做到这一点,我使用卡片布局api方法HERE来显示点击按钮后的JPanel。

JButton btnCallPanel1 = new JButton("Call PanelOne");
   btnCallPanel1.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {

            CardLayout card = (CardLayout)mainPanel.getLayout();
            card.show(mainPanel, "PanelOne");                     //call Panel One

        }  

输出

什么都没有出现,面板没有被调用,没有错误弹出。

我的代码是HERE

package wan.dev.sample.cardlayout;

import java.awt.CardLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;

public class HowToUseCardLayout {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HowToUseCardLayout window = new HowToUseCardLayout();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public HowToUseCardLayout() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 688, 358);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel mainPanel = new JPanel();
        mainPanel.setBounds(0, 0, 672, 260);
        frame.getContentPane().add(mainPanel);
        mainPanel.setLayout(new CardLayout(0, 0));

                JPanel PrePanel = new JPanel();
                mainPanel.add(PrePanel, "name_246268073832057");
                PrePanel.setLayout(null);

                        JLabel lblPanel_1 = new JLabel("Pre Panel");
                        lblPanel_1.setBounds(280, 115, 57, 20);
                        PrePanel.add(lblPanel_1);

        JPanel panelOne = new JPanel();
        mainPanel.add(panelOne, "name_246268067657434");
        panelOne.setLayout(null);

        JLabel lblPanel = new JLabel("panel 1");
        lblPanel.setBounds(279, 118, 46, 14);
        panelOne.add(lblPanel);

        JButton btnPan1 = new JButton("Call PanelOne");
        btnPan1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                CardLayout card = (CardLayout) mainPanel.getLayout();
                card.show(mainPanel, "PanelOne");

            }
        });
        btnPan1.setBounds(262, 286, 144, 23);
        frame.getContentPane().add(btnPan1);
    }
}

ANSWER

我无法调用我的面板的原因是因为我没有使用标识符来调用它。 我必须给我的欲望jpanel提供标识符名称并使用我的cardLayout.show上的标识符(..)

Public Static final String PANEL_ONE = "panel one";                  //Name of JPanel Identifier


//add panel to main panel and declare panelOne identifier
mainPanel.add(panelOne, PANEL_ONE);                         //PANEL_ONE function like 
                                                            //an identifier


JButton btnCallPanel1 = new JButton("Call PanelOne");
btnCallPanel1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {

        CardLayout card = 
                     (CardLayout)mainPanel.getLayout();

        card.show(mainPanel, PANEL_ONE);                     //call panelOne using PANEL_ONE 
                                                             //instead of JPanel name

    }  

1 个答案:

答案 0 :(得分:4)

我怀疑 - 您使用CardLayout.show(...)参数" PanelOne"来调用String方法,但是您还没有添加任何组件到使用相同String的CardLayout使用容器,因此它不会起作用。解决方案:不要这样做。使用将组件添加到CardLayout的相同字符串,使用容器作为用于显示它的容器。

即,如果您想显示容器foo并使用String "bar"将其添加到CardLayout - 使用容器,那么您必须通过" bar&#34 ;进入CardLayout show(...)方法。再次,为此使用String常量,以便减少搞乱的可能性。

其他问题:您正在使用null布局和setBounds - 不要。这样做会使GUI非常不灵活,虽然它们在一个平台上看起来不错但在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护。

如,

import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class CardLayoutFoo extends JPanel {
   public static final String BAR = "bar";
   public static final String BUTTON_PANEL = "button panel";
   private CardLayout cardlayout = new CardLayout();

   public CardLayoutFoo() {
      setLayout(cardlayout);

      JLabel fooLabel = new JLabel("Foo", SwingConstants.CENTER);
      add(fooLabel, BAR); // added using String constant, BAR

      JButton showFooBtn = new JButton(new AbstractAction("Show Foo") {

         @Override
         public void actionPerformed(ActionEvent evt) {
            // use same String, BAR, to get the fooLabl shown
            cardlayout.show(CardLayoutFoo.this, BAR);
         }
      });
      JPanel btnPanel = new JPanel();
      btnPanel.add(showFooBtn);
      add(btnPanel, BUTTON_PANEL);

      cardlayout.show(this, BUTTON_PANEL);            
   }

   private static void createAndShowGui() {
      CardLayoutFoo mainPanel = new CardLayoutFoo();

      JFrame frame = new JFrame("CardLayoutFoo");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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