如何改变JLabel的位置?

时间:2014-07-03 19:02:33

标签: java swing jpanel jlabel

我正在尝试使用计时器将JLabel的位置从我的JPanel上的一个位置更改为另一个位置。我不确定是否可以使用say .getLocation(),然后只更改水平x值,最后使用.setLocation()来有效地修改JLabel。我还使用了.getBounds.setBounds,但我仍然不确定如何获取旧的水平x值来更改并重新应用到新的x值。

我试过的代码看起来像这样,但这两种方法都不是改变JLabel位置的有效方法。

// mPos is an arraylist of JLabels to be moved.

for(int m = 0; m < mPos.size(); m++){
        mPos.get(m).setLocation(getLocation()-100);
    }

    for(int m = 0; m < mPos.size(); m++){
        mPos.get(m).setBounds(mPos.get(m).getBounds()-100);
    }

如果我能得到水平x值的位置,我可以改变标签的位置。

4 个答案:

答案 0 :(得分:2)

如果您正在寻找动画,请尝试使用Swing Timer

请查看How to Use Swing Timers

以下是示例代码:

int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
      //...Perform a task...
  }
};
new Timer(delay, taskPerformer).start();

找到Sample code here

enter image description here


示例代码:(以200 ms的间隔从左到右水平移动Hello World消息10px

private int x = 10;
...
final JPanel panel = new JPanel() {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Hello World", x, 10);
    }
};

int delay = 200; // milliseconds
ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        x += 10;
        if (x > 100) {
            x = 10;
        }
        panel.repaint();
    }
};
new Timer(delay, taskPerformer).start();

答案 1 :(得分:1)

我做了一个类似的例子,所以你可以得到它的基本玩笑,尝试在一个名为&#34; LabelPlay&#34;的新类中复制粘贴它。它应该工作正常。

import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class LabelPlay {

private JFrame frame;
private JLabel label;
private Random rand;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                LabelPlay window = new LabelPlay();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public LabelPlay() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 659, 518);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    label = new JLabel("YEEEHAH!");
    label.setBounds(101, 62, 54, 21);
    frame.getContentPane().add(label);

    JButton btnAction = new JButton("Action!");
    rand = new Random();
    btnAction.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int a = rand.nextInt(90)+10;
            int b = rand.nextInt(90)+10;
            int c = rand.nextInt(640)+10;
            int d = rand.nextInt(500)+10;
            label.setBounds(a, b, c, d);                
        }
    });
    btnAction.setBounds(524, 427, 89, 23);
    frame.getContentPane().add(btnAction);

}

}

如果你希望在特定时间循环发生这种情况,你可以把它放在一个循环中,然后在运行代码之前在循环中使用Thread.sleep(毫秒数)。

答案 2 :(得分:0)

为什么不在位置a创建JLabel,将其设置为可见,在位置b创建另一个JLabel,将其设置为不可见?计时器启动后,隐藏第一个并显示第二个。

答案 3 :(得分:0)

您是否计划为某种类型的游戏创建一些移动的imageIcon?或者一些在各处移动的标签? 我会使用绝对布局并每次手动设置位置。

myPanel.setLayout(null);

// an initial point
int x = 100;
int y =100;

while (
    //some moving pattern
    x++;   // 1 pixel per loop
    y+=2;  // 2 pixels per loop
    myLabel.setLocation(x,y);
}