Java 应用程序不会显示,但解决方案必须在主类之外可运行
我正在尝试制作一个简单的 Java 俄罗斯方块游戏。我正在关注有关的教程 GameDev.net。我想通过将可运行的 Frame 类保留在主类之外来超出教程的范围,而不是像教程所说的那样将所有内容都放在一个类中(如果我这样做,代码就可以工作)。
这段代码是我尝试执行以下操作(如下),并且我认为调用了可运行程序,但由于某种原因该应用程序没有显示。
有人可以澄清我做错了什么以及我需要做什么,同时在 Frame 类
Main.java 类
import java.lang.String;
public class Main {
public static void main(String args[])
{
//boolean victory = false;
Frame bob = new Frame();
bob.init();
}
}
Frame.java 类中保持可运行状态:
import java.applet.*;
import java.awt.Graphics;
public class Frame extends Applet implements Runnable {
Thread t;
int i;
public void init()
{
t = new Thread(this);
t.start();
i = 0;
}
public void run()
{
while(true)
{
i++;
repaint();
try {
t.sleep(1000/30);
} catch (InterruptedException e) { ; }
}
}
public void paint(Graphics g)
{
g.drawString("i = "+i,10,20);
}
}
非常感谢帮助!
I am trying to make a simple Java tetris game. I am following a tutorial on
GameDev.net. I want to go outside the tutorial by keeping the runnable Frame class outside of the main class, rather than putting everything in one class like the tutorial says (the code works if I do so).
This code is my attempt to do the following (below), and I presume that the runnable is called, but the app doesn't display for some reason.
Can someone clarify what I did wrong and what I need to do while keeping my runnable in the Frame class
Main.java class
import java.lang.String;
public class Main {
public static void main(String args[])
{
//boolean victory = false;
Frame bob = new Frame();
bob.init();
}
}
Frame.java class:
import java.applet.*;
import java.awt.Graphics;
public class Frame extends Applet implements Runnable {
Thread t;
int i;
public void init()
{
t = new Thread(this);
t.start();
i = 0;
}
public void run()
{
while(true)
{
i++;
repaint();
try {
t.sleep(1000/30);
} catch (InterruptedException e) { ; }
}
}
public void paint(Graphics g)
{
g.drawString("i = "+i,10,20);
}
}
Help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要运行小程序,请创建 HTML 文档。
命令
现在在网络浏览器中或从命令提示符中打开“sample.html”文档,发出以下
To run an applet, create an HTML document.
sample.html
Now open "sample.html" document in web-browser or from the command-prompt, issue following command