【发布时间】:2013-02-19 05:56:41
【问题描述】:
我正在尝试编写一个程序来将矩形向下移动到 JFrame,谁能解释为什么这不起作用?
public class DrawingComponent extends JLabel {
public static int x = 0;
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Rectangle rect = new Rectangle(50,x,50,50);
g2.draw(rect);
x = x+100;
}
}
public class GameL {
javax.swing.JFrame frame = new javax.swing.JFrame();
public static void main(String[] args) {
GameL tetris = new GameL();
tetris.start();
}
public void start(){
//setup frame
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
TimerEH timereh = new TimerEH();
Timer timer = new Timer(5000,timereh);
timer.start();
}
class TimerEH implements ActionListener{
public void actionPerformed(ActionEvent e){
DrawingComponent dc = new DrawingComponent();
frame.add(dc);
}
}
}
【问题讨论】:
标签: java swing graphics2d