簡體   English   中英

如何在Java中更改標簽顏色?

[英]How can I change label color in Java?

我在LinkList調用“ l”中設置了一組標簽,我需要更改這些標簽的背景顏色。 我需要在每次顏色更改之間留出2秒的間隔,因此我嘗試按如下方法使用重繪方法,但它沒有給我所需的結果。 請有人可以給我解決這個問題的方法嗎?

    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是通過defalut non-Opaque ,即使您設置了backGround(whatever),也沒有為myLabel.setOpaque(true);定義myLabel.setOpaque(true); 沒有為JLabel'area着色,另一種方法是將CustomPaint與overpaintpaintComponetn paintComponetn() ,例如

在此處輸入圖片說明

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);
    }
}

編輯:

您張貼的空白在Swing中的Concurency方面存在太多問題, 因此必須將BackGroung Tasks到GUI的所有輸出包裝到invokeLater()並且代碼塊中的最后代碼行將revalidate ()repaint()以填充JComponents內部可見容器

三件事:1)通過使用setBackground設置背景色應導致它使用新顏色重新繪制自身,而無需調用重新繪制(假定它是不透明的)。 2)我建議為此使用javax.swing.Timer類。 確保您知道此類與java.util.Timer類之間的區別。 3)調用setBackground(null)應該恢復“默認顏色”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM