Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用netbeans将jLabel从Jframe的一侧动画化到另一侧_Java_Swing_Netbeans_Paint_Thread Sleep - Fatal编程技术网

Java 如何使用netbeans将jLabel从Jframe的一侧动画化到另一侧

Java 如何使用netbeans将jLabel从Jframe的一侧动画化到另一侧,java,swing,netbeans,paint,thread-sleep,Java,Swing,Netbeans,Paint,Thread Sleep,我想创建一个小应用程序。在我的应用程序中,我有jLabel1和Jbutton1。我想使用jButton单击从一侧到另一侧设置jLabel1动画。我不知道如何在jButton1ActionPerformed中调用它来创建jLabel1的动画。我做了一个油漆应用程序代码,如下所示 这是我的密码: public void paint(Graphics g) { super.paint(g); Graphics2D g2=(Graphics2D)g; g2.drawString(

我想创建一个小应用程序。在我的应用程序中,我有
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计时器。然而,如果我是你,我就不会搬家。但是我将直接在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 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()
实现,我想说您可能应该重写Java组件中的
paintComponents(Graphics g)
,而不是
paint(Graphics g)
。 另外,不要用
Thread.sleep()
之类的东西来扰乱你的绘画方法,它可能会冻结你的UI。绘制方法应仅包含绘制所需的代码,而不包含其他内容。

为了保持简单,可以对动画使用Swing计时器。然而,如果我是你,我就不会搬家。但是我将直接在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 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()
实现,我想说您可能应该重写Java组件中的
paintComponents(Graphics g)
,而不是
paint(Graphics g)

另外,不要用
Thread.sleep()
之类的东西来扰乱你的绘画方法,它可能会冻结你的UI。绘制方法应该只包含绘制所需的代码,而不包含任何其他内容。

您可能会发现有用的代码和@guleryuz这两个代码对我来说都不理解,您可以为其制定适当的解决方案吗?@S.V您可以查看下面我的解决方案。您可能会发现有用的代码和@guleryuz这两个代码对我来说都不理解,可以吗请为它制定合适的解决方案?@S.V您可以看看下面我的解决方案。(1+)使用计时器,重写paintComponent()并删除Thread.sleep()@sv当使用动画时,您还需要将布局设置为null,否则标签将跳回其默认位置,即调用布局管理器(即调整帧大小)。这意味着您需要手动设置标签的大小/位置。相反,您可能需要考虑使用为此目的而设计的.If(.Time.ISRunn())+++(1 +),用于使用定时器、重写PrimtCutices()和消除线程。sv当使用动画时,您还需要将布局设置为null,否则标签将跳回其默认位置,即调用布局管理器(即调整帧大小)。这意味着您需要手动设置标签的大小/位置。相反,您可能需要考虑使用为此目的而设计的.If(.Time.ISRunTunn())+++。