间隔后的Java显示组件

时间:2012-04-02 13:40:10

标签: swing

我有一个JComponents列表,每个都有一个可见时间。我必须从List中选择每个组件,使用swing计时器,在指定的可见时间内显示它,然后从面板中删除显示的组件,从列表中取出下一个组件并显示,依此类推。在特定时间,我应该只显示一个组件。请帮忙

    testList.add(new JLabel());
    testList.add(new JButton());

    java.util.Timer timer;
    TimerTask task;

   for (Iterator<> it = testList.iterator(); it.hasNext(); ) {
    final JComponent comp= it.next();
    comp.setSize(new Dimension(200, 200));
        for(int index=0; index==getComponents().length;index++) {
          if(getComponents().length==0)
              add(not.getComponent(), BorderLayout.CENTER);
      }
    timer= new java.util.Timer();
    task= new TimerTask() {
        public void run() {
            validate();
            try {
                remove(comp);

            } catch (Exception e) {
                e.printStackTrace();
            }

            validate();
            repaint();
            }
        };

        timer.schedule( task ,30*1000);
        it.remove();

   }

2 个答案:

答案 0 :(得分:2)

尝试使用此代码,这样做是为了满足您的需求: - )

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;

public class ListAndTimer
{
    private JButton button;
    private JToggleButton toggleButton;
    private JLabel label;
    private JCheckBox cbox;
    private JRadioButton rButton;
    private Object[] data;
    private JList myList;
    private List<JComponent> components;
    private JComponent lastComponent;
    private JPanel contentPane;
    private Timer timer;

    public ListAndTimer()
    {
        contentPane = new JPanel();

        button = new JButton("BUTTON");
        button.setName("2");

        toggleButton = new JToggleButton("TOGGLE BUTTON");
        toggleButton.setName("3");

        label = new JLabel("LABEL");
        label.setName("4");

        cbox = new JCheckBox("CHECKBOX", false);
        cbox.setName("2");

        rButton = new JRadioButton("RADIOBUTTON", false);
        rButton.setName("3");

        components = new ArrayList<JComponent>();
        components.add(button);
        components.add(toggleButton);
        components.add(label);
        components.add(cbox);
        components.add(rButton);        

        data = new Object[]{
                    "Button", "Toggle Button", "Label",
                    "CheckBox", "RadioButton"
               };

        myList = new JList(data);
        myList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        myList.addListSelectionListener(new ListSelectionListener()
        {
            public void valueChanged(ListSelectionEvent lse)
            {
                int index = myList.getSelectedIndex();
                int time = 0;
                System.out.println("Index : " + index);             
                lastComponent = components.get(index);
                contentPane.add(lastComponent);
                time = Integer.parseInt(lastComponent.getName());

                contentPane.revalidate();
                contentPane.repaint();

                timer = new Timer(time * 1000, timerAction);
                timer.setRepeats(false);
                timer.start();
            }
        });
        myList.setVisibleRowCount(5);
        myList.setLayoutOrientation(JList.VERTICAL_WRAP);
    }

    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            contentPane.remove(lastComponent);
            contentPane.revalidate();
            contentPane.repaint();
        }
    };

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("List And Timer");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationByPlatform(true);

        frame.getContentPane().add(contentPane, BorderLayout.CENTER);
        frame.getContentPane().add(myList, BorderLayout.LINE_END);

        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ListAndTimer().createAndDisplayGUI();
            }
        });
    }
}

答案 1 :(得分:1)

你可以这样做:

private static int current = 0;

public static void main ( String[] args )
{
    final List<Component> components = new ArrayList<Component> ();
    components.add ( new JButton ( "Button" ) );
    components.add ( new JToggleButton ( "Toggle" ) );
    components.add ( new JLabel ( "Label" ) );
    components.add ( new JCheckBox ( "Check" ) );
    components.add ( new JRadioButton ( "Radio" ) );


    final JPanel panel = new JPanel ();
    panel.setBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ) );

    panel.add ( components.get ( current ) );

    JFrame frame = new JFrame ();
    frame.add ( panel );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );

    new Timer ( 2000, new ActionListener ()
    {
        public void actionPerformed ( ActionEvent e )
        {
            current = current < components.size () - 1 ? current + 1 : 0;
            panel.removeAll ();
            panel.add ( components.get ( current ) );
            panel.revalidate ();
            panel.repaint ();
        }
    } ).start ();
}

这里有一些重要的事情:
1.从面板中删除旧组件(我只删除所有现有的以简化代码)
2.在布局中的同一位置添加新的
3.重新验证容器(JPanel),以便更新其子节点 4.重新绘制容器,以便正确查看更改
另外这个(我的意思是改变JPanel内容的代码部分)应该在Swing-worker线程中完成。在我的示例中,javax.swing.Timer操作在该线程中执行,因此我不需要担心它
在其他情况下,您应该在里面执行代码:

    SwingUtilities.invokeLater ( new Runnable ()
    {
        public void run ()
        {
            // Code here
        }
    } );

或:

    SwingUtilities.invokeAndWait ( new Runnable ()
    {
        public void run ()
        {
            // Code here
        }
    } );

视情况而定。

没有必要,但强烈建议,因为Swing不是线程安全的,以后当你的应用程序会变宽时,你可能会遇到很多问题。