JButton和一个新的框架 [英] JButton and a new Frame

查看:68
本文介绍了JButton和一个新的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我在正在制作的游戏中遇到了JButton的问题。

我有一个mainFrame就是我们玩游戏的地方。在这个类中我有一个主要的方法来测试它。我们还有一个第一个窗口。在第一个窗口我们有一个按钮播放,当我们推它时,我们显示mainFrame。


问题:当我按下按钮时它会显示mainFrame但是所有的窗口都是白色的。(我应该看到一些图片和按钮)


在actionPerformed方法中我复制粘贴mainFrame主类的代码,它正常工作,所以我猜它的作用也会在actionPerformed中正确,但是没有。

谢谢

解决方案

你的按钮的actionPerformed()方法在AWT线程使线程

忙,没有时间去做所有的绘图操作。当你从main()方法做了同样的事情时,你在

中做了帧初始化,另一个线程让AWT线程自由地完成所有绘图。


永远不要让AWT线程忙(太长时间),即你的mainFrame设置

和另一个线程中的东西。看看 SwingUtilities 线程

课程。


亲切问候,


Jos


谢谢你,但我现在不想学习线程。我没有时间。

有没有办法关闭第一个窗口(不是setVisible(false)但是关闭它)然后从另一个类运行main?(所有这些都在Action中执行)


< blockquote>


谢谢你,但我现在不想学习线程。我没有时间。

有没有关闭第一个窗口的方法(不是setVisible(false)但是关闭它)然后从另一个类运行main?(所有这些在Action中执行)



只需使用SwingUtilities类;现在你有这样的事情:

展开 | 选择 | Wrap | 行号


Hello. I got a problem with a JButton in a game that i am making.
I have a mainFrame that is where we play the game.In this class i have a main method in order to test it.We also have a first window.In first window we have a button play and when we push it , we show the mainFrame.

The problem: When i push the button it shows me the mainFrame but all the window is white.( i should see some pictures and buttons)

In actionPerformed method i copy paste the code of the main class of mainFrame, which works correctly, so i guess that it will works also correct in actionPerformed,but no.
thanks

解决方案

Your button''s actionPerformed() method runs in the AWT thread so that thread
is busy and doesn''t have any time left to do all your drawing operations. When
you did the same from your main() method you did the frame initialization in
another thread leaving the AWT thread free to do all the drawing.

Never keep the AWT thread busy (for too long), i.e. do your mainFrame setup
and stuff in another thread. Have a look at the SwingUtilities and Thread
classes.

kind regards,

Jos


Thank you jos but i wouldn''t like to learn about threads now.I don''t have the time.
Is there any way to close the first window(not to setVisible(false) but close it) and then run the main from the other class?(all these in Action Performed)


Thank you jos but i wouldn''t like to learn about threads now.I don''t have the time.
Is there any way to close the first window(not to setVisible(false) but close it) and then run the main from the other class?(all these in Action Performed)

Simply use the SwingUtilities class; now you have something like this:

Expand|Select|Wrap|Line Numbers


这篇关于JButton和一个新的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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