[code]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");
setSize(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("Read:");
JLabel greenLabel=new JLabel("Green:");
JLabel blueLabel=new JLabel("Blue:");
GridLayout grid=new GridLayout(4,1);
FlowLayout right=new FlowLayout(FlowLayout.RIGHT);
setLayout(grid);
JPanel redPanel=new JPanel();
redPanel.setLayout(right);
redPanel.add(redLabel);
redPanel.add(red);
add(redPanel);
JPanel greenPanel=new JPanel();
greenPanel.setLayout(right);
greenPanel.add(greenLabel);
greenPanel.add(green);
add(greenPanel);
JPanel bluePanel=new JPanel();
bluePanel.setLayout(right);
bluePanel.add(blueLabel);
bluePanel.add(blue);
add(bluePanel);
add(canvas);
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[] arguments){
ColorSlide cs=new ColorSlide();
}
}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;
}
}[/code]
可以在代码看到getInsets()是被创建了,但没调用,可通过修改边界值(如改为:45,10,45,10)就能发现实际上是被调用了的,那是怎么被调用了?
还有就是,既然有了getInsets()方法,怎么没有setInsets()方法呢?
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");
setSize(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("Read:");
JLabel greenLabel=new JLabel("Green:");
JLabel blueLabel=new JLabel("Blue:");
GridLayout grid=new GridLayout(4,1);
FlowLayout right=new FlowLayout(FlowLayout.RIGHT);
setLayout(grid);
JPanel redPanel=new JPanel();
redPanel.setLayout(right);
redPanel.add(redLabel);
redPanel.add(red);
add(redPanel);
JPanel greenPanel=new JPanel();
greenPanel.setLayout(right);
greenPanel.add(greenLabel);
greenPanel.add(green);
add(greenPanel);
JPanel bluePanel=new JPanel();
bluePanel.setLayout(right);
bluePanel.add(blueLabel);
bluePanel.add(blue);
add(bluePanel);
add(canvas);
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[] arguments){
ColorSlide cs=new ColorSlide();
}
}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;
}
}[/code]
可以在代码看到getInsets()是被创建了,但没调用,可通过修改边界值(如改为:45,10,45,10)就能发现实际上是被调用了的,那是怎么被调用了?
还有就是,既然有了getInsets()方法,怎么没有setInsets()方法呢?
解决方案 »
- 一个很基础的问题。关于静态属性
- 抽象类可以直接使用吗?
- Java IDE工具JawaBeginer发布1.4版本了。增加好多好用的功能。
- 我的classpath路径前加有‘.'怎么还是不能在当前目录下编译?
- 向为新手做贡献 达内2007Java培训视频 不知道怎么上传 求教
- applet 问题。请高手指点!
- 高分求答案!!
- 今天心情不好 散分
- 爽啊,jb8在www.borland.com已经可以下载,大家快去啊!!!!!!!!!!!!
- 一个log4j的问题:我能不能根据level的不同定义不同的输出对象,比如不同的log文件
- 求解:一个读取文件内容的问题!竟然因为文件名过长而读取不到!困惑
- 关于站网在tomcat中运行找不到类的问题~!初学者..请各位指点
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");
setSize(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("Read:");
JLabel greenLabel=new JLabel("Green:");
JLabel blueLabel=new JLabel("Blue:");
GridLayout grid=new GridLayout(4,1);
FlowLayout right=new FlowLayout(FlowLayout.RIGHT);
setLayout(grid);
JPanel redPanel=new JPanel();
redPanel.setLayout(right);
redPanel.add(redLabel);
redPanel.add(red);
add(redPanel);
JPanel greenPanel=new JPanel();
greenPanel.setLayout(right);
greenPanel.add(greenLabel);
greenPanel.add(green);
add(greenPanel);
JPanel bluePanel=new JPanel();
bluePanel.setLayout(right);
bluePanel.add(blueLabel);
bluePanel.add(blue);
add(bluePanel);
add(canvas);
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[] arguments){
ColorSlide cs=new ColorSlide();
}
}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;
}
}