首先,我想說如果我的問題聽起來不太具體,我對編程知之甚少,請原諒我。啓動運行命令的問題
這段代碼的問題是,我希望它隨機移動方塊(雖然知道它只是說嗨,但重複),但運行()沒有激活。
代碼:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.net.*;
public class Square extends JApplet {
int X = 10;
int Y = 10;
public void init() {
FlowLayout flow = new FlowLayout();
setLayout(flow);
}
public void paint(Graphics screen) {
// Paint lines
Graphics2D g2 = (Graphics2D) screen;
g2.draw(new Line2D.Double(X, Y, (X + 50), Y));
g2.draw(new Line2D.Double(X, Y, X, (Y + 50)));
g2.draw(new Line2D.Double(X, (Y + 50), (X + 50), (Y + 50)));
g2.draw(new Line2D.Double((X + 50), (Y + 50), (X + 50), Y));
}
public void run() {
System.out.println("Hi2");
while (true) {
System.out.println("Hi");
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Square");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JApplet applet = new Square();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(550,100));
f.setVisible(true);
}
}
(我知道我進口的幾件事情,我可能並不需要,我複製了一些這從另一個代碼)
http://docs.oracle.com/javase/tutorial/deployment/applet/index.html – Aubin 2013-03-06 22:05:52
你對run()方法有什麼期待?它永遠不會被調用。 – Aubin 2013-03-06 22:11:19