简体   繁体   English

JFrame 在使用按钮创建后不绘画

[英]JFrame isn't painting after creating it with a button

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.当我正常调用我的项目的 play() 方法时,它会绘制第一级。 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?有人可以帮我解决我的问题吗?

Please see you have play() method used inside of play() method.请查看您在play()方法中使用了play()方法。 Please try to change ones name.请尝试更改姓名。 Maybe you are just calling wrong method inside of actionPerformed() method.也许您只是在actionPerformed()方法中调用了错误的方法。

If that won't work, you can try deleting whole play(){...} and actionPerformed(){...} methods from your snippet and delete line:如果这不起作用,您可以尝试从代码片段中删除整个play(){...}actionPerformed(){...}方法并删除行:

startButton.addActionListener(this);

Then you can just add this into your prepare() function:然后您可以将其添加到您的 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:它将向您的按钮添加新的 ActionListener :

  • disposes titlescreen (you do not need it running in background)处理titlescreen(你不需要它在后台运行)
  • runs new PlayGame object in new thread (to avoid lags especially if you have any timer in your game)在新线程中运行新的 PlayGame object(以避免滞后,特别是如果您的游戏中有任何计时器)

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:您每次创建新的 PlayGame object 时都在使用这两种方法,因此您应该将它们放入 PlayGame class 的构造函数中,因此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM