我正在尝试创建一个标签,该标签将显示特定滑块的当前选定值。目前,我可以插入标签并将它们放到窗口上。我不知道如何获得滑块的值以显示在标签中。我很确定我需要解析信息,因为它是两种不同的值类型。
任何有关从哪里出发的帮助将不胜感激。
编辑时添加了代码:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class ColorSliders extends JFrame implements ChangeListener {
ColorPanel canvas = new ColorPanel();
JSlider red = new JSlider(0, 255, 255);
JSlider blue = new JSlider(0, 255, 0);
JSlider green = new JSlider(0, 255, 0);
// Creating labels to measure output of Slider Value
JLabel redLabelValue = new JLabel("Red Value: ");
JLabel greenLabelValue = new JLabel("Green V: ");
JLabel blueLabelValue = new JLabel("Blue: ");
public ColorSliders() {
super("Color Slide");
setSize(350, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
red.setMajorTickSpacing(50);
red.setMinorTickSpacing(10);
red.setPaintTicks(true);
red.setPaintLabels(true);
red.addChangeListener(this);
green.setMajorTickSpacing(50);
green.setMinorTickSpacing(10);
green.setPaintTicks(true);
green.setPaintLabels(true);
green.addChangeListener(this);
blue.setMajorTickSpacing(50);
blue.setMinorTickSpacing(10);
blue.setPaintTicks(true);
blue.setPaintLabels(true);
blue.addChangeListener(this);
JLabel redLabel = new JLabel("Red: ");
JLabel greenLabel = new JLabel("Green: ");
JLabel blueLabel = new JLabel("Blue: ");
GridLayout grid = new GridLayout(4,1);
FlowLayout right = new FlowLayout(FlowLayout.LEFT);
setLayout(grid);
JPanel redPanel = new JPanel();
redPanel.setLayout(right);
redPanel.add(redLabel);
redPanel.add(red);
redPanel.add(redLabelValue);
add(redPanel);
JPanel greenPanel = new JPanel();
greenPanel.setLayout(right);
greenPanel.add(greenLabel);
greenPanel.add(green);
greenPanel.add(greenLabelValue);
add(greenPanel);
JPanel bluePanel = new JPanel();
bluePanel.setLayout(right);
bluePanel.add(blueLabel);
bluePanel.add(blue);
bluePanel.add(blueLabelValue);
add(bluePanel);
add(canvas);
setVisible(true);
}
public void stateChanged(ChangeEvent event) {
JSlider source = (JSlider) event.getSource();
if (source.getValueIsAdjusting() != true) {
Color current = new Color(red.getValue(), green.getValue(),
blue.getValue());
String blueVal = Integer.parseInt(blue.getValue());
blueLabelValue.setText(blueVal);
canvas.changeColor(current);
canvas.repaint();
}
}
public Insets getInsets() {
Insets border = new Insets(45, 10, 10, 10);
return border;
}
public static void main(String[] arguments) {
ColorSliders cs = new ColorSliders();
}
}
class ColorPanel extends JPanel {
Color background;
ColorPanel() {
background = Color.red;
}
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D) comp;
comp2D.setColor(background);
comp2D.fillRect(0, 0, getSize().width, getSize().height);
}
void changeColor(Color newBackground) {
background = newBackground;
}
}
答案 0 :(得分:0)
变化:
String blueVal = Integer.parseInt(blue.getValue());
要
String blueVal = String.valueOf(blue.getValue());
您必须在String
Integer
vom Integer
而不是String
答案 1 :(得分:0)
您首先需要使用以下方法获取滑块的值:
int value = <JSliderFieldName>.getValue();
然后将您的JLabel文本设置为:
String labelText = "JSlider Value: " + value;