繁体   English   中英

如何更改JLabel的位置?

[英]How to change position of a JLabel?

我正在尝试使用计时器将JLabel的位置从JPanel上的一个位置更改为另一个位置。 我不确定是否可以使用.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值的位置,则可以更改标签的位置。

如果您要寻找一些动画,请尝试使用Swing Timer

请看看如何使用Swing计时器

这是示例代码:

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

在此处找到示例代码

在此处输入图片说明


示例代码:( 以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();

我做了一个类似的例子,只是您可以得到它的基本玩笑,请尝试将其复制粘贴到一个名为“ LabelPlay”的新类中,它应该可以正常工作。

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(毫秒数)。

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

您是否打算为某些类型的游戏创建一些运动的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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM