CardLayout,来自单独类的面板未显示

时间:2015-03-16 21:24:31

标签: java swing user-interface cardlayout

我正在为一个相当简单的棋盘游戏制作我的第一个GUI。除了游戏视图,我还需要主菜单和其他一些视图。不幸的是,早上我的GUI看起来比我看起来更丑,因为整个菜单结构都在一个类中。我正在使用卡片布局在不同的视图之间切换,所以我想我可以将这些视图分成不同的类。可悲的是,这是我遇到问题的地方。我得到的只是一个空白的窗口。

经过四个小时的研究后,我无法解决我的问题所以我问你们,你知道我的代码有什么问题吗?即使很难,我想我已经排除了明显的问题(没有在我的面板上添加面板卡布局,没有设置布局等)。我仍然认为问题必须是非常简单的。然而,我发现的一切都没有帮助。

所以对于企业来说,这是我的“基础GUI”

package teekkariloikka.gui;

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


public class TLGui extends JFrame {

    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 1000;
    private static final int HEIGHT = 800;

    private static final String MAINMENU = "mainmenu";
    private static final String GAME = "game";

    private JPanel basePanel;

    private GameViewPanel gameViewPanel;
    private MainMenuPanel mainMenuPanel;

    JFrame frame = new JFrame();

    //Layout
    private CardLayout cardL = new CardLayout();

    public TLGui(){

        //Frame stuff
        frame.setTitle("Teekkariloikka");
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);      

        //This is the panel where I add the ohter panels for menu and game
        basePanel = new JPanel();
        basePanel.setLayout(cardL);

        //Creating the panels I want to add. Excuse the confusing naming.
        gameViewPanel = new GameViewPanel(); 
        mainMenuPanel = new MainMenuPanel();

        //Adding the panels to the basePanel
        basePanel.add(mainMenuPanel, MAINMENU);
        basePanel.add(gameViewPanel, GAME);
        cardL.show(basePanel, MAINMENU);        

        //ADDING basePanel to the frame
        frame.add(basePanel);

        //Action listeners for buttons. Take a look at the classes.
        mainMenuPanel.setNewGameButtonListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                cardL.show(basePanel, GAME);
            }
        });

        gameViewPanel.setMainMenuButtonListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){{
                cardL.show(basePanel, MAINMENU);
            }
        }); 
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run(){
                new TLGui();
            }
        }); 
    }
}

然后是我的MainMenuPanel类。这是一项正在进行的工作,但至少应该显示按钮。我尽力翻译我用芬兰语写的所有内容(我的母语),我真的希望我不会对我的代码视而不见,以至于忘了一些东西。

package teekkariloikka.gui;

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

public class MainMenuPanel extends JPanel{

    private static final long serialVersionUID = 1L;

    //Buttons
    private JPanel panel;
    private JButton newGameButton;
    private JButton quitButton;

    //Font
    Font menuFont = new Font("Arial", Font.BOLD, 24);
    //Layout
    private GridBagLayout menuLayout = new GridBagLayout();
    private GridBagConstraints c1 = new GridBagConstraints();

    public MainMenuPanel(){

        panel = new JPanel();

        //For testing //TODO remove
        panel.setBackground(Color.BLACK);

        newGameButton = new JButton("New Game");
        quitButton = new JButton("Quit");

        //New Game -button
        newGameButton.setPreferredSize(new Dimension(200, 100));
        newGameButton.setFont(menuFont);

        //Quit-button
        quitButton.setPreferredSize(new Dimension(200, 100));
        quitButton.setFont(menuFont);

        panel.setLayout(menuLayout);
        c1.insets = new Insets(10, 10, 10, 10);
        c1.gridx = 0;
        c1.gridy = 1;
        panel.add(newGameButton, c1);
        c1.gridx = 0;
        c1.gridy = 2;
        panel.add(quitButton, c1);
    }

    //Idea: http://stackoverflow.com/questions/19104014/add-an-actionlistener-to-a-jbutton-from-another-class
    public void setNewGameButtonListener(ActionListener listener){
        newGameButton.addActionListener(listener);
    }

    public void setQuitButtonListener(ActionListener listener){
        quitButton.addActionListener(listener);
    }
}

和GameViewPanel类。也正在进行中,但与上一课一样,这应该看起来很好。

package teekkariloikka.gui;

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

public class GameViewPanel extends JPanel{

    private static final long serialVersionUID = 1L;

    private JButton lopetaButton;
    private JButton tallennaButton;
    private JButton menuButton;

    BorderLayout bordL = new BorderLayout();    

    public GameViewPanel(){

        JPanel gameViewPanel = new JPanel();

        gameViewPanel.setLayout(bordL);
        gameViewPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); 

        //Creating toolbar
        JToolBar toolbar = new JToolBar();
        toolbar.setFloatable(false);

        gameViewPanel.setLayout(bordL);
        gameViewPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); 
        gameViewPanel.add(toolbar, BorderLayout.PAGE_START);        

        //Buttons
        lopetaButton = new JButton("Lopeta");
        menuButton = new JButton("Päävalikko");
        tallennaButton = new JButton("Tallenna");

        //Adding buttons to game view toolbar
        toolbar.add(lopetaButton); //TODO Add functionality!
        toolbar.add(tallennaButton); //TODO Add functionality!
        toolbar.add(menuButton); //TODO Add functionality!
    }

    public void setMainMenuButtonListener(ActionListener listener){
        menuButton.addActionListener(listener);
    }
}

我希望有人可以指出我犯过的错误 - 不管我感觉多么愚蠢。

此外,任何其他反馈将受到高度赞赏。正如我所说,这是我第一次编写GUI,我必须说我时不时感到很失落。

3 个答案:

答案 0 :(得分:1)

 cardL.show(basePanel, "2");

小组的名称不是" 2"。

您可以定义变量MAINMENUGAME来表示每个面板的名称。

答案 1 :(得分:0)

MainMenuPanel窗格中,您可以创建JPanel ...

的实例
public MainMenuPanel(){
    // A new panel
    panel = new JPanel();

    //For testing //TODO remove
    panel.setBackground(Color.BLACK);

    newGameButton = new JButton("New Game");
    quitButton = new JButton("Quit");

    //New Game -button
    newGameButton.setPreferredSize(new Dimension(200, 100));
    newGameButton.setFont(menuFont);

    //Quit-button
    quitButton.setPreferredSize(new Dimension(200, 100));
    quitButton.setFont(menuFont);

    panel.setLayout(menuLayout);
    c1.insets = new Insets(10, 10, 10, 10);
    c1.gridx = 0;
    c1.gridy = 1;
    panel.add(newGameButton, c1);
    c1.gridx = 0;
    c1.gridy = 2;
    panel.add(quitButton, c1);
    // But what do you do with panel??
}

但永远不要将其添加到MainMenuPanel ...

您在GameMenu ...

中也会做同样的事情
 public GameViewPanel(){

    // Create JPanel...
    JPanel gameViewPanel = new JPanel();

    gameViewPanel.setLayout(bordL);
    gameViewPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); 

    //Creating toolbar
    JToolBar toolbar = new JToolBar();
    toolbar.setFloatable(false);

    gameViewPanel.setLayout(bordL);
    gameViewPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); 
    gameViewPanel.add(toolbar, BorderLayout.PAGE_START);        

    //Buttons
    lopetaButton = new JButton("Lopeta");
    menuButton = new JButton("Päävalikko");
    tallennaButton = new JButton("Tallenna");

    //Adding buttons to game view toolbar
    toolbar.add(lopetaButton); //TODO Add functionality!
    toolbar.add(tallennaButton); //TODO Add functionality!
    toolbar.add(menuButton); //TODO Add functionality!
    // But never do anything with it...
}

我的建议是,不要。 MainMenuPanelGameMenu都来自JPanel,只需直接在这些面板上创建UI。似乎没有必要使用这些“内部”面板......

答案 2 :(得分:0)

好吧,当我转向其他事情时,我实际上已经找到了答案。当我研究一些布局设计时,我看到了this段代码,并且想到在我的面板类中使用“this”引用。

所以我从MainMenuPanelGameviewPanel

中删除了这段代码
// A new panel
panel = new JPanel();

然后用this替换对该面板的引用。 MainMenuPanel的示例:

this.setLayout(menuLayout);
c1.insets = new Insets(10, 10, 10, 10);
c1.gridx = 0;
c1.gridy = 1;
this.add(newGameButton, c1);
c1.gridx = 0;
c1.gridy = 2;
this.add(quitButton, c1);

我不能说我完全理解为什么这种方法有效,而另一方面却没有,但是现在,我需要一个可行的解决方案。 我希望这对其他人有一天也有用。