import java.awt.*; import javax.swing.*;public class MyFrame extends JFrame { public static final int Default_Width = 300; public static final int Default_Height = 400;
public MyFrame() { setSize(Default_Width, Default_Height);
Container con = getContentPane();
MyPanel panel = new MyPanel(); con.add(panel); }
public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }class MyPanel extends JPanel { private int x; private int y; private Thread t;
public MyPanel() { x = 140; y = 30; Runnable r = new MyRunnable(); t = new Thread(r); t.start(); }
public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillRect(x, y, 20, 20); }
class MyRunnable implements Runnable { public void run() { while(true) { try { y += 10; if(y > getHeight()) y = 30; repaint(); Thread.sleep(1000); } catch(InterruptedException e) { Thread.currentThread().interrupt(); } } } } }
哦了!不行再联系我吧,哈哈
哦了!不行再联系我吧,哈哈
import java.awt.*;
import javax.swing.*;public class MyFrame extends JFrame
{
public static final int Default_Width = 300;
public static final int Default_Height = 400;
public MyFrame()
{
setSize(Default_Width, Default_Height);
Container con = getContentPane();
MyPanel panel = new MyPanel();
con.add(panel);
}
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class MyPanel extends JPanel
{
private int x;
private int y;
private Thread t;
public MyPanel()
{
x = 140;
y = 30;
Runnable r = new MyRunnable();
t = new Thread(r);
t.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(x, y, 20, 20);
}
class MyRunnable implements Runnable
{
public void run()
{
while(true)
{
try
{
y += 10;
if(y > getHeight())
y = 30;
repaint();
Thread.sleep(1000);
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
}
}
}