gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:46:41 25 4
gpt4 key购买 nike

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

经过四个小时的研究,我还没能解决我的问题,所以我问你们,你们知道我的代码有什么问题吗?即使很困难,我认为我已经排除了明显的问题(没有使用卡片布局将面板添加到我的面板中,没有设置布局等),我仍然认为问题一定是非常简单的。但我发现没有任何帮助。

对于企业来说,这是我的“基本 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来表示每个面板的名称。

关于java - CardLayout,来自单独类的面板未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29087190/

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