我要的结果在窗口下方,可以动态显示颜色变化情况.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class ColorSlide extends JFrame implements ChangeListener
{
ColorPanel canvas = new ColorPanel();
JSlider red = new JSlider(0,255,255);
JSlider green = new JSlider(0,255,0);
JSlider blue = new JSlider(0,255,0);

public ColorSlide()
{
super("Color Slide");
setBounds(80,80,270,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.RIGHT);
Container pane = getContentPane();
pane.setLayout(grid);
JPanel redPanel = new JPanel();
redPanel.setLayout(right);
redPanel.add(redLabel);
redPanel.add(red);
pane.add(redPanel);
JPanel greenPanel = new JPanel();
greenPanel.setLayout(right);
greenPanel.add(greenLabel);
greenPanel.add(green);
pane.add(greenPanel);
JPanel bluePanel = new JPanel();
bluePanel.setLayout(right);
bluePanel.add(blueLabel);
bluePanel.add(blue);
pane.add(bluePanel);
pane.add(canvas);
setContentPane(pane);
}
public void stateChanged(ChangeEvent evt)
{
JSlider source = (JSlider)evt.getSource();
if(source.getValueIsAdjusting() != true)
{
Color current = new Color(red.getValue(),green.getValue(),blue.getValue());
canvas.changeColor(current);
canvas.repaint();
}
}
public Insets getInsets()
{
Insets border = new Insets(45,10,10,10);
return border;
}
public static void main(String [] args)
{
ColorSlide cs = new ColorSlide();
}
//内部类
   class ColorPanel extends JPanel
{
Color background;
ColorPanel()
{
background = Color.red;
}
public void paintComponet(Graphics comp)
{
Graphics2D comp2D = (Graphics2D)comp;
comp2D.setColor(background);
comp2D.fillRect(0,0,getSize().width,getSize().height);
}
void changeColor(Color newBackground)
{
background = newBackground;
}
}
}

解决方案 »

  1.   

    方法名写错了,调用repaint()方法时,会调用paint(Graphics g)而不是paintComponet(Graphics comp)方法
    将方法        public void paintComponet(Graphics comp)
            {
                Graphics2D comp2D = (Graphics2D)comp;
                comp2D.setColor(background);
                comp2D.fillRect(0,0,getSize().width,getSize().height);
            }
    改为        public void paint(Graphics comp)
            {
                Graphics2D comp2D = (Graphics2D)comp;
                comp2D.setColor(background);
                comp2D.fillRect(0,0,getSize().width,getSize().height);
            }
      

  2.   

    我真服了,JPanel 自己就有改变背景色的方法你非得自己实现一个(还有错儿)。import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    //import java.awt.Graphics;
    //import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    @SuppressWarnings("serial")
    public class ColorSlide extends JFrame implements ChangeListener { private ColorPanel canvas = new ColorPanel();  //ColorPanel canvas = new ColorPanel();
    private JSlider red = new JSlider(0, 255, 255);  //JSlider red = new JSlider(0, 255, 255);
    private JSlider green = new JSlider(0, 255, 0);  //JSlider green = new JSlider(0, 255, 0);
    private JSlider blue = new JSlider(0, 255, 0);  //JSlider blue = new JSlider(0, 255, 0); public ColorSlide() {
    super("Color Slide");
    setBounds(80, 80, 270, 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.RIGHT);
    Container pane = getContentPane();
    pane.setLayout(grid);
    JPanel redPanel = new JPanel();
    redPanel.setLayout(right);
    redPanel.add(redLabel);
    redPanel.add(red);
    pane.add(redPanel);
    JPanel greenPanel = new JPanel();
    greenPanel.setLayout(right);
    greenPanel.add(greenLabel);
    greenPanel.add(green);
    pane.add(greenPanel);
    JPanel bluePanel = new JPanel();
    bluePanel.setLayout(right);
    bluePanel.add(blueLabel);
    bluePanel.add(blue);
    pane.add(bluePanel);
    pane.add(canvas);
    setContentPane(pane);
    setVisible(true);
    } public void stateChanged(ChangeEvent evt) {
    JSlider source = (JSlider)evt.getSource();
    if (source.getValueIsAdjusting() != true) {
    Color current = new Color(red.getValue(), green.getValue(), blue.getValue());
    canvas.changeColor(current);
    canvas.repaint();
    }
    } public Insets getInsets() {
    Insets border = new Insets(45, 10, 10, 10);
    return border;
    } public static void main(String[] args) {
    new ColorSlide();  //ColorSlide cs = new ColorSlide();
    } //内部类
    class ColorPanel extends JPanel { //Color background; //ColorPanel() {
    // background = Color.red;
    //} /*public void paintComponet(Graphics comp) {
    Graphics2D comp2D = (Graphics2D)comp;
    comp2D.setColor(background);
    comp2D.fillRect(0, 0, getSize().width, getSize().height);
    }*/ void changeColor(Color newBackground) {
    //background = newBackground;
    this.setBackground(newBackground);
    } }}
      

  3.   

    能不能再问下paint 和 paintComponet有什么区别??
      

  4.   

    canvas.repaint();
    没有对应的方法。怎么会调用得起来的???
      

  5.   

    repaint方法我觉得是一个奇怪的方法.
    居然能在无形中调用
    paint()方法
    和paintComponent方法??
      

  6.   

    如果学过windows的消息机制的话可能容易理解。
    有精力了解一下java的事件机制。