2

我创建了一个 JFrame,它包含一个 JPanel。我创建了许多 JLabel。我可以将 JLabels 添加到 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();
            }
        });
    }
}
4

1 回答 1

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();
            }
        });
    }
}
于 2013-01-13T09:41:44.837 回答