开发者

How can I change label color in Java?

I have set of labels in LinkList call "l" and I need to change the background colors of those labels. I need to put 2 secconds gap between each color change so I tried to use repaint method as follows but it doesn't give me the required result. Please can some one give me a solution for this problem ?

    public static void changeColor(LinkedList l,JFrame f){

    for (int i = 0; i < l.size(); i++) {
        try {
            final JLabel xx = (JLabel) l.get(i);
            xx.setBackground(Color.red);
            f.repaint();
            xx.setText("B");
            System.out.println(i);
            new thread().run();
   开发者_运维百科         xx.setBackground(Color.GRAY);
            xx.setText("A");
            f.repaint();

            } catch (Exception ex) {
               Logger.getLogger(TestView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


JLabel is by defalut non-Opaque, even you setBackGround(whatever), then without definitions for myLabel.setOpaque(true); isn't JLabel'area colorized, another way is using CustomPaint with overide paintComponetn(), for example

How can I change label color in Java?

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

public class LabelBackGround {

    private JFrame frame;

    public LabelBackGround() {
        JLabel lblWest = new JLabel();
        lblWest.setPreferredSize(new Dimension(50, 150));
        lblWest.setOpaque(true);
        lblWest.setBackground(Color.red);
        JLabel lblEast = new JLabel();
        lblEast.setPreferredSize(new Dimension(50, 150));
        lblEast.setOpaque(true);
        lblEast.setBackground(Color.red);
        frame = new JFrame();
        frame.add(new CustomColoredComponents(), BorderLayout.NORTH);
        frame.add(new CustomColoredComponents(), BorderLayout.SOUTH);
        frame.add(lblWest, BorderLayout.WEST);
        frame.add(lblEast, BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                LabelBackGround gridBadFrame = new LabelBackGround();
            }
        });
    }
}

class CustomColoredComponents extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 20);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 30);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.blue);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

EDIT:

and void that you posted has too much problem with Concurency in Swing, then all output from BackGroung Tasks to the GUI must be wrapped into invokeLater() and last code lines in you code block would be revalidate () and repaint() for fill JComponents inside visible Container


Three things: 1) Setting the background colour by using setBackground should result in it repainting itself with the new colour without a call to repaint (assuming it is opaque). 2) I would suggest using javax.swing.Timer class for this. Make sure you know the difference between this and the java.util.Timer class. 3) Calling setBackground(null) should restore the "default colour".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜