使用cardlayout,有一个我无法弄清楚的问题

时间:2014-03-09 19:10:14

标签: java swing user-interface layout-manager cardlayout

所以稍早一些人建议我使用CardLayout作为Java游戏的菜单。 我读了一下它并开始编码。它非常好用! 我只有这个问题,我无法弄清楚: 在我的第一个JPanel我有3个JButtons +一个JLabel(图片,我的徽标),在我的第二个JPanel上,我有相同但按钮上的文字不同。 问题是我的徽标在第一个JPanel启动时没有显示(按钮确实),当我转到第二个JPanel时,徽标确实出现在按钮上(它应该如何) 所以它是:Jpanel1是新游戏,Highscores,退出 - >新游戏导致3个难度按钮。

我的代码的简短版本:

package labyrinthproject.View;


import labyrinthproject.Controller.Control;
import labyrinthproject.model.Spel.Labyrint;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MainMenu {
ImageIcon logoImg = new ImageIcon("Resources\\mainlogo.png");
JFrame frame = new JFrame("A-Maze-Ing");
JPanel panelCont = new JPanel();
JPanel panelMenu = new JPanel();
JPanel panelOption = new JPanel();
JButton playButton = new JButton ("Nieuw Spel");
JButton highscoresButton = new JButton ("Highscores");
JButton sluitButton = new JButton ("Sluiten");
JButton maze1 = new JButton("Makkelijk");
JButton maze2 = new JButton("Normaal");
JButton maze3 = new JButton("Hardcore");
JLabel logo = new JLabel();
CardLayout cl = new CardLayout();

public void mainmenu()   {

    //Cardlayout
    panelCont.setLayout(cl);

    // 800x600 Frame
    frame.setSize(new Dimension(800, 600));
    frame.getContentPane().setBackground(new Color(14,36,69));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setMinimumSize(frame.getMinimumSize());

    //logo
    logo.setIcon(logoImg);
    logo.setAlignmentX(JComponent.CENTER_ALIGNMENT);

    //690x540 Panel ( grootte van map );
    Dimension expectedDimension = new Dimension(690, 540);
    panelMenu.setLayout(new BoxLayout(panelMenu, BoxLayout.Y_AXIS));
    panelMenu.setPreferredSize(expectedDimension);
    panelMenu.setMaximumSize(expectedDimension);
    panelMenu.setMinimumSize(expectedDimension);
    panelMenu.setBackground(new Color(14, 36, 69));       //14, 36, 69
    panelMenu.add(logo);
    panelMenu.add(playButton);
    panelMenu.add(highscoresButton);
    panelMenu.add(sluitButton);
    sluitButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);

    //Optionpanel
    panelOption.setLayout(new BoxLayout(panelOption,BoxLayout.Y_AXIS));
    panelOption.setPreferredSize(expectedDimension);
    panelOption.setMaximumSize(expectedDimension);
    panelOption.setMinimumSize(expectedDimension);
    panelOption.setBackground(new Color(14,36,69));
    panelOption.add(logo);
    panelOption.add(maze1);
    panelOption.add(maze2);
    panelOption.add(maze3);
    maze1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    maze2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    maze3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    terug.setAlignmentX(JComponent.CENTER_ALIGNMENT);

    //maze1
    panelMaze1.setBackground(new Color(255,0,0)); //test

    //maze2
    panelMaze2.setBackground(new Color(0,0,255)); //test

    //maze3
    panelMaze3.setBackground(new Color(0,255,0)); //test

    //playButton
    playButton.setFont(new Font("Old English Text MT", Font.BOLD, 40));
    playButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cl.show(panelCont, "2");
        }
    });

    //highscorelabel
    highscoresButton.setFont(new Font("Old English Text MT", Font.BOLD, 40));

    //sluitButton
    sluitButton.setFont(new Font("Old English Text MT", Font.BOLD, 40));
    sluitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    //makkelijk button
    maze1.setFont(new Font("Old English Text MT", Font.BOLD, 40));
    maze1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cl.show(panelCont, "3");
        }
    });

    //normaal button
    maze2.setFont(new Font("Old English Text MT", Font.BOLD, 40));
    maze2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cl.show(panelCont, "4");
        }
    });

    //hardcore button
    maze3.setFont(new Font("Old English Text MT", Font.BOLD, 40));
    maze3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cl.show(panelCont, "5");
        }
    });

    panelCont.add(panelMenu, "1");
    panelCont.add(panelOption, "2");
    panelCont.add(panelMaze1, "3");
    panelCont.add(panelMaze2, "4");
    panelCont.add(panelMaze3, "5");
    cl.show(panelCont, "1");


    frame.add(panelCont, BorderLayout.CENTER);
    frame.setVisible(true);
 }
}

提前谢谢! 真诚的初学java程序员!

1 个答案:

答案 0 :(得分:2)

  

“问题是我的徽标在第一个JPanel启动时没有显示(按钮确实),当我转到第二个JPanel时,徽标确实出现在按钮”

您必须了解组件的一件事是它们只能在父容器上。如果您尝试使用相同的组件两次,则只有您添加它的最后一个容器才会收到该组件。

您只有一个 JLabel icon,但是您尝试将其添加两次,一次添加到panelMenu,一次添加到paneOption,因此panelOption是唯一接受它的人。

JLabel logo = new JLabel();
panelMenu.add(logo);
panelOption.add(logo);

简单修复就是创建两个 JLabels。嘿可以使用相同的ImageIcon,因为ImageIcon不是组件。