【发布时间】:2012-07-07 14:22:22
【问题描述】:
你好,我是新来的,我正在制作一个小游戏,我制作了一个 gui,如下所示:
public static void main(String[] args) {
Frame frame1 =new Frame("TickTacToe");
frame1.setLayout(null);
frame1.setBounds(250,150,500,500);
frame1.setVisible(true);
frame1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
final Button button11 = new Button("");
button11.addActionListener(null);
final Button button12 = new Button("");
button11.addActionListener(null);
final Button button13 = new Button("");
button11.addActionListener(null);
final Button button21 = new Button("");
button11.addActionListener(null);
final Button button22 = new Button("");
button11.addActionListener(null);
final Button button23 = new Button("");
button11.addActionListener(null);
final Button button31 = new Button("");
button11.addActionListener(null);
final Button button32 = new Button("");
button11.addActionListener(null);
final Button button33 = new Button("");
button11.addActionListener(null);
button11.setBounds(100, 100, 80, 70);
button12.setBounds(100, 200, 80, 70);
button13.setBounds(100, 300, 80, 70);
button21.setBounds(200, 100, 80, 70);
button22.setBounds(200, 200, 80, 70);
button23.setBounds(200, 300, 80, 70);
button31.setBounds(300, 100, 80, 70);
button32.setBounds(300, 200, 80, 70);
button33.setBounds(300, 300, 80, 70);
frame1.add(button11);
frame1.add(button12);
frame1.add(button13);
frame1.add(button21);
frame1.add(button22);
frame1.add(button23);
frame1.add(button31);
frame1.add(button32);
frame1.add(button33);
}
我想向按钮添加动作侦听器,但最好不要在这个 void 中添加一个不同的类,这样我就可以创建一种方法来运行函数循环,例如玩家轮换器,如果它是第一个,它将设置按钮文本到 x 和 o 如果它是 othe 我或多或少知道我需要使用的代码,但我想不出一种方法来使用 anzwhere 的 gui,而不是它自己的 void。我不太清楚我在寻找什么,所以非常感谢任何帮助。
【问题讨论】:
-
很抱歉,这不是问题。您需要知道如何在 java 中处理类和函数。如何使用(MVC 模式)en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller。请阅读一些 java swing 教程以找到自己的方式
-
所以这里的 void Main 在 Gui 类中,我尝试使用类似这样的方法: public class Main { private Main(String[] args){ Gui g = new Gui;游戏循环 gl = 新游戏循环; } } Gameloop 是我有功能的地方,我可以像这样把它们放在一起吗?这是正确的方向吗?
-
要么为你的按钮使用数组(如果适用的话),或者将它们命名为比 button31 更好的名称,等等!
-
@user1489906 : 我做了这个很久很久了,虽然不知道它有多大帮助,因为随着我对 Java 的了解越来越多,我意识到,很多事情真的没有在我认为我必须的方式。但是here,这个游戏的代码可能会给你一些想法。用户名:guest 密码:guest
-
@nice cow 感谢您提供该项目的链接,这将很有帮助。