I've been trying to modify the following code so that it only displays one scrollbar for 3 radio buttons but so far I just can get it to work right. After the modification the gui should have 3 radio buttons at the top, one scrollbar for all 3 buttons, and labels underneath the scrollbar. After selecting which color( with radiobuttons), as the scrollbar is moved the background should fill in with the choosen color. I will keep trying to do it but if anyone could help me out with this code I'll appreciate it. My programming isn't that great.
The hint that I was given was this:
Put the buttons into a panel. Call setBackground separately for each button, the panel containing the buttons, and the label underneath the scrollbar. The window should correctly close using the X.
import java.awt.*;
import java.awt.event.*;
//Frame class
class PickColor extends Frame {
private Label redLabel =
new Label("Red = 128", Label.CENTER);
private Label greenLabel =
new Label("Green = 128", Label.CENTER);
private Label blueLabel =
new Label("Blue = 128", Label.CENTER);
private Scrollbar redBar =
new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
private Scrollbar greenBar =
new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
private Scrollbar blueBar =
new Scrollbar(Scrollbar.HORIZONTAL, 128, 1, 0, 256);
// Constructor
public PickColor(String title) {
// Set title, background color, and layout
super(title);
setBackground(new Color(128, 128, 128));
setLayout(new GridLayout(6, 1));
// Create scrollbar listener
ScrollbarListener listener = new ScrollbarListener();
// Add red scrollbar and label to frame; attach
// listener to scrollbar
add(redBar);
redBar.addAdjustmentListener(listener);
add(redLabel);
// Add green scrollbar and label to frame; attach
// listener to scrollbar
add(greenBar);
greenBar.addAdjustmentListener(listener);
add(greenLabel);
// Add blue scrollbar and label to frame; attach
// listener to scrollbar
add(blueBar);
blueBar.addAdjustmentListener(listener);
add(blueLabel);
// Attach window listener
addWindowListener(new WindowCloser());
}
// Driver class
public static void main(String[] args) {
Frame f = new PickColor("Pick Color");
f.setSize(150, 200);
f.setVisible(true);
}
//Listener for all scrollbars
class ScrollbarListener implements AdjustmentListener {
public void adjustmentValueChanged(AdjustmentEvent evt) {
int red = redBar.getValue();
int green = greenBar.getValue();
int blue = blueBar.getValue();
redLabel.setText("Red = " + red);
greenLabel.setText("Green = " + green);
blueLabel.setText("Blue = " + blue);
Color newColor = new Color(red, green, blue);
redLabel.setBackground(newColor);
greenLabel.setBackground(newColor);
blueLabel.setBackground(newColor);
}
}
}