Swing timers basic functionality [closed]

2019-03-06 16:59发布

问题:

I'm new at java graphic design, and I would like you, if possible, to help me with an easy example to help me to get to understand the basic functionality of JFrames, Timers, SwingControllers, and all this stuff. How would you implement the following case:

We have a JFrame with a JPanel inside. When the execution starts, the JPanel is white, but we want it to change it's colour every two seconds:

public class MiJFrame extends javax.swing.JFrame {

    public MiJFrame() {
        initComponents();
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MiJFrame().setVisible(true);
                jPanel1.setBackground(Color.yellow);
                jPanel1.setBackground(Color.RED);
            }
        });
    }

    // Variables declaration - do not modify
    private static javax.swing.JPanel jPanel1;
    // End of variables declaration
}

At first, I used the sleep method of a thread object between the setBackgroud() methods but it doesn't work, as it only shows the last change. How would you use here a Timer object?

回答1:

First of all, whenever you need to change the colour of the said thingy, always set Opaque property to true for the said thingy. Like in your case it's the JPanel so first of all you must use panelObject.setOpaque(true), for some Look And Feels calling this method is a must for background colour changes to take effect.

Do try this code example, regarding the rest :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 * @see
 * http://stackoverflow.com/q/11036830/1057230
 */

public class ColourTimer
{
    private JPanel contentPane;
    private Timer timer;
    private int counter;
    private Color[] colours = {
                                Color.RED,
                                Color.WHITE,
                                Color.BLUE,
                                Color.DARK_GRAY,
                                Color.YELLOW,
                                Color.LIGHT_GRAY,
                                Color.BLACK,
                                Color.MAGENTA,
                                Color.PINK,
                                Color.CYAN
                              };

    private ActionListener timerAction = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if (counter == (colours.length - 1))
                counter = 0;
            contentPane.setBackground(colours[counter++]);
        }    
    };

    public ColourTimer()
    {
        counter = 0;
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("Colour Timer");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setOpaque(true);

        final JButton button = new JButton("STOP");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (timer.isRunning())
                {
                    button.setText("START");
                    timer.stop();
                }
                else
                {
                    button.setText("STOP");
                    timer.start();
                }
            }
        });

        frame.getContentPane().add(contentPane, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.PAGE_END);
        frame.setSize(300, 200);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        timer = new Timer(2000, timerAction);
        timer.start();
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ColourTimer().displayGUI();
            }
        });
    }
}