1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 5 import javax.swing.*; 6 7 public class Test_16_15 extends JFrame{ 8 9 public Test_16_15(){ 10 add(new JP()); 11 } 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Test_16_15 frame = new Test_16_15(); 15 frame.setSize(200, 200); 16 frame.setTitle("Test_16_15"); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setLocationRelativeTo(null); // Center the frame 19 frame.setVisible(true); 20 } 21 22 class JP extends JPanel{ 23 private int width ; 24 private int height; 25 private int x = 30 , y = 150; 26 27 public JP(){ 28 Timer timer = new Timer(1000,new TimerListener()); 29 timer.start(); 30 } 31 protected void paintComponent(Graphics g) 32 { 33 width = getWidth(); 34 height = getHeight(); 35 36 super.paintComponent(g); 37 38 if(x > width) x = 0; 39 g.fillOval(x + 30, y, 10, 10); 40 g.fillRect(x, y - 10, 50, 10); 41 x += 20; 42 } 43 class TimerListener implements ActionListener{ 44 45 @Override 46 public void actionPerformed(ActionEvent arg0) { 47 // TODO Auto-generated method stub 48 repaint(); 49 } 50 51 } 52 } 53 }
Test_16_15.java
如图:
时间: 2024-10-18 20:56:04