从 JSlider 向 JLabel 显示 RGB
displaying RGB to a JLabel from a JSlider
我正在尝试编写一个程序,从每个值的滑块中获取红色、绿色和蓝色的颜色值,然后将其显示为 rgb 值。我不确定如何从三个滑块中提取 rgb 值,但我认为我很接近。我不知道如何将数据发送到 JLabel,或者我是否使用正确的方法来获取 rgb。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.image.ColorModel;
import javax.swing.JSlider;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MyColorChooser extends JFrame {
public final JSlider colorRed;
public static JSlider colorBlue;
public static JSlider colorGreen;
public static JLabel rgbValue;
public static JLabel green;
public static JLabel blue;
public int r;
public int g;
public int b;
Color c;
public Color getC() {
return c;
}
public void setC(Color c) {
this.c = new Color(r, g, b);
}
public MyColorChooser() {
super("Slider Demo");
JPanel Panel1 = new JPanel(new BorderLayout());
JPanel Panel2 = new JPanel(new BorderLayout());
JPanel Panel3 = new JPanel(new BorderLayout());
JPanel Panel4 = new JPanel(new BorderLayout());
rgbValue = new JLabel("RGB Value");
colorRed
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorBlue
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorGreen
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorRed.setMajorTickSpacing(5); // create tick every 10
colorRed.setPaintTicks(true);
colorRed.setPaintLabels(true);// paint ticks on slider
colorBlue.setMajorTickSpacing(5); // create tick every 10
colorBlue.setPaintTicks(true);
colorBlue.setPaintLabels(true);//
colorGreen.setMajorTickSpacing(5); // create tick every 10
colorGreen.setPaintTicks(true);
colorGreen.setPaintLabels(true);//
colorRed.setLabelTable(colorRed.createStandardLabels(10));
colorGreen.setLabelTable(colorGreen.createStandardLabels(10));
colorBlue.setLabelTable(colorBlue.createStandardLabels(10));
Panel1.add(colorRed);
Panel2.add(colorGreen);
Panel3.add(colorBlue);
Panel4.add(rgbValue);
setLayout(new GridLayout(10, 1));
add(Panel1);
add(Panel2);
add(Panel3);
add(Panel4);
colorGreen.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorGreen = (JSlider) e.getSource();
g = colorGreen.getValue();; //To change body of generated methods, choose Tools | Templates.
}
});
colorRed.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorRed = (JSlider) e.getSource();
r = colorRed.getValue();
}
});
colorBlue.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorBlue = (JSlider) e.getSource();
b = colorBlue.getValue();//To change body of generated methods, choose Tools | Templates.
}
});
}
}
I can't figure out how to send the data to the JLabel or if I'm using the correct approach to get the rgb
好吧,如果你想改变文本,你需要在标签上调用 setText(....)
方法。
所以这意味着您需要创建一个类似于 updateLabel()
的方法,您可以从 3 个侦听器中的每个侦听器中调用该方法。在该方法中,您将获得 r、g、b 变量的值,并按照您希望文本在标签中显示的方式格式化字符串。
如果我明白你在这里想要什么...你想从 3 个 JSliders 中获取值,可能是 0-255,然后将其转换为 JLabel 上的 background/foreground 颜色?如果是这样,那么您将需要在每次 JSlider 调用时都有 changeListener rgbValue.setForeground(new Color(r, g, b));
。如果您希望 JLabel 显示颜色的十六进制表示,那么它将是
rgbValue.setText("RGB Value: #" + String.format("%02X", r) + String.format("%02X", g) + String.format("%02X", b));
我正在尝试编写一个程序,从每个值的滑块中获取红色、绿色和蓝色的颜色值,然后将其显示为 rgb 值。我不确定如何从三个滑块中提取 rgb 值,但我认为我很接近。我不知道如何将数据发送到 JLabel,或者我是否使用正确的方法来获取 rgb。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.image.ColorModel;
import javax.swing.JSlider;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MyColorChooser extends JFrame {
public final JSlider colorRed;
public static JSlider colorBlue;
public static JSlider colorGreen;
public static JLabel rgbValue;
public static JLabel green;
public static JLabel blue;
public int r;
public int g;
public int b;
Color c;
public Color getC() {
return c;
}
public void setC(Color c) {
this.c = new Color(r, g, b);
}
public MyColorChooser() {
super("Slider Demo");
JPanel Panel1 = new JPanel(new BorderLayout());
JPanel Panel2 = new JPanel(new BorderLayout());
JPanel Panel3 = new JPanel(new BorderLayout());
JPanel Panel4 = new JPanel(new BorderLayout());
rgbValue = new JLabel("RGB Value");
colorRed
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorBlue
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorGreen
= new JSlider(SwingConstants.HORIZONTAL, 0, 255, 0);
colorRed.setMajorTickSpacing(5); // create tick every 10
colorRed.setPaintTicks(true);
colorRed.setPaintLabels(true);// paint ticks on slider
colorBlue.setMajorTickSpacing(5); // create tick every 10
colorBlue.setPaintTicks(true);
colorBlue.setPaintLabels(true);//
colorGreen.setMajorTickSpacing(5); // create tick every 10
colorGreen.setPaintTicks(true);
colorGreen.setPaintLabels(true);//
colorRed.setLabelTable(colorRed.createStandardLabels(10));
colorGreen.setLabelTable(colorGreen.createStandardLabels(10));
colorBlue.setLabelTable(colorBlue.createStandardLabels(10));
Panel1.add(colorRed);
Panel2.add(colorGreen);
Panel3.add(colorBlue);
Panel4.add(rgbValue);
setLayout(new GridLayout(10, 1));
add(Panel1);
add(Panel2);
add(Panel3);
add(Panel4);
colorGreen.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorGreen = (JSlider) e.getSource();
g = colorGreen.getValue();; //To change body of generated methods, choose Tools | Templates.
}
});
colorRed.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorRed = (JSlider) e.getSource();
r = colorRed.getValue();
}
});
colorBlue.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider colorBlue = (JSlider) e.getSource();
b = colorBlue.getValue();//To change body of generated methods, choose Tools | Templates.
}
});
}
}
I can't figure out how to send the data to the JLabel or if I'm using the correct approach to get the rgb
好吧,如果你想改变文本,你需要在标签上调用 setText(....)
方法。
所以这意味着您需要创建一个类似于 updateLabel()
的方法,您可以从 3 个侦听器中的每个侦听器中调用该方法。在该方法中,您将获得 r、g、b 变量的值,并按照您希望文本在标签中显示的方式格式化字符串。
如果我明白你在这里想要什么...你想从 3 个 JSliders 中获取值,可能是 0-255,然后将其转换为 JLabel 上的 background/foreground 颜色?如果是这样,那么您将需要在每次 JSlider 调用时都有 changeListener rgbValue.setForeground(new Color(r, g, b));
。如果您希望 JLabel 显示颜色的十六进制表示,那么它将是
rgbValue.setText("RGB Value: #" + String.format("%02X", r) + String.format("%02X", g) + String.format("%02X", b));