Java 如何在JLabel中同时设置setLocation和setText?

Java 如何在JLabel中同时设置setLocation和setText?,java,jlabel,sleep,settext,Java,Jlabel,Sleep,Settext,在我的代码的try块中,setText()或setLocation()分别工作良好,如下所述: .................... label.setText(name[i]); label.setLocation((label.getLocationOnScreen().x+50),0); Thread.sleep(time[i]);

在我的代码的try块中,setText()或setLocation()分别工作良好,如下所述:

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
以下代码根据给定的睡眠以正确的方式移动标签(每次50 pxl):

                ......... 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ...............
                ...................                                     
                label.setText(name[i]); 
                Thread.sleep(time[i]); 
                ....................
                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
以下代码根据给定的睡眠正确设置文本(“名称1”、“名称2”等):

                ......... 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ...............
                ...................                                     
                label.setText(name[i]); 
                Thread.sleep(time[i]); 
                ....................
                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
以下代码根据给定的睡眠状态正确设置文本(“名称1”、“名称2”等),但不移动JLabel:

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
我两个都需要。换句话说,在第一乐章中,它应该显示“名字1”,在第二乐章中,它应该显示“名字2”,依此类推

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
我是java和编程的新手。因此,请帮助我获得所需的输出。谢谢你的期待

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
我的完整代码如下:

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

class Move {
    JFrame frame = new JFrame();
    JLabel label = new JLabel("Journey");
    static int x=0;


public Move() {
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(800,600);
        frame.setLayout(new GridLayout());
        frame.add(label); 
        label.setLocation(0, 0); 
        frame.setVisible(true);

    doAll();
}

    void doAll(){ 
    Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
            try{
                String[] name={"Name 1","Name 2","Name 3","Name 4","Name 5","Name 6","Name 7","Name 8","Name 9","Name 10"};
                int time []={2000,1000,2000,1000,2000,1000,2000,1000,2000,1000};

                for (int i=0; i<10; i++){
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]);  

                } 
            }catch(Exception ae){

        }
    }
});

t.start();
}

public static void main(String[]args){
    new Move();  
    }
}
导入java.awt.GridLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
阶级运动{
JFrame=新JFrame();
JLabel标签=新JLabel(“旅程”);
静态int x=0;
公共行动{
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
框架。设置尺寸(800600);
frame.setLayout(新的GridLayout());
框架。添加(标签);
label.setLocation(0,0);
frame.setVisible(true);
doAll();
}
void doAll(){
线程t=新线程(新的可运行线程(){
@凌驾
公开募捐{
试一试{
字符串[]名称={“名称1”、“名称2”、“名称3”、“名称4”、“名称5”、“名称6”、“名称7”、“名称8”、“名称9”、“名称10”};
int time[]={200010002000100010001000200010001000100010002000100010001000100010001000200020001000100010001000200020002000100010001000100020002000100010001000200020002000100010001000100020002000200020001000100020002000200020001000100010002000200020002000100010001000;

对于(int i=0;i,框架的LayoutManager似乎正在为您设置标签的位置

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
您需要做的是将布局设置为空:

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
 frame.setLayout(null);
这将使框架不会试图自行布局组件

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
那你就可以了

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
label.setLocation((label.getLocationOnScreen().x+50),0);
然而,没有布局并不理想,但是
GridLayout
也不能满足您的需求

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
原因
GridLayout
将始终按照您编写的“
container.add(component)
”语句的顺序对组件进行排序。除了为之前的所有位置插入虚拟组件外,您不能将组件放置在特定位置

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................

您可能需要尝试
GridBagLayout
。在
GridBagLayout
中,您在关键事件上要做的就是更新动态面板的位置,它将显示在固定面板顶部的该位置。

您可以在JPanel上绘制图像并移动图像,而不是尝试移动Swing组件

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
这是我创建的GUI

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................

                ....................
                label.setText(name[i]); 
                label.setLocation((label.getLocationOnScreen().x+50),0);
                Thread.sleep(time[i]); 
                ....................
  • 您必须通过调用SwingUtilities invokeLater方法来启动Swing应用程序。此方法将所有Swing组件的创建和使用置于

  •                 ....................
                    label.setText(name[i]); 
                    label.setLocation((label.getLocationOnScreen().x+50),0);
                    Thread.sleep(time[i]); 
                    ....................
    
    >P>每当你试图协调2个或更多数组时,你应该考虑创建一个模型类。我创建了一个腿类来保存名字和延迟。这样,我只需要在旅程类中创建一个列表。

                    ....................
                    label.setText(name[i]); 
                    label.setLocation((label.getLocationOnScreen().x+50),0);
                    Thread.sleep(time[i]); 
                    ....................
    
  • 我从JPanel创建了DrawingPanel。始终在JPanel上创建GUI。永远不要直接在JFrame上创建GUI

  •                 ....................
                    label.setText(name[i]); 
                    label.setLocation((label.getLocationOnScreen().x+50),0);
                    Thread.sleep(time[i]); 
                    ....................
    
  • 在单独的Runnable、Thread或Swing计时器中运行动画。GUI在其自己的线程中运行,动画代码必须在不同的线程中运行,或者阻止GUI更新

  •                 ....................
                    label.setText(name[i]); 
                    label.setLocation((label.getLocationOnScreen().x+50),0);
                    Thread.sleep(time[i]); 
                    ....................
    
  • 由于动画在单独的线程中运行,因此绘图面板中的支腿设置包含在SwingUtilities invokeLater方法中。这确保绘图在事件调度线程(EDT)上进行

  •                 ....................
                    label.setText(name[i]); 
                    label.setLocation((label.getLocationOnScreen().x+50),0);
                    Thread.sleep(time[i]); 
                    ....................
    
  • 当我创建JFrame时,我在JFrame中添加了一个WindowListener,这样我就可以检测到窗口关闭,并在退出程序之前停止动画

  •                 ....................
                    label.setText(name[i]); 
                    label.setLocation((label.getLocationOnScreen().x+50),0);
                    Thread.sleep(time[i]); 
                    ....................
    
    这是密码

                    ....................
                    label.setText(name[i]); 
                    label.setLocation((label.getLocationOnScreen().x+50),0);
                    Thread.sleep(time[i]); 
                    ....................
    
    package com.ggl.testing;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.font.FontRenderContext;
    import java.awt.geom.Rectangle2D;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ImageMove implements Runnable {
    
        private JFrame frame;
    
        private JourneyRunnable journeyRunnable;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new ImageMove());
        }
    
        @Override
        public void run() {
            frame = new JFrame("Image Move");
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent event) {
                    exitProcedure();
                }
            });
    
            DrawingPanel drawingPanel = new DrawingPanel();
            frame.add(drawingPanel);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
    
            journeyRunnable = new JourneyRunnable(drawingPanel, new Journey());
            new Thread(journeyRunnable).start();
        }
    
        public void exitProcedure() {
            journeyRunnable.setRunning(false);
            frame.dispose();
            System.exit(0);
        }
    
        public class DrawingPanel extends JPanel {
    
            private static final long serialVersionUID = -8498462824787395933L;
    
            private int xPos, yPos, width, height;
    
            private Leg leg;
    
            public DrawingPanel() {
                this.width = 100;
                this.height = 50;
                this.xPos = 0;
                this.yPos = (200 - height) / 2;
                this.setPreferredSize(new Dimension(800, 200));
            }
    
            public void setLeg(Leg leg) {
                this.leg = leg;
                this.xPos += 10;
                repaint();
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, getWidth(), getHeight());
    
                g.setColor(Color.ORANGE);
                g.fillRect(xPos, yPos, width, height);
    
                if (leg != null) {
                    g.setColor(Color.BLACK);
                    centerString(g, new Rectangle(xPos, yPos, width, height),
                            leg.getName(), g.getFont());
                }
            }
    
            /**
             * This method centers a <code>String</code> in a bounding
             * <code>Rectangle</code>.
             * 
             * @param g
             *            - The <code>Graphics</code> instance.
             * @param r
             *            - The bounding <code>Rectangle</code>.
             * @param s
             *            - The <code>String</code> to center in the bounding
             *            rectangle.
             * @param font
             *            - The display font of the <code>String</code>
             * 
             * @see java.awt.Graphics
             * @see java.awt.Rectangle
             * @see java.lang.String
             */
            public void centerString(Graphics g, Rectangle r, String s, Font font) {
                FontRenderContext frc = new FontRenderContext(null, true, true);
    
                Rectangle2D r2D = font.getStringBounds(s, frc);
                int rWidth = (int) Math.round(r2D.getWidth());
                int rHeight = (int) Math.round(r2D.getHeight());
                int rX = (int) Math.round(r2D.getX());
                int rY = (int) Math.round(r2D.getY());
    
                int a = (r.width / 2) - (rWidth / 2) - rX;
                int b = (r.height / 2) - (rHeight / 2) - rY;
    
                g.setFont(font);
                g.drawString(s, r.x + a, r.y + b);
            }
        }
    
        public class JourneyRunnable implements Runnable {
    
            private boolean running;
    
            private DrawingPanel drawingPanel;
    
            private Journey journey;
    
            public JourneyRunnable(DrawingPanel drawingPanel, Journey journey) {
                this.drawingPanel = drawingPanel;
                this.journey = journey;
                this.running = true;
            }
    
            @Override
            public void run() {
                while (running) {
                    Leg leg = journey.getLeg();
                    setLeg(leg);
                    sleep(leg);
                }
            }
    
            public void setLeg(final Leg leg) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        drawingPanel.setLeg(leg);
                    }
                });
            }
    
            private void sleep(Leg leg) {
                try {
                    Thread.sleep(leg.getDelay());
                } catch (InterruptedException e) {
    
                }
            }
    
            public void setRunning(boolean running) {
                this.running = running;
            }
    
        }
    
        public class Journey {
    
            private int index;
            private List<Leg> legs;
    
            public Journey() {
                this.legs = new ArrayList<>();
    
                this.legs.add(new Leg("Name 1", 2000L));
                this.legs.add(new Leg("Name 2", 1000L));
                this.legs.add(new Leg("Name 3", 2000L));
                this.legs.add(new Leg("Name 4", 1000L));
                this.legs.add(new Leg("Name 5", 2000L));
                this.legs.add(new Leg("Name 6", 1000L));
                this.legs.add(new Leg("Name 7", 2000L));
                this.legs.add(new Leg("Name 8", 1000L));
                this.legs.add(new Leg("Name 9", 2000L));
                this.legs.add(new Leg("Name 10", 1000L));
    
                this.index = 0;
            }
    
            public Leg getLeg() {
                Leg leg = legs.get(index);
                index = ++index % legs.size();
                return leg;
            }
        }
    
        public class Leg {
            private final String name;
            private final long delay;
    
            public Leg(String name, long delay) {
                this.name = name;
                this.delay = delay;
            }
    
            public String getName() {
                return name;
            }
    
            public long getDelay() {
                return delay;
            }
    
        }
    
    }
    

    你的回答很有效,也很有教育意义。