Jframe用按钮创建它之后,没有绘画

发布于 2025-02-11 09:22:40 字数 1249 浏览 1 评论 0原文

我想通过单击按钮来打开标题屏幕并打开游戏。当我调用Play() - 正常项目的方法时,它会绘制第一级。但是,当我用按钮调用它时,会出现一个白屏,我不知道为什么。这是一个代码段:

package com.company;

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

public class Game implements ActionListener {
    private String csvFile;
    private JFrame titlescreen;
    private JButton startButton;

public Game(String csvFile){
    this.csvFile = csvFile;
}

public void prepare(){
    titlescreen = new JFrame("Titlescreen");
    titlescreen.setSize(100,100);
    startButton = new JButton("Start Game");
    startButton.addActionListener(this);

    titlescreen.add(startButton);
    titlescreen.setVisible(true);
}

public void play(){
    titlescreen.setVisible(false);
    if(csvFile!= null) {
        LevelBuilder levelBuilder = new LevelBuilder(csvFile, ";", ",");

        AllObstacles ao = new AllObstacles();
        ao.addObstaclesOfString(levelBuilder.getLvlString(), ";", ",");
        Goal g = new Goal(125, 125, 250, 250);
        PlayGame c = new PlayGame(ao, g, levelBuilder);
        c.create();
        c.play();
    }
 }

@Override
public void actionPerformed(ActionEvent e) {
    startButton.dispatchEvent(e);
    play();
}
}

有人可以帮助我解决我的问题吗?

I want to have titlescreen and open a game by clicking a button. When i call the play()-method of my project normally, it draws the first level. But when i call it with a button, a white screen appeares and I dont know why. Here is a code snippet:

package com.company;

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

public class Game implements ActionListener {
    private String csvFile;
    private JFrame titlescreen;
    private JButton startButton;

public Game(String csvFile){
    this.csvFile = csvFile;
}

public void prepare(){
    titlescreen = new JFrame("Titlescreen");
    titlescreen.setSize(100,100);
    startButton = new JButton("Start Game");
    startButton.addActionListener(this);

    titlescreen.add(startButton);
    titlescreen.setVisible(true);
}

public void play(){
    titlescreen.setVisible(false);
    if(csvFile!= null) {
        LevelBuilder levelBuilder = new LevelBuilder(csvFile, ";", ",");

        AllObstacles ao = new AllObstacles();
        ao.addObstaclesOfString(levelBuilder.getLvlString(), ";", ",");
        Goal g = new Goal(125, 125, 250, 250);
        PlayGame c = new PlayGame(ao, g, levelBuilder);
        c.create();
        c.play();
    }
 }

@Override
public void actionPerformed(ActionEvent e) {
    startButton.dispatchEvent(e);
    play();
}
}

Can someone help me with my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2025-02-18 09:22:40

请查看play() play()方法中使用的方法。请尝试更改名称。也许您只是在ActionPerformed()方法的内部调用错误的方法。

如果那不起作用,您可以尝试删除整个play(){...} and action performed(){...}从您的代码和删除行中:

startButton.addActionListener(this);

然后,您只需将其添加到您的准备()函数中:

startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(csvFile!= null) {
                   titlescreen.dispose();
                   LevelBuilder levelBuilder = new LevelBuilder(csvFile, ";", ",");
                   AllObstacles ao = new AllObstacles();
                   ao.addObstaclesOfString(levelBuilder.getLvlString(), ";", ",");
                   Goal g = new Goal(125, 125, 250, 250);
                   SwingUtilities.invokeLater(() -> new PlayGame(ao, g, levelBuilder);
            }
        });

它将添加到您的按钮新的ActionListener:

  • Depers titlesCreen(您不需要在后台运行)
  • 在新线程中运行新的PlayMage对象(避免滞后,尤其是在您 此外,游戏中有任何计时器)

c.create()
c.play()

您每次创建新游戏对象时都使用这两种方法,因此您应该将它们放入游戏游戏类的构造函数中,因此:

public PlayGame(){
//your current code
create();
play();
}

Please see you have play() method used inside of play() method. Please try to change ones name. Maybe you are just calling wrong method inside of actionPerformed() method.

If that won't work, you can try deleting whole play(){...} and actionPerformed(){...} methods from your snippet and delete line:

startButton.addActionListener(this);

Then you can just add this into your prepare() function:

startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(csvFile!= null) {
                   titlescreen.dispose();
                   LevelBuilder levelBuilder = new LevelBuilder(csvFile, ";", ",");
                   AllObstacles ao = new AllObstacles();
                   ao.addObstaclesOfString(levelBuilder.getLvlString(), ";", ",");
                   Goal g = new Goal(125, 125, 250, 250);
                   SwingUtilities.invokeLater(() -> new PlayGame(ao, g, levelBuilder);
            }
        });

It will add to your button new ActionListener that:

  • disposes titlescreen (you do not need it running in background)
  • runs new PlayGame object in new thread (to avoid lags especially if you have any timer in your game)

Moreover:

c.create()
c.play()

you are using those two methods everytime you create new PlayGame object, so you should put them into constructor of PlayGame class, so:

public PlayGame(){
//your current code
create();
play();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文