如何在多个JFrame的情况下处理动作事件 [英] how to handle action events in case of more than one JFrame
本文介绍了如何在多个JFrame的情况下处理动作事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从互联网上下载了一个蛇游戏项目,最初这个项目包含三个java文件,即
1。 Engine.java
2. Snake.java
3. Gameoard.java
当我运行Engine.java时
,游戏开始运行。
现在我已对其进行了一些修改以改善游戏的交互性,即我添加了两个名为
$ b的JDialog $ b
1。 PlayGame.java
2.RulesDialog.java
PlayGame.java有三个JButtons: - 播放按钮,规则按钮,退出
- 当点击播放按钮时,游戏应该开始
- 当规则按钮被点击RulesDialog.java的JDialog出现和
- 当退出按钮被点击应用程序或游戏应关闭。
按钮的动作设置在各个按钮的
actionPerformed()
方法中。
现在我面临的问题是当我< pre lang =text>点击播放按钮时,屏幕上会显示游戏窗口,但蛇不是移动
我无法指出如何解决这个问题..
所以plz帮助我出去
解决方案
我的建议是做有的actionPerformed
通话sepperate方法对于每个bu tton:
public class 演示
extends JFrame
implements ActionListener
{
私人 JButton优先;
私人 JButton秒;
私人 JButton第三名;
public Demo(){
first = new JButton( first);
first.addActionListener( this );
calcFrame.add(第一个);
second = new JButton( 第二跨度>);
second.addActionListener( this );
calcFrame.add(第二个);
third = new JButton( 第三跨度>);
third.addActionListener( this );
calcFrame.add(第三个);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()== first)
{
actionFirst();
} else if (e.getSource()== second)
{
actionSecond();
} else if (e.getSource()== third)
{
actionThird();
}
}
}
I have downloaded a snake game project from the internet, initially this project contains three java files namely
1. Engine.java
2. Snake.java
3. Gameoard.java
when i runs Engine.java, game starts running .
Now i have done some modification in it to improve the interactivity of the game, i.e i have added two JDialog named
1. PlayGame.java
2.RulesDialog.java
PlayGame.java has three JButtons :- Play button, Rules Button , Exit
-when play button gets clicked game should start -when rules button gets clicked RulesDialog.java JDialog appears and -when exit button gets clicked application or game should be closed.
Action of button is set in the
actionPerformed()
method of individual buttons.
Now problem i am facing is when i <pre lang="text">click Play button game window is visible on the screen but snake is not moving
and i am not getting to point how to resolve this problem ..
So plz help me out
解决方案
My recommendation would be to do haveactionPerformed
call sepperate methods for each button:
public class Demo extends JFrame implements ActionListener { private JButton first; private JButton second; private JButton third; public Demo() { first = new JButton("first"); first.addActionListener(this); calcFrame.add(first); second = new JButton("second"); second.addActionListener(this); calcFrame.add(second); third = new JButton("third"); third.addActionListener(this); calcFrame.add(third); } public void actionPerformed(ActionEvent e) { if(e.getSource() == first) { actionFirst(); } else if(e.getSource() == second) { actionSecond(); } else if(e.getSource() == third) { actionThird(); } } }
这篇关于如何在多个JFrame的情况下处理动作事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文