如果我说,在最后一秒和第三行
label.setText("x = ");
标签正在完美移动,但是当我将其更改为
label.setText("x = "+ x);
它不动。具体来说,我想查看 JLabel 移动变量x时的宽度位置!除此之外,我说label.setBounds(x,(getHeight()/2),300,300);
将标签的 Y 边界设置为帧大小的一半,但它不在帧的中间?任何的想法?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.JFrame;
public class myTiemr {
public static void main(String args[])
{
TimeFrame frame = new TimeFrame();
}
}
class TimeFrame extends JFrame
{
private static final long serialVersionUID = 1L;
private int x = 0;
JLabel label = new JLabel("Here is my label");
public TimeFrame()
{
int d = 10;
setTitle("My Frame");
setSize(500,500);
this.setLocationRelativeTo(null);
add(label);
Timer time = new Timer(d,new TimerListener());
time.start();
setVisible(true);
}
class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(x>getWidth()){
x=-100;
}
x+=1;
label.setText("x = "+ x);
//label.setText("x = ");
label.setBounds(x,(getHeight()/2),300,300);
}
}
}