如何在向JFrame添加组件之间添加时间延迟?

时间:2013-01-13 09:17:41

标签: java swing timer repaint jcomponent

我创建了一个JFrame,它包含一个JPanel。我创建了许多JLabel。我可以将JLabel添加到JPanel,并正确显示它们。但我想实现它们,因为它们顺序显示;要显示的每个JLabel之间的时间延迟。

在搜索StackOverfLow之后,我尝试了一些代码,但它没有效果! 那么如何使用计时器通过设置时间延迟来逐个显示组件(标签)。

我不希望修复我的代码,特别是在答案中。 只是 显示如何以延迟的方式显示任何类型的组件,每个一段时间后显示的组件。就这些。我提供了我的代码,以显示我努力解决问题。

首先,这是使用JLabel的子类:(没有问题)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JLabel;

public class DLabel extends JLabel
{
    Dimension size = new Dimension(70, 75);
    Font font = new Font(Font.SANS_SERIF, 12, 35);

    public DLabel(String t)
    {
        this.setPreferredSize(size);
        this.setBorder(BorderFactory.createBevelBorder(1, Color.white, Color.black));
        this.setVerticalAlignment(JLabel.CENTER);
        this.setHorizontalAlignment(JLabel.CENTER);
        this.setText(t);
        this.setFont(font);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Color color1 = new Color(226, 218, 145);
        Color color2 = color1.brighter();
        int w = getWidth();
        int h = getHeight();
        GradientPaint gp = new GradientPaint(
                0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
        super.paintComponent(g);
    }
}

使用DLabel类的另一个类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

public class DelayedLabels extends JPanel
{

    static JFrame frame;
    Timer timer;   //timer to be used for dealy

    DLabel card_1;  //Defining the DLabels
    DLabel card_2;
    DLabel card_3;
    JLabel[] labelsArray;

    public DelayedLabels()
    {
        this.setLayout(null);

        card_1 = new DLabel("1");
        card_2 = new DLabel("2");
        card_3 = new DLabel("3");

        labelsArray = new DLabel[3]; //create the array

        createLabelsArray(); //add the Labels Objects to labelsArray
        setLabelsLocations(labelsArray); // set the locations of the Labels to be displayed on the JPanel
        addLabelsToPanel(labelsArray); //The adding of the Labels to the JPanel
    }

    private void createLabelsArray()
    {
        labelsArray[0] = card_1;
        labelsArray[1] = card_2;
        labelsArray[2] = card_3;
    }

    private void setLabelsLocations(JLabel[] labels)
    {
        int length = labels.length;
        int gap = 10;
        int counter = 10;
        for (int i = 0; i < length; i++)
        {
            labels[i].setBounds(170, counter, 60, 70);
            counter = counter + labels[i].getBounds().height + gap;

        }
    }

    private void addLabelsToPanel(JLabel[] labels)
    {
        for (int i = 0; i < labels.length; i++)
        {
            frame.revalidate();
            frame.repaint();
            this.add(labels[i]);
            timer = new Timer(1000, timerAction); //timer to use with 1000 milliseconds
            timer.start();
        }
    }
    private ActionListener timerAction = new ActionListener() //action to be invoked after each label added
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            frame.revalidate();
            frame.repaint();
        }
    };

    private static void createAndShowGUI()
    {
        frame = new JFrame();
        frame.setSize(600, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DelayedLabels demo = new DelayedLabels();
        demo.setOpaque(true);
        frame.add(demo);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:5)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DelayedLabels extends JPanel {

    static JFrame frame;
    Timer timer;   //timer to be used for dealy
    JLabel card_1;  //Defining the JLabels
    JLabel card_2;
    JLabel card_3;
    JLabel[] labelsArray;
    ActionListener listener;

    public DelayedLabels() {
        listener = new ActionListener() {

            int i = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                Component c = DelayedLabels.this.getTopLevelAncestor();
                DelayedLabels.this.add(labelsArray[i++]);
                c.validate();
                c.repaint();
                if (i==labelsArray.length) {
                    timer.stop();
                }
            }
        };
        this.setLayout(new GridLayout(0, 1, 20, 20));

        card_1 = new JLabel("Label 1");
        card_2 = new JLabel("Label 2");
        card_3 = new JLabel("Label 3");

        labelsArray = new JLabel[3]; //create the array

        createLabelsArray(); //add the Labels Objects to labelsArray
        timer = new Timer(1000,listener);
        timer.start();
    }

    private void createLabelsArray() {
        labelsArray[0] = card_1;
        labelsArray[1] = card_2;
        labelsArray[2] = card_3;
    }

    private static void createAndShowGUI() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DelayedLabels demo = new DelayedLabels();
        demo.setOpaque(true);
        frame.add(demo, BorderLayout.PAGE_START);
        frame.pack();
        frame.setSize(200, 150);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
}