I am trying to create a label that will show what the current selected value is for a particular slider. Currently, I am able to insert the labels and get them onto the window. I am not sure how to get the value of the slider to show up in the label. I am pretty sure that I need to parse the information being that it is two different value types.
Any help with where to go from here would be greatly appreciated.
Code added on edit:
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;
}
}
Change:
String blueVal = Integer.parseInt(blue.getValue());
TO
String blueVal = String.valueOf(blue.getValue());
You have to create a String
vom an Integer
, not an Integer
from a String
You'd first need to get the value of the slider by using:
int value = <JSliderFieldName>.getValue();
And then set your JLabel text to:
String labelText = "JSlider Value: " + value;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.