Java Swing使用带“的CardLayout;卡片“;由多个JPanel组成

Java Swing使用带“的CardLayout;卡片“;由多个JPanel组成,java,swing,user-interface,cardlayout,Java,Swing,User Interface,Cardlayout,我正在尝试对两个独立的JPanel使用cardlayout,我将在cardlayout中使用的第一个JPanel是我的“主菜单”面板,它是一个简单的JPanel,上面有两个按钮 第二个JPanel我在我的cardalayout中使用的是我的“车库管理器”面板,它实际上是一个由5个JPanel组成的JPanel 我的第一个问题是,既然我的“车库管理器”面板是由多个JPanel组成的面板,那么我正在尝试做的是可能的吗 如果可能的话,你能解释一下我的代码在我的卡片布局中没有显示任何一张“卡片”的原因吗

我正在尝试对两个独立的
JPanel
使用
cardlayout
,我将在cardlayout中使用的第一个
JPanel
是我的“主菜单”面板,它是一个简单的
JPanel
,上面有两个按钮

第二个
JPanel
我在我的
cardalayout
中使用的是我的“车库管理器”面板,它实际上是一个由5个JPanel组成的
JPanel

我的第一个问题是,既然我的“车库管理器”面板是由多个JPanel组成的面板,那么我正在尝试做的是可能的吗

如果可能的话,你能解释一下我的代码在我的卡片布局中没有显示任何一张“卡片”的原因吗

因为我是新来的,所以我只能在这篇文章上附上一张图片,所以我附上了我的“GarageManager”面板应该是什么样子的图片

我将尝试附加我的“主菜单”面板应该是什么样子的图像,以及我在评论中运行代码时实际得到的灰色框架的图像,或者在我发布后在这篇文章上得到的图像

我对编程和swing相当陌生,因此非常感谢您对我的代码提出任何建设性的批评或帮助

Main.java

public static void main(String[] args) {
    window window = new window();
    window.createGUI();
   }
}
window.java

import javax.swing.*;
import java.awt.*;

public class window {
//Create default frame to be used
JFrame frame = new JFrame("Parking Garage Manager");

//JPanel to be used as card panel
JPanel cardPanel = new JPanel();
CardLayout cardLayout = new CardLayout();

//2 main JPanels to be used in cardLayout
JPanel mainMenuPanel = new mainMenu();
JPanel garageManagerPanel = new garageManager();

//frame height and width
private final int frameWidth = 1600;
private final int frameHeight = 1000;

public void createGUI() {
    cardPanel.setLayout(cardLayout);
    cardPanel.add(garageManagerPanel, "GMP");
    cardPanel.add(mainMenuPanel, "MMP");
    cardLayout.show(cardPanel, "MMP");
    frame.getContentPane().add(cardPanel);

    //manage frame essentials.
    frame.setSize(frameWidth, frameHeight);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.setVisible(true);
  }// end of createGUI class
}// end of window class
garageManager.java

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

public class garageManager extends JPanel {
//JPanels for garage manager card.
JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 300, 50));
JPanel eastPanel = new JPanel(new FlowLayout());
JPanel southPanel = new JPanel(new GridBagLayout());
JPanel westPanel = new JPanel(new FlowLayout());
JPanel centerPanel = new JPanel(new FlowLayout());
JButton backToMainMenu = new JButton("Back To Main Menu");
JButton compact = new JButton("Compact");
JButton fullSize = new JButton("Full Size");
JButton motorcycle = new JButton("Motorcycle");
JButton handicap = new JButton("HandiCap");
JLabel welcomeLabel = new JLabel();
JLabel vehicleChoice = new JLabel();
JLabel closestSpots = new JLabel();
private final int frameWidth = 1600;
private final int frameHeight = 1000;

public garageManager() {
    setSize(frameWidth, frameHeight);
    setLayout(new BorderLayout());

    // manage north panel
    northPanel.setSize(frameWidth, (int)(frameHeight * 0.2));
    northPanel.setLocation(0,0);
    northPanel.setBorder(new LineBorder(Color.gray));

    welcomeLabel.setText("Welcome To The Parking Garage System");
    welcomeLabel.setFont(new Font("Serif", Font.PLAIN, 50));
    welcomeLabel.setForeground(Color.RED);
    northPanel.add(welcomeLabel);

    // manage south panel
    southPanel.setSize(frameWidth, (int)(frameHeight * 0.2));
    southPanel.setLocation(0, (int)(frameHeight * 0.8));
    southPanel.setBorder(new LineBorder(Color.gray));
    GridBagConstraints constraints = new GridBagConstraints();

    vehicleChoice.setText("What Type of Vehicle Will You Be Parking?");
    vehicleChoice.setFont(new Font("Serif", Font.BOLD, 20));
    vehicleChoice.setForeground(Color.blue);

    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.ipady = 50;
    constraints.gridwidth = 10;
    constraints.gridheight = 2;
    constraints.gridx = 0;
    constraints.gridy = 0;
    southPanel.add(vehicleChoice, constraints);

    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.ipady = 20;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.gridx = 2;
    constraints.gridy = 2;
    fullSize.setForeground(Color.cyan);
    southPanel.add(fullSize, constraints);

    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.ipady = 20;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.gridx = 4;
    constraints.gridy = 2;
    compact.setForeground(Color.orange);
    southPanel.add(compact, constraints);

    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.ipady = 20;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.gridx = 6;
    constraints.gridy = 2;
    motorcycle.setForeground(Color.RED);
    southPanel.add(motorcycle, constraints);

    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.ipady = 20;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.gridx = 8;
    constraints.gridy = 2;
    handicap.setForeground(Color.GREEN);
    southPanel.add(handicap, constraints);

    // manage west panel
    westPanel.setSize((int)(frameWidth * 0.2), (int)(frameHeight * 0.6));
    westPanel.setLocation((int)(frameWidth * 0.8), (int)(frameHeight * 0.2));
    westPanel.setBorder(new LineBorder(Color.gray));

    westPanel.add(backToMainMenu);

    // manage east panel
    eastPanel.setSize((int)(frameWidth * 0.2), (int)(frameHeight * 0.6));
    eastPanel.setLocation(0, (int)(frameHeight * 0.2));
    eastPanel.setBorder(new LineBorder(Color.gray));

    closestSpots.setText("The closest spots available are...");
    closestSpots.setFont(new Font("Serif", Font.BOLD, 17));
    closestSpots.setForeground(Color.blue);

    eastPanel.add(closestSpots);

    // manage center panel
    centerPanel.setSize((int)(frameWidth * 0.6), (int)(frameHeight * 0.6));
    centerPanel.setLocation((int)(frameWidth * 0.2), (int)(frameHeight * 0.2));
    centerPanel.setBorder(new LineBorder(Color.gray));

    add(northPanel, "North");
    add(southPanel, "South");
    add(eastPanel, "East");
    add(westPanel, "West");
    add(centerPanel, "Center");
 }
}
mainMenu.java

import javax.swing.*;
import java.awt.*;

public class mainMenu extends JPanel {

JButton enterGarage = new JButton("Enter Parking Garage");
JButton leaveGarage = new JButton("Pay and Leave Garage");

private final int frameWidth = 1600;
private final int frameHeight = 1000;

public mainMenu() {
    setSize(frameWidth, frameHeight);
    setLayout(new GridBagLayout());
    setBackground(Color.CYAN);
    setVisible(true);
    initComponets();
}// end of mainMenu()

public void initComponets() {
    GridBagConstraints constraints = new GridBagConstraints();
    enterGarage.setSize(300, 300);

    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.ipadx = 50;
    constraints.ipady = 50;
    constraints.gridwidth = 3;
    constraints.gridheight = 10;
    constraints.gridx = 0;
    constraints.gridy = 0;
    this.add(enterGarage, constraints);

    leaveGarage.setSize(300, 300);

    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.ipadx = 50;
    constraints.ipady = 50;
    constraints.gridwidth = 3;
    constraints.gridheight = 10;
    constraints.gridx = 5;
    constraints.gridy = 0;
    this.add(leaveGarage, constraints);
  }// end of initComponets()
}// end of mainMenu Class.

这是一个指向mainMenu类应该是什么样子的链接,这是一个指向当我运行所有代码时实际显示的内容的链接
我的第一个问题是,我的“车库管理器”面板是由多个JPanel组成的面板,所以我尝试做的是什么呢?
-GarageManager上有多少个面板并不重要。只有顶层面板被交换,因此所有子面板也被交换。阅读Swing教程中的一节,了解一个可以帮助您入门的工作示例。因此,调试的想法是从小处开始,然后再进行大的调试。因此,首先得到一个使用两个面板的简单示例,这两个面板只包含一个组件。如果你有问题,那么你有一个简单的帖子。我们对阅读真实应用程序的所有代码不感兴趣。然后,一旦您理解了基本概念,就可以用更复杂的面板替换其中一个面板。一旦工作正常,请更换另一个面板。只要顶层设计正确,那么每个可交换面板上的组件数量就变得无关紧要了。*“这里有一个指向mainMenu类应该是什么样子的链接…”“该死的垃圾!我正要嵌入这些图像,但它不需要3200像素大小的图像来表示两个按钮或一个空白GUI。见camickr-好的,谢谢。这就是我所认为的子面板与顶级面板交换时的情况,但不是100%确定。因此,我尝试这样做的方式显然有问题。我将阅读更多关于如何使用布局的内容。