【问题标题】:CardLayout showing blank JPanel in JavaCardLayout 在 Java 中显示空白 JPanel
【发布时间】:2016-01-05 22:28:29
【问题描述】:

我搜索了不同的教程并查看了 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 之前,它可以工作。我现在正在尝试将其切换到卡片布局,因为我想使用按钮导航到多个屏幕

【问题讨论】:

  • 我没有看到你添加面板,getContentPane().add(mainUI)
  • 你为什么使用setBounds()而不是Layout Managers的组合?
  • 我没有看到 menus 被添加到什么地方?
  • 考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将减少混乱并获得更好的响应
  • 我还要添加菜单是什么意思?

标签: java swing jpanel cardlayout


【解决方案1】:

尝试将mainUI 添加到JFrame

getContentPane().add(mainUI)

add(mainUI)

【讨论】:

  • getContentPane().add(mainUI) 不起作用,但 add(mainUI) 起作用(在显示内容方面)。为什么一切都没有在 setBounds 设置的位置?是否有与我应该使用的 JPanel 的 setBounds 类似的方法?
  • 您在使用 absolute 定位时使用 setBounds() 方法,这仅在您将容器的布局设置为 null 时有效
  • 如果您希望您的组件使用边界然后mainUI.setLayout(null); 而不是使用卡片布局。这将通过 setBounds 显示所有内容
猜你喜欢
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 2013-04-15
  • 2016-04-09
  • 2011-07-18
相关资源
最近更新 更多