问答题 本题程序的功能是通过滑动条修改颜色的RGB值,从而控制颜色。程序中有一个面板、3个标签和3个滑动条,标签和滑动条一一对应,分别对应三原色红、绿、蓝,任意拖动其中的一个滑动条,所对应的颜色值就会发生变化,面板的颜色也会对应地发生变化。滑动条值的范围是0~255。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class simple extends JFrame implements AdjustmentListener
{
public simple()
{
setTitle("simple");
setSize(300,200);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
System.exit (0);
});
Container contentPane =______;
JPanel p = new JPane1();
p.setLayout(new GridLayout(3,2));
p.add(redLabel = new JLabel("Red 0"));
p.add(red = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255));
red.setBlockIncrement(16);
red.addAdjustmentListener(this);
p.add(greenLabel = new JLabel("Green 0"));
p.add(green = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255));
green.setBlockIncrement(16);
green.addAdjustmentListener(this);
p.add(blueLabel = new JLabel("Blue 0"));
p.add(blue = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255));
blue.setBlockIncrement(16);
blue.addAdjustmentListener(this);
contentPane.add(p,"South");
colorPanel = new JPanel();
colorPanel.setBackground(new Color(0,0,0));
contentPane.add(colorPanel,"Center");
}
public void adjustmentValueChanged(AdjustmentEvent evt)
{
redLabel.setText("Red " + red.getValue());
greenLabel.setText("Green " + green.getValue());
blueLabel.setText("Blue " + blue.getValue());
colorPanel.setBackground(new Color(red.getValue().green.getValue(),blue.getValue()));
______;
}
public static void main(String[] args)
{
JFrame f = new simple();
f.show();
}
private JLabel redLabel;
private JLabel greenLabel;
private JLabel blueLabel;
private JScrollBar red;
private JScrollBar green;
private JScrollBar blue;
private JPanel colorPanel;
}
【正确答案】
【答案解析】getContentPane()。
colorPanel.repaint()。