Hi!
Can anyone please help me understand why my panel p1 won't change color?
Thanks,
Lynn
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.Color.*;
public class Scroll extends JFrame implements AdjustmentListener
{
//Declare
private JScrollBar jscbHort1, jscbHort2, jscbHort3;
//Main
public static void main(String[] args)
{
Scroll frame = new Scroll();
frame.pack();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400, 250);
frame.setVisible(true);
}
//Default constructor
public Scroll()
{
setTitle("Exercise 9.9: Use Scroll Bars");
//Create panel to hold the color display
JPanel p1 = new JPanel();
p1.add(new JLabel("Show Colors", SwingConstants.CENTER));
p1.setLayout(new GridLayout(1, 1));
//Create second panel that holds the scrollbars
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3, 1));
p2.setBorder(new TitledBorder("Choose Colors"));
p2.add(new JLabel("Red"));
p2.add(jscbHort1 = new JScrollBar());
jscbHort1.setValues(0,50,0,255);
p2.add(new JLabel("Green"));
p2.add(jscbHort2 = new JScrollBar());
jscbHort2.setValues(0,50,0,255);
p2.add(new JLabel("Blue"));
p2.add(jscbHort3 = new JScrollBar());
jscbHort3.setValues(0,50,0,255);
jscbHort1.setOrientation(Adjustable.HORIZONTAL);
jscbHort2.setOrientation(Adjustable.HORIZONTAL);
jscbHort3.setOrientation(Adjustable.HORIZONTAL);
//Add panels to frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);
//Register the listeners
jscbHort1.addAdjustmentListener(this);
jscbHort2.addAdjustmentListener(this);
jscbHort3.addAdjustmentListener(this);
}
//Handle the scroll bar adjustment actions
public void adjustmentValueChanged(AdjustmentEvent e)
{
JPanel p1 = new JPanel();
if(e.getSource() == jscbHort1)
{
Color c = new Color(jscbHort1.getValue(), jscbHort2.getValue(), jscbHort3.getValue());
p1.setForeground(Color.red);
p1.repaint();
}
else if(e.getSource() == jscbHort2)
{
Color c = new Color(jscbHort1.getValue(), jscbHort2.getValue(), jscbHort3.getValue());
p1.setForeground(Color.green);
p1.repaint();
}
else if(e.getSource() == jscbHort3)
{
Color c = new Color(jscbHort1.getValue(), jscbHort2.getValue(), jscbHort3.getValue());
p1.setForeground(Color.blue);
p1.repaint();
}
}
}