如何使用 netbeans 将 jLabel 从 Jframe 的一侧动画化到另一侧
How to animate jLabel from one side to another side of Jframe using netbeans
我想创建一个小型 application.In 我的应用程序 我有 jLabel1
和 Jbutton1
。我想使用 jButton
单击将 jLabel1
从一侧动画化到另一侧。我不知道如何在 jButton1ActionPerformed
中 调用 它来创建 jLabel1
的动画。我已经完成了一个绘画应用程序代码,如下所示。
这是我的代码:
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.drawString("ancd", x, y);
try {
Thread.sleep(10000);
} catch (Exception e) {
System.out.println(""+e);
}
x+=10;
if(x>this.getWidth())
{
x=0;
}
repaint();
}
为简单起见,您可以使用 Swing 计时器来制作动画。但是,如果我是你,我不会移动 JLabel。但我会直接在 JPanel 上绘制并保留一组位置(图像的 x 和 y)。在计时器中,更新位置并重新绘制。
既然你想在屏幕上移动一个 JLabel,你可以这样做:
class DrawingSpace extends JPanel{
private JLabel label;
private JButton button;
private Timer timer;
public DrawingSpace(){
setPreferredSize(new Dimension(200, 300));
initComponents();
add(label);
add(button);
}
public void initComponents(){
label = new JLabel("I am a JLabel !");
label.setBackground(Color.YELLOW);
label.setOpaque(true);
button = new JButton("Move");
//Move every (approx) 5 milliseconds
timer = new Timer(5, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//Move 1 px everytime
label.setLocation(label.getLocation().x, label.getLocation().y+1);
}
});
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(!timer.isRunning())
timer.start();
else
timer.stop();
}
});
}
}
然后class到运行的程序:
class Mover{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() { // Run the GUI codes on the EDT
@Override
public void run() {
JFrame frame = new JFrame("Some Basic Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawingSpace());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
如果您打算使用 paint()
来实施,我会说您可能应该覆盖 paintComponents(Graphics g)
而不是 Java 组件中的 paint(Graphics g)
。
也不要用 Thread.sleep()
之类的东西弄乱你的绘画方法,它可能会冻结你的 UI。 paint 方法应该只包含绘画所需的代码,没有其他内容。
我想创建一个小型 application.In 我的应用程序 我有 jLabel1
和 Jbutton1
。我想使用 jButton
单击将 jLabel1
从一侧动画化到另一侧。我不知道如何在 jButton1ActionPerformed
中 调用 它来创建 jLabel1
的动画。我已经完成了一个绘画应用程序代码,如下所示。
这是我的代码:
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.drawString("ancd", x, y);
try {
Thread.sleep(10000);
} catch (Exception e) {
System.out.println(""+e);
}
x+=10;
if(x>this.getWidth())
{
x=0;
}
repaint();
}
为简单起见,您可以使用 Swing 计时器来制作动画。但是,如果我是你,我不会移动 JLabel。但我会直接在 JPanel 上绘制并保留一组位置(图像的 x 和 y)。在计时器中,更新位置并重新绘制。
既然你想在屏幕上移动一个 JLabel,你可以这样做:
class DrawingSpace extends JPanel{
private JLabel label;
private JButton button;
private Timer timer;
public DrawingSpace(){
setPreferredSize(new Dimension(200, 300));
initComponents();
add(label);
add(button);
}
public void initComponents(){
label = new JLabel("I am a JLabel !");
label.setBackground(Color.YELLOW);
label.setOpaque(true);
button = new JButton("Move");
//Move every (approx) 5 milliseconds
timer = new Timer(5, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//Move 1 px everytime
label.setLocation(label.getLocation().x, label.getLocation().y+1);
}
});
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(!timer.isRunning())
timer.start();
else
timer.stop();
}
});
}
}
然后class到运行的程序:
class Mover{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() { // Run the GUI codes on the EDT
@Override
public void run() {
JFrame frame = new JFrame("Some Basic Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawingSpace());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
如果您打算使用 paint()
来实施,我会说您可能应该覆盖 paintComponents(Graphics g)
而不是 Java 组件中的 paint(Graphics g)
。
也不要用 Thread.sleep()
之类的东西弄乱你的绘画方法,它可能会冻结你的 UI。 paint 方法应该只包含绘画所需的代码,没有其他内容。