gpt4 book ai didi

java - CardLayout 在 Java 中显示空白 JPanel

转载 作者:行者123 更新时间:2023-12-01 10:37:05 26 4
gpt4 key购买 nike

我搜索了不同的教程并查看了 CardLayout 和 JPanel 的类配置文件,但我似乎无法显示我的窗口。目前,它打开一个具有正确尺寸和标题的框架,但实际容器中没有任何内容。

这是我的代码(P.S.我知道这是一团糟)

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class Casino extends JFrame implements ActionListener {

private JButton start, settings, scenario, music;


/**
* Constructor method
*/

public Casino(){

JPanel mainUI, startUI, settingsUI, scenarioUI, blackjackUI, oddorevenUI, tcmUI, overorunderUI, slotsUI;
JPanel menus = new JPanel(new CardLayout());


CardLayout GUI = (CardLayout) menus.getLayout();
mainUI = new JPanel();
getContentPane().add(mainUI);
mainUI.setBackground(new Color(53, 9, 9));

//Background items
JLabel title = new JLabel(new ImageIcon("title.png"));
title.setBounds(0,-280,780,700);
mainUI.add(title);

JLabel border = new JLabel(new ImageIcon("mainscreenborder.png"));
border.setBounds(0, 180, 780, 700);
mainUI.add(border);

//Main menu buttons
settings = new JButton();
ImageIcon s = new ImageIcon("settings-button.png");
settings.setBounds(320, 200, 122, 63);
settings.setIcon(s);
mainUI.add(settings);

music = new JButton();
ImageIcon m = new ImageIcon("music-button.png");
music.setBounds(320, 268, 122, 63);
music.setBackground(new Color(53, 9, 9));
music.setIcon(m);
mainUI.add(music);

scenario = new JButton();
ImageIcon sc = new ImageIcon("scenario-button.png");
scenario.setBounds(320, 336, 122, 63);
scenario.setBackground(new Color(53, 9, 9));
scenario.setIcon(sc);
mainUI.add(scenario);

start = new JButton();
ImageIcon st = new ImageIcon("start-button.png");
start.setBounds(320, 404, 122, 63);
start.setBackground(new Color(53, 9, 9));
start.setIcon(st);
mainUI.add(start);


menus.add(mainUI, "Main Menu");

GUI.show(menus, "Main Menu");




setSize(780, 700);
setResizable(false);
setLayout(GUI);
setTitle("White Lily Casino");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e){

}

public static void main(String[] args) {
// TODO Auto-generated method stub

Casino wlc = new Casino();

}
}

注意:之前当我使用 Container c 方法而不是使用 JPanel 和 CardLayout 时,它可以工作。我现在尝试将其切换为卡片布局,因为我想使用按钮导航到多个屏幕

最佳答案

尝试将 mainUI 添加到 JFrame

getContentPane().add(mainUI)

add(mainUI)

关于java - CardLayout 在 Java 中显示空白 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34622388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com