简体   繁体   English

JButton无法显示JPanel

[英]JButton Wont Display JPanel

I want to call My JPanel with button. 我想用按钮调用My JPanel。 My Jpanel is actually a sub JPanel from main Panel with card layout. 我的Jpanel实际上是主面板上具有卡布局的子JPanel。

to do that, i am using card layout api method HERE to show the JPanel after a button was clicked. 为此,我在单击按钮后使用卡布局API方法在此处显示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

        }  

output : 输出:

nothing appear, panel not called and no error pop out. 什么都没有出现,面板没有被调用,并且没有错误弹出。

My Code is 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 回答

The reason i cant call my panel because i did not call it by using identifier. 我之所以无法打电话给我的面板,是因为我没有使用标识符来打电话。 i have to give identifier name to my desire jpanel and use the identifier on my cardLayout.show(..) 我必须给我想要的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

    }  

As I suspected — You're calling the CardLayout.show(...) method with the String parameter "PanelOne", but yet you've not added any component to the CardLayout-using container using this same String, so it makes sense that it won't work. 正如我所怀疑的—您正在使用String参数“ PanelOne”调用CardLayout.show(...)方法,但是您尚未使用此相同的String向使用CardLayout的容器添加任何组件,因此这很有意义那是行不通的 Solution: don't do this. 解决方案:不要这样做。 Use the Same String that you add the component to the CardLayout using container as the one that you use to display it. 使用将容器添加到CardLayout的相同字符串作为用于显示组件的字符串。

ie, If you want to display container foo and use the String "bar" to add it to the CardLayout -using container, then you must pass "bar" into the CardLayout 's show(...) method. 即,如果要显示容器foo并使用String "bar"将其添加到使用容器的CardLayout中,则必须将“ bar”传递到CardLayoutshow(...)方法中。 Again, use String constants for this so that you reduce the chances of messing up. 同样,为此使用字符串常量,以减少混乱的可能性。

Other issues: You're using null layout and setBounds — Don't. 其他问题:您正在使用null布局和setBounds不用。 Doing this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. 这样做会使GUI非常不灵活,尽管它们在一个平台上看起来可能不错,但在大多数其他平台或屏幕分辨率上看起来却很糟糕,并且很难更新和维护。

eg, 例如,

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

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

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