一个小程序,帮忙看看

ipotang 2004-10-04 11:41:02
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
public class GrayScalePanel extends AbstractColorChooserPanel
implements ChangeListener {
JSlider scale;
// Set up our list of grays. We'll assume we have all 256 possible shades,


static Color[] grays = new Color[256];
static {
for (int i=0; i<256; i++) { grays[i] = new Color(i, i, i); }
}
public GrayScalePanel() {
setLayout(new GridLayout(0, 1));
// create the slider and attach us as a listener
scale = new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
scale.addChangeListener(this); //疑问1
// Set up our display for the chooser
add(new JLabel("Pick your shade of gray:", JLabel.CENTER));
JPanel jp = new JPanel();
jp.add(new JLabel("Black"));
jp.add(scale);
jp.add(new JLabel("White"));
add(jp);
}
// We did this work in the constructor so we can skip it here.
protected void buildChooser() { }
// Make sure the slider is in sync with the other chooser panels. We rely on
// the red channel, but we could do a fancier averaging if we really wanted

public void updateChooser() { //疑问2
scale.setValue(getColorSelectionModel().getSelectedColor().getRed());
}
// Pick a name for our tab in the chooser
public String getDisplayName() { return "Gray Scale"; }
// No need for an icon.
public Icon getSmallDisplayIcon() { return null; }
public Icon getLargeDisplayIcon() { return null; }
// And lastly, update the selection model as our slider changes.
public void stateChanged(ChangeEvent ce) {
getColorSelectionModel().setSelectedColor(grays[scale.getValue()]); //疑问3
}
}
我在注释中标明了疑问1和疑问2。
scale.addChangeListener(this);
这个方法的参数应该是ChangeLister对象呀,这里的this是指什么
public void updateChooser() { //疑问2
scale.setValue(getColorSelectionModel().getSelectedColor().getRed());
}这个方法是怎么被触发的,scale如何和颜色值相关联的?
...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
无欲则钢 2004-10-04
  • 打赏
  • 举报
回复
ChangeListener是个接口,他有个框架,你实现了方法他就会自动调用的。
无欲则钢 2004-10-04
  • 打赏
  • 举报
回复
答案1
public class GrayScalePanel extends AbstractColorChooserPanel
implements ChangeListener
this 就是 GrayScalePanel 他实现了ChangeListener,他就可以上漱到ChangeListener。可以把他当ChangeListener看。
chenmingchuang 2004-10-04
  • 打赏
  • 举报
回复
路过看看,,,,还是看不明白

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧