我现在要用java2d做一个东西。利用一个相当与计时器的东西控制一个图片的位置的变化,图片放在jlabel中。我尝试了很多方法,对于它的位置调节都没用,有人说需要调用它的父窗体的方法(是这样的吗)怎么实现啊。总觉得用java编界面没有用vb好用,vb中可以任意调整大小和位置,java为何不那么做,总觉得不解。

解决方案 »

  1.   

    setBounds(x,y,width,height) 比 VB 牛多了
      

  2.   

    就是  java 比vb要强大多了
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import javax.swing.Icon;public class MoveLabel  extends JFrame{
    static int x=0;
    static int y=0;
    static JLabel label=new JLabel();


    public MoveLabel(){

    this.setSize(new Dimension(400,300));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().setLayout(null);
    this.setVisible(true);

    label.setIcon(new ImageIcon(getClass().getResource("images/boy2-6.gif")));

    } public static void main(String[] args){

    MoveLabel win=new MoveLabel();


    win.getContentPane().add(label); javax.swing.Timer t=new javax.swing.Timer(1000,new 
    ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    label.setBounds(x++  ,y++,30,30);

    }
    }
    );
    t.start();



    }


    }
      

  4.   

    setBounds()不好使,我还用过了setlocation(),我还在netbean中尝试了直接创建一个jframe然后调节属性也不管用,在属性的中初始化后代码中加入setBounds()和setlocation()也不好使。冷月我用你的程序编译执行了,可是什么都没有(连图片也没显示,图片的名字我改了)。