簡體   English   中英

CardLayout,來自單獨類的面板未顯示

[英]CardLayout, panels from separate class not showing

我正在制作一個非常簡單的棋盤游戲的第一個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,我必須時常感到非常迷茫。

 cardL.show(basePanel, "2");

面板的名稱不是“ 2”。

您定義變量MAINMENUGAME來代表每個面板的名稱。

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執行相同的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擴展的,只需直接在這些面板的GameMenu創建UI。 似乎不需要這些“內部”面板...

好吧,當我繼續其他事情時,我實際上自己就找到了答案。 在研究一些布局設計時,我看到了這段代碼,並有了在面板類中使用“ 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);

我不能說我完全理解為什么這樣做有效,反之則不行,但是目前,我只需要一個可行的解決方案。 我希望這一天對其他人也有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM