Java Swing使用CardLayout和" Card"由多个JPanel组成

时间:2017-04-05 19:17:56

标签: java swing user-interface cardlayout

我正在尝试将cardlayout用于2个单独的JPanels,我将在卡布局中使用的第一个JPanel是我的"主菜单"面板,它是一个简单的JPanel,上面有2个按钮。

我在JPanel中使用的第二个cardlayout是我的"车库经理"面板,实际上是一个由{5}组成的JPanel

我的第一个问题是,自从我的" Garage Manager"以来,我正在尝试做什么。面板是由多个JPanel组成的面板?

如果可以的话,你能解释或指出我正确的方向,为什么我的代码没有显示任何一张"卡"在我的cardlayout?

由于我是新来的,所以我只会在此帖子上附上一张图片,所以我附上了我的" GarageManager"面板应该看起来像。

我会尝试附上一张我的" mainMenu"面板应该看起来和我在评论中运行我的代码或发布后在这篇帖子上实际获得的灰色框架的图像。

我是编程和摇摆的新手,所以对我的代码的任何建设性批评或帮助都非常感激。

What garageManager should look like

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.

0 个答案:

没有答案