简体   繁体   English

如何使用for循环在JLabel中显示更改的文本

[英]How to display changing text in JLabel using for loop

public class UserInterface {
    OpenFile of = new OpenFile();

    JFrame jf = new JFrame("File Manager");
    JButton jb1 = new JButton("Open File");
    JLabel jl1 = new JLabel("Recommendations appear here");
    JLabel jl2 = new JLabel();
    JList<String> list;

    public void build() {

        DefaultListModel<String> str = new DefaultListModel<String>();

        for (int i = 0; i < of.f.length; i++) {
            str.addElement(of.f[i].getAbsolutePath());
        }

        list = new JList<String>(str);

        Border b = BorderFactory.createLineBorder(Color.black, 2);
        Font f = new Font("Arial", Font.BOLD, 20);

        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                of.OpenFileMethod(list.getSelectedValue());
            }
        });

        jl1.setFont(f);
        jl1.setBorder(b);
        list.setFont(f);
        jf.add(jl1).setBounds(30, 100, 300, 200);
        jf.add(list).setBounds(400, 100, 300, 300);
        jf.add(jb1).setBounds(250, 300, 100, 50);
        jf.setLayout(null);
        jf.setSize(800, 800);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 0; i < 100; i++) {
            jl1.setText("Loading.");
            jl1.setText("Loading...");
        }
    }
}

Problem in the for loop, only setting last "loading..." text to JLabel I want it to get in the loop and print it 100 times. for循环中的问题,仅将最后的“正在加载...”文本设置为JLabel,我希望它进入循环并打印100次。 May be the loop gets over before the launch of the swing application. 可能是循环在启动swing应用程序之前结束了。 any solution for this? 有什么解决办法吗?

any solution for this? 有什么解决办法吗?

their is nothing wrong here this code work perfect but the problem here is when that loop executes it's done in a blink of an eye! 他们没有错,这里的代码可以完美地工作,但是这里的问题是当循环执行时,眨眼就可以完成!

your best friend for this case is the class javax.swing.Timer 在这种情况下,您最好的朋友是javax.swing.Timer

and the example will show you how to use it and hopefully solve your issue , timers have their own shared Thread so you don't have to worry about it it will run without hanging your ui or blocking your code . 该示例将向您展示如何使用它,并希望解决您的问题,计时器具有自己的共享Thread因此您不必担心它会在挂起用户界面或阻止代码的情况下运行。

//this int determines the time delay for each time it executes it's actions
    private int delay = 20;
    private int times = 0;
    private String text = "Loading ";
    private Timer textTimer;
    private class TimerAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            times++;
            String theSetText = text;
            for (int i = 0; i < times; i++) {
                theSetText += ".";
            }
            if (times == 3) {
                times = 0;
            }
        }

    }

you can always add more action listeners via timer.addActioListener method and it will be looped there too. 您始终可以通过timer.addActioListener方法添加更多的动作侦听器, timer.addActioListener方法也将在那里循环。

for your issue just add the above code to your class and add replace the loop in your code with this 对于您的问题,只需将上面的代码添加到您的类中,然后在代码中添加替换循环即可

textTimer = new Timer (delay,new TimerAction ());
textTimer.start();

and when the time is right (as you want) when you want this to stop just call 当时间合适时(根据需要)当您希望停止时,只需致电

textTimer.stop();

to stop the timer from running . 停止计时器运行。 here's a link to get more about the topic How to Use Swing Timers 这是获得有关主题如何使用摆动计时器的更多信息的链接

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

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