java2图形设计卷Ⅱ学习小结Word格式.docx
《java2图形设计卷Ⅱ学习小结Word格式.docx》由会员分享,可在线阅读,更多相关《java2图形设计卷Ⅱ学习小结Word格式.docx(46页珍藏版)》请在冰豆网上搜索。
java.util.EventObject
构造方法
publicChageEvent(Objectsource)
ChangeEvent类仅提供了一个构造方法,没有提供其他方法。
ChangeEvent构造方法以事件源作为参数。
图3-9所示的小应用程序通过监控滑杆的值来说明轻量通知。
一个变化监听器添加到这个小应用程序的滑杆中以获得滑杆
值并更新这个小应用程序的状态区。
图3-9轻量通知
例3-2列出了图3-9所示的小应用程序的代码
例3-2一个滑杆的轻量通知
importjava.awt.*;
importjavax.swing.*;
importjavax.swing.event.*;
publicclassTestextendsJApplet{
publicvoidinit(){
JSliderslider=newJSlider(0,100,50);
getContentPane().add(slider,BorderLayout.CENTER);
slider.addChangeListener(newChangeListener(){
publicvoidstateChanged(ChangeEvente){
JSliders=(JSlider)e.getSource();
showStatus(Integer.toString(s.getValue()));
}
});
}
}
5.状态事件通知
对不经常变化的模型属性,模型使用状态通知。
状态通知激发所有类型的事件,并且提供比事件源更多信息(轻量事件通
知仅提供事件源一种信息)。
例如,当选取或取消选取一个单选按钮时,按钮模型将激发一个项事件。
6.属性变化通知
当模型的关联属性变化时(当一个属性的变化激发一个属性变化事件,则这个属性称作关联属性。
)模型会以一个
java.beans.PropertyChangeEvent的形式产生状态通知。
属性变化通知由一些对象来处理,这些对象的类实现
java.beans.PropertyChangeListener接口,接口总结3-2中总结了这个接口。
importjavax.swing.*;
importjava.awt.event.*;
importjava.util.*;
importjava.beans.*;
publicclassTestextendsJFrame{
JTreetree=newJTree();
publicTest(){
ContainercontentPane=getContentPane();
JScrollPanescrollPane=newJScrollPane(tree);
contentPane.add(newControlPanel(),BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
tree.addPropertyChangeListener(newPropertyListener());
classControlPanelextendsJPanel{
JCheckBoxshowRoot=newJCheckBox("
showrootnode"
);
publicControlPanel(){
showRoot.setSelected(tree.isRootVisible());
setLayout(newFlowLayout());
add(showRoot);
showRoot.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
tree.setRootVisible(showRoot.isSelected());
}
});
}
classPropertyListenerimplementsPropertyChangeListener{
publicvoidpropertyChange(PropertyChangeEvente){
Stringname=e.getPropertyName();
if(name.equals(JTree.ROOT_VISIBLE_PROPERTY)){
Stringmsg="
RootVisibleProperty:
"
+
e.getNewValue().toString();
JOptionPane.showMessageDialog(
Test.this,//parentcomp
msg,//message
"
PropertyChange"
//title
JOptionPane.INFORMATION_MESSAGE);
例4-7不容透明Swing组件
importjava.awt.*;
publicclassOpaqueTestextendsJApplet{
publicvoidinit(){
ContainercontentPane=getContentPane();
RainPanelrainPanel=newRainPanel();
ColoredPanelopaque=newColoredPanel(),
transparent=newColoredPanel();
//JComponentsareopaquebydefault,sotheopaque
//propertyonlyneedstobesetfortransparent
transparent.setOpaque(false);
rainPanel.add(opaque);
rainPanel.add(transparent);
contentPane.add(rainPanel,BorderLayout.CENTER);
System.out.println(opaque.isOpaque());
System.out.println(transparent.isOpaque());
}
classRainPanelextendsJPanel{
ImageIconrain=newImageIcon(this.getClass().getResource("
rain.gif"
));
privateintrainw=rain.getIconWidth();
privateintrainh=rain.getIconHeight();
publicvoidpaintComponent(Graphicsg){
Dimensionsize=getSize();
for(introw=0;
row<
size.height;
row+=rainh)
for(intcol=0;
col<
size.width;
col+=rainw)
rain.paintIcon(this,g,col,row);
classColoredPanelextendsJPanel{
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(0,0,size.width-1,size.height-1);
g.setColor(Color.red);
g.fillRect(size.width/2-25,size.height/2-25,50,50);
publicDimensiongetPreferredSize(){
returnnewDimension(100,100);
4.3.7立即绘制Swing组件
例4-8在事件处理方法中使用paintImmediately()
importjava.awt.event.*;
publicclassTestAppletextendsJApplet{
finalJPanelpanel=newJPanel();
JButtonbutton=newJButton("
repaint"
panel.setBackground(Color.blue);
panel.setPreferredSize(newDimension(100,100));
contentPane.setLayout(newFlowLayout());
contentPane.add(button);
contentPane.add(panel);
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
Colorc=panel.getBackground();
Dimensionsz=panel.getSize();
panel.setBackground(
c==Color.blue?
Color.red:
Color.blue);
panel.paintImmediately(
0,0,sz.width,sz.height);
//for