为我的 JSliders 旁边的 JLabel 添加颜色

Adding color to JLabel next to my JSliders

想要在我的 JLabel 中将红色、绿色、蓝色的文本颜色更改为它们适当的颜色,同时保持我的背景和 JSliders 仍然是程序的默认颜色。我不关心高度和宽度颜色,只关心 JSliders 旁边的红色、蓝色和绿色文本。任何帮助表示赞赏!谢谢!

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.swing.event.*;
public class ControlApp extends JFrame
{
    private JPanel mainPanel;
    private PrintWriter out;
    private JSlider height,width,red,green,blue;
    private String aspect;
    private String value;

public static void main(String []args)
{
    new ControlApp();
}

public ControlApp()
{
    super("ControlApp");
    mainPanel = new JPanel();
    add(mainPanel,BorderLayout.CENTER);

    //SliderListener sl = new SliderListener();

    height = new JSlider(JSlider.VERTICAL, 0,500,0);
    //height.addChangeListener(sl);
    width = new JSlider(JSlider.VERTICAL, 0,500,0);
    //width.addChangeListener(sl);
    red = new JSlider(JSlider.VERTICAL, 0,255,0);
    //red.addChangeListener(sl);       
    green = new JSlider(JSlider.VERTICAL, 0,255,0);
    //green.addChangeListener(sl);
    blue = new JSlider(JSlider.VERTICAL, 0,255,0);
    //blue.addChangeListener(sl);

    mainPanel.add(new JLabel("Height"));
    mainPanel.add(height,BorderLayout.CENTER);
    mainPanel.add(new JLabel("Width"));
    mainPanel.add(width,BorderLayout.CENTER);
    mainPanel.add(new JLabel("Red"));
    mainPanel.add(red,BorderLayout.CENTER);
    mainPanel.add(new JLabel("Green"));
    mainPanel.add(green,BorderLayout.CENTER);
    mainPanel.add(new JLabel("Blue"));
    mainPanel.add(blue,BorderLayout.CENTER);

    setLocationRelativeTo(null);
    this.setSize(500,250);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}
}

ttrigger10 -

您想使用 setForgroundColor 方法在这些 JLabel 上设置前景色,使它们成为特定颜色。请参阅下面修改后的代码以更改颜色:

    JLabel redLabel, blueLabel, greenLabel;
    redLabel = new JLabel("Red");
    redLabel.setForeground(Color.RED);
    greenLabel = new JLabel("Green");
    greenLabel.setForeground(Color.GREEN);
    blueLabel = new JLabel("Blue");
    blueLabel.setForeground(Color.BLUE);

    mainPanel.add(new JLabel("Height"));
    mainPanel.add(height, BorderLayout.CENTER);
    mainPanel.add(new JLabel("Width"));
    mainPanel.add(width, BorderLayout.CENTER);
    mainPanel.add(redLabel);
    mainPanel.add(red, BorderLayout.CENTER);
    mainPanel.add(greenLabel);
    mainPanel.add(green, BorderLayout.CENTER);
    mainPanel.add(blueLabel);
    mainPanel.add(blue, BorderLayout.CENTER);

在添加到默认使用 FlowLayout 的 JPanel 时,您不应使用 BorderLayout 常量。事实上,我建议您使用一个完全不同的布局,一个 GridLayout,这样所有的列都可以整齐地排列在一起。另一种选择是在 JSlider 周围使用 TitledBorder,例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.EnumMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.*;

public class ControlPanel extends JPanel {
    private static final int GAP = 3;
    private Map<Control, JSlider> sliderMap = new EnumMap<>(Control.class);

    public ControlPanel() {
        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        setLayout(new GridLayout(1, 0, GAP, GAP));
        for (Control control : Control.values()) {
            JSlider slider = new JSlider(JSlider.VERTICAL, 0, control.getMaxValue(), 0);
            slider.setPaintLabels(true);
            slider.setPaintTicks(true);
            slider.setPaintTrack(true);
            slider.setMajorTickSpacing(100);
            slider.setMinorTickSpacing(20);
            slider.addChangeListener(new SliderListener(control));
            sliderMap.put(control, slider);
            JPanel panel = new JPanel(new BorderLayout());
            TitledBorder titleBorder = BorderFactory.createTitledBorder(control.getText());
            titleBorder.setTitleColor(control.getColor());
            panel.setBorder(titleBorder);
            panel.add(slider);
            add(panel);            
        }
    }

    private class SliderListener implements ChangeListener {
        private Control control;

        public SliderListener(Control control) {
            this.control = control;
        }

        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider slider = (JSlider) e.getSource();
            int value = slider.getValue();            
            System.out.printf("Control: %s, Value: %d%n", control, value);
        }

    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Control Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ControlPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

public enum Control {
    HEIGHT("Height", 500, Color.BLACK), WIDTH("Width", 500, Color.black), RED("Red", 255, Color.RED), 
    GREEN("Green", 255, Color.GREEN), BLUE("Blue", 255, Color.BLUE);

    private String text;
    private int maxValue;
    private Color color;

    private Control(String text, int maxValue, Color color) {
        this.text = text;
        this.maxValue = maxValue;
        this.color = color;
    }

    public String getText() {
        return text;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public Color getColor() {
        return color;
    }

}

显示为: