java中的GUI线程 [英] GUI threading in java

查看:142
本文介绍了java中的GUI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java编写一个简单的游戏。基本结构是单个 JFrame ,我在不同时间添加/删除了不同的 JPanels 。在启动时,有一个 JPanel 这是一个基本菜单(开始游戏,高分等)。按下开始按钮后,它会切换到一个水平选择器面板,其中有三个按钮可选择游戏的难度级别。按下三个按钮中的任何一个后,它会切换到另一个面板,该面板将显示三秒倒计时,然后显示实际游戏。所有三个按钮都调用相同的方法,只是传入了不同的难度值。

I'm trying to code a simple game in Java. The basic structure is a single JFrame with different JPanels that I add/remove at different times. At startup, there is a JPanel that's a basic menu (start game, high scores, etc). Once the "Start" button is pressed it switches to a level selector panel with three buttons to select the difficult level of the game. Once any of the three buttons is pressed, it switches to another panel that will displays a three second countdown, then the actual game. All three buttons call the same method, just with a different difficulty value passed in.

我有所有单独的部分工作正常,但我遇到过渡的麻烦从水平选择面板到倒计时。如果我不使用线程,屏幕会在按下按钮时冻结,并且不会切换到新面板。我已经尝试过搞乱线程了,但我对它们知之甚少,并且只取得了有限的成功(我已经得到它所以它会成功切换一些时间,但不是一贯的)。

I have all the separate pieces working fine, but I'm having troubles with the transition from the level selection panel to the countdown. If I don't use threads the screen freezes on button press and does not switch to the new panel. I've tried messing around with threads, but I don't know that much about them and have only had limited success (I've got it so it will successfully switch some of the time, but not consistently).

在代码方面,在级别选择面板中,我有类似于按下按钮点击的内容:

In terms of code, in the level selection panel I have something like this listening for button clicks:

private class ButtonClickedListener implements ActionListener {
    public void actionPerformed(ActionEvent evt) {  
        gui.newLevel(1);
    }
}

代替 gui.newLevel(1)我已经搞砸了开始新线程并从中调用方法。

where in place of just gui.newLevel(1) I've messed around with starting new threads and calling the method from them.

newLevel()方法如下:

getContentPane().removeAll();

levelPanel = new LevelPanel(levelNum, this);
add(levelPanel);
validate();

levelPanel.start();

从开始菜单切换时我使用非常相似的代码 JPanel 到关卡选择器面板(再次,按钮上有一个 ActionListener ),效果很好。

I use very similar code when switching from the start menu JPanel to the level selector panel (again, with an ActionListener on the buttons), which works just fine.

LevelPanel的 start()方法初始化新 JPanel 的值,并在屏幕上显示倒计时(目前有下面的代码,虽然我在显示实际游戏之前在 newLevel()方法中混淆了这样的东西:

LevelPanel's start() method initializes values for the new JPanel and displays the countdown on screen (currently with the following code, although I messed with putting something like this in the newLevel() method instead) before displaying the actual game:

try {
    Thread.sleep(1000);
    //update countdown number
    validate();
    repaint();
    Thread.sleep(1000);
    //update countdown number
    validate();
    repaint();
    Thread.sleep(1000);
    //update countdown number
    validate();
    repaint();
} catch (Exception e) {
    System.out.println(e);
}

//start game

我真的很感激任何帮助使这个代码工作,我很确定某种线程是要走的路,但我不太确定在哪里/如何。任何建议和/或代码示例都会很棒!

I would really appreciate any help getting this code to work, and I'm pretty sure some sort of threading is the way to go but I'm not quite sure where/how. Any suggestions and/or code samples would be great!

提前致谢!

编辑: 我最后用计时器而不是 Thread.sleep()来重写倒计时,这解决了问题的一部分,其余部分我最终想通了与GUI的东西完全无关,这就是为什么我不想在一开始就检查它。

I ended up rewriting the countdown using a timer instead of Thread.sleep(), which fixed part of the problem and the rest of it I eventually figured out and was entirely unrelated to GUI stuff, which is why I didn't think to check it in the first place.

推荐答案

从来没有真的永远不要在EDT期间使用 Thread.sleep(1000); ,这个代码导致G​​UI冻结是un_resposible,直到新事件调用EDT或鼠标悬停在这个容器上也可以

never really never use Thread.sleep(1000); during EDT, this code caused freeze on GUI is un_resposible, untill a new event invoke EDT or mouse hover over can alive this container too

1)有两种方法可以通过工具来实现Swing GUI中的任何事件

1) there are two ways how to dealy any event(s) in the Swing GUI, by implements


  • Swing Timer

使用<$ c $延迟c> Thread.sleep(1000); 在 SwingWorker

这篇关于java中的GUI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆