- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 Java 虚拟宠物游戏,当前菜单面板工作正常,按钮正在加载和操作,但是当我单击本应启动到 GamePanel 面板的新游戏按钮时,我收到 nullpointerException。
这是构建原始框架和要切换的 Jpanel 的主类。
public class MainFrame extends JPanel {
public JPanel mainPanel;
public CardLayout cl;
private final GamePanel gamePanel;
private final MenuPanel menuPanel;
/**
* Constructs the main panel to be used to switch between panels.
*
*/
public MainFrame() {
// creates a new panel to add panels to.
cl = new CardLayout();
// panel to be used as a main switch.
mainPanel = new JPanel();
Dimension size = getPreferredSize();
size.width = 600;
size.height = 600;
setPreferredSize(size);
setBackground(Color.BLACK);
add(mainPanel);
gamePanel = new GamePanel();
menuPanel = new MenuPanel();
// sets layout
mainPanel.setLayout(cl);
mainPanel.add(menuPanel, "menuPanel");
mainPanel.add(gamePanel, "gamePanel");
}
public void changePanel(String name) {
cl.show(mainPanel, name);
}
/**
* Main frame used by the game.
*
* @param args
*/
public static void main(String[] args) {
MainFrame game = new MainFrame();
JFrame frame = new JFrame("Main Window");
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
}
}
这是我的 MenuPanel 类中的代码,它应该访问 changePanel() 方法。
public class MenuPanel extends JPanel {
MainFrame mf;
public MenuPanel() {
this.mf = mf;
Dimension size = getPreferredSize();
size.width = 600;
size.height = 600;
setPreferredSize(size);
ImageIcon menuIcon = new ImageIcon("C:\\Programming\\NetBeansProjects\\PDCMain\\src\\Data\\the_menu_title2.png");
JLabel menuLbl = new JLabel();
menuLbl.setIcon(menuIcon);
JButton newGameBtn = new JButton("New Game");
JButton loadGameBtn = new JButton("Load Game");
JButton helpBtn = new JButton("Instructions");
JButton exitBtn = new JButton("Exit");
newGameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("New Game");
mf.changePanel("gamePanel");
}
});
loadGameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
helpBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showHelp();
}
});
exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = 0;
gc.weighty = 0.1;
gc.gridx = 0;
gc.gridy = 1;
add(menuLbl, gc);
gc.gridx = 0;
gc.gridy = 2;
add(newGameBtn, gc);
gc.gridx = 0;
gc.gridy = 3;
add(loadGameBtn, gc);
gc.gridx = 0;
gc.gridy = 4;
add(helpBtn, gc);
gc.gridx = 0;
gc.gridy = 5;
add(exitBtn, gc);
}
public void showHelp(){
String[] help = new String[100];
try {
BufferedReader br = new BufferedReader(new FileReader("instruct.txt"));
String line = "";
int i = 0;
while((line = br.readLine()) != null) {
help[i] = line;
i++;
}
JOptionPane.showMessageDialog(null, help);
} catch(IOException ex) {
System.err.println("IOException Error: " + ex.getMessage());
}
}
这是游戏面板
public class GamePanel extends JPanel{
private ButtonsPanel buttonsPanel;
private GraphicsPanel graphicsPanel;
private final JPanel gamePanel;
public GamePanel(){
super();
this._initGUI();
gamePanel = new JPanel();
Dimension size = getPreferredSize();
size.width = 600;
size.height = 600;
setPreferredSize(size);
setBackground(Color.RED);
}
private void _initGUI(){
this.buttonsPanel = new ButtonsPanel();
this.graphicsPanel = new GraphicsPanel();
this.setLayout(new BorderLayout());
this.add(buttonsPanel, BorderLayout.SOUTH);
this.add(graphicsPanel, BorderLayout.CENTER);
}
public void run(){
graphicsPanel.run();
}
}
这是我第一次尝试从头开始构建 GUI 并使用卡片布局。我看不到它将在哪里分配空值,因为面板是在用于切换的主面板中声明的,有什么想法吗?
最佳答案
花点时间看看这段代码...
public static class MenuPanel extends JPanel {
MainFrame mf;
public MenuPanel() {
this.mf = mf;
基本上,您正在分配 ( this
) mf
至this.mf
,但自从 mf
已经是null
, this.mf
将保持null
...
您应该传递 MainFrame
的引用给您MenuPanel
现在,因为我讨厌将 UI 组件的引用传递给程序的其他部分,而程序的其他部分应该不知道 UI 的其余部分并且不直接控制它们,所以我建议不要传递 MainFrame
像这样让人眼花缭乱。
相反,创建一个 interface
它提供了您期望的功能 MenuPanel
应该允许执行( newGame
, loadGame
, showInstructions
, exit
),并允许执行此 interface
(可能是 MainFrame
)来处理它
然后您可以将此接口(interface)的实例传递给 MenuPanel
...这限制了类对不需要这些类功能的其他类的暴露,并解耦了您的代码,以便您可以将接口(interface)的任何旧实例传递给类,而无需重写代码的整个部分...
更新了基本概念
从某种游戏 Controller 界面开始,例如......
public interface GameController {
public void newGame();
public void loadGame();
public void showInstructions();
public void exit();
}
这描述了针对此接口(interface)的实现可以采取的操作。
使用MainFrame
并实现GameController
...
public class MainFrame extends JPanel implements GameController {
//...
public void newGame() {
}
public void loadGame() {
}
public void showInstructions() {
}
public void exit() {
}
更改MenuPane
需要 GameController
的实例当它构建完成时...
public class MenuPanel extends JPanel {
private GameController controller;
public MenuPanel(GameController gameController) {
this.controller = gameController;
//...
现在,当您处理用户交互时,您会将请求传递给 Controller ......
newGameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("New Game");
controller.newGame();
}
});
这样,MenuPanel
不知道新游戏是如何创建的,它只知道它将被照顾......
现在,您需要更新 newGame
MainFrame
中的方法做某事,例如...
public void newGame() {
// Setup the new game...
cl.show(mainPanel, "gamePanel");
}
这将所有责任与 GameController
的实现完全 Hook 。决定如何进行某些操作,使流程解耦,因此如果您更改 GameController
的实现对于其他一些类,您无需担心更新 MenuPanel
,因为它只对 GameController
感兴趣。 ...很好;)
关于java - Swing CardLayout 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26459688/
嗨,我正在使用表单做一个小项目。目前我使用了 netbeans,但我的类变得非常复杂,因为所有 jpanel ie 卡都在一个类中,这是一个框架。我被要求简化。 我的问题是如果我把一个 jpanel
是否有一种优雅的方法可以用指向另一个对象的新引用替换添加到 CardLayout 中的对象的引用。我不知道现在我是否想得好,但我只想得到反馈。 最佳答案 而不是 bankAccPanel 直接添加将其
在我的项目中,我在 JFrame 中有一个 JPanel (bottomPanel) 需要交换,因此我在 BottomPanel 上使用 CardLayout。 在我的父 JFrame 中,我基本上在
我想熟悉 CardLayout,所以我正在制作一个模拟游戏菜单。该菜单应该有三个按钮,但布局部分很简单。 所以,我想做的是用带有三个按钮的菜单启动它。单人游戏按钮应该将用户看到的内容更改为单个按钮,这
我是java初学者。在第二个卡片面板中,用户名和密码对齐不正确。有什么办法可以解决吗?我还想知道使用多个框架的缺点是什么。 import java.awt.*; import java
我正在尝试编写一个简短的程序,它有一个带有标题的主页和 4 个按钮,其中 3 个按钮将离开主屏幕并转到一个新页面,您可以在其中相应地输入信息。我开始使用拖放编辑器,但通过论坛发现我应该使用卡片布局,而
这是我的 SSCE(尽管分为三个单独的类(class))。 启动.java public class Startup { public static void main(String args
每当我感觉自己已经学到了很多关于 Java 的知识时,我就会突然遇到一堵砖墙,让我感觉自己像个十足的白痴。今天的大问题是 CardLayout。至少我终于通过在字段列表中实例化 buttonsCard
我正在使用 Eclipse 和 Window Builder。但是我无法在 Window Builder 中使用卡片布局。所以我开始输入自己的代码,现在我卡在显示第一张卡上,该卡显示正确,但在单击 j
有没有办法告诉使用 CardLayout 的 JPanel 在哪里添加组件? 假设我在框架中央有一个这样的面板,并且我想在该面板内显示 3 个组件。这可能吗? 最佳答案 当然,这很容易。只需将 JPa
我在 CardLayout 中放置了一堆面板,其中第 n 面板取决于 (n - 1)th 面板。由于使用 CardLayout,您必须事先初始化并添加所有面板。因此,它使得管理状态变得比必要的更加困难
我制作了一个简单的程序,几乎可以完成所有操作,除了返回到第一个面板的开关之外。因此,如果我单击“Druck”按钮,它将把钱从一个银行帐户转移到另一个银行帐户,然后切换到第二个面板,其中显示两个帐户的余
我正在关注 YouTube 上关于 CardLayout 的教程。我下载了the original code ,效果很好。但对于我试图制作的程序,我需要一个单独的类来运行应用程序(即仅具有 main
我有一个已经运行并可以运行的游戏,我想为其添加一个标题屏幕。我正在尝试添加 CardLayout 以便在游戏和标题屏幕之间轻松切换。我当前的问题是没有显示任何内容。这是一张图片:/image/AooM
我创建了使用多个面板的应用程序,所以我选择了cardLayout。问题是,当执行以下代码时,在 UserInterface 方法 SinglePlayer() 中会发生一些奇怪的事情。我使用命令 fr
我正在为一个相当简单的棋盘游戏制作我的第一个 GUI。除了游戏 View 之外,我还需要主菜单和其他一些 View 。不幸的是,我的 GUI 看起来比早上更难看,因为整个菜单结构都在一个类中。我使用卡
这是我第一次尝试使用 Cardlayout 运行代码。这是 add 方法的异常(空指针)。不过,我也尝试弄清楚如何设计卡片布局,例如卡片并排或一张在下。我更喜欢后者。我已经尝试更改我的代码并阅读有关类
所以我有一个带有 CardLayout 的 JPanel。正如预期的那样,此 CardLayout 管理框架中面板的切换。切换是通过两个按钮完成的:“后退”和“下一步”。 我想知道当它位于最后一张卡上
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我知道这个问题之前已经被问过,但我似乎无法让答案起作用,也不理解它们。 我想做的是交换按钮单击的面板。这是我的主要功能: public class CreateWindow extends JFram
我是一名优秀的程序员,十分优秀!