1、java2图形设计卷学习小结java 2 图形设计卷学习小结4.轻量事件通知由“模型”一节我们知道,模型能够提供轻量通知和状态通知两种通知。轻量通知使用一个只知道事件源的ChangeEvent(变化事件),状态通知则使用提供有关变化的更多信息的事件。变化事件由一些事件来处理,这些对象的类实现ChangeListener接口。接口总结3-1对ChangeListener接口进行了总结。接口总结3-1 ChangeListenerpublic abstract void stateChanged (ChageEvent)与大多数监听器一样,ChangeListener接口只定义了一个方法。Stat
2、eChanged方法以ChangeEvent的一个实例作为参数。类总结3-1中介绍了ChangeEvent类。类总结3-1 ChangeEvent扩展:java.util.EventObject构造方法public ChageEvent(Object source)ChangeEvent类仅提供了一个构造方法,没有提供其他方法。ChangeEvent构造方法以事件源作为参数。图3-9所示的小应用程序通过监控滑杆的值来说明轻量通知。一个变化监听器添加到这个小应用程序的滑杆中以获得滑杆值并更新这个小应用程序的状态区。图3-9轻量通知例3-2列出了图3-9所示的小应用程序的代码例3-2一个滑杆的轻量
3、通知 import java.awt.*;import javax.swing.*;import javax.swing.event.*;public class Test extends JAppletpublic void init()JSlider slider = new JSlider(0,100,50);getContentPane().add(slider,BorderLayout.CENTER);slider.addChangeListener(new ChangeListener()public void stateChanged(ChangeEvent e)JSlider
4、s = (JSlider) e.getSource();showStatus(Integer.toString(s.getValue();); 5.状态事件通知对不经常变化的模型属性,模型使用状态通知。状态通知激发所有类型的事件,并且提供比事件源更多信息(轻量事件通知仅提供事件源一种信息)。例如,当选取或取消选取一个单选按钮时,按钮模型将激发一个项事件。6.属性变化通知当模型的关联属性变化时(当一个属性的变化激发一个属性变化事件,则这个属性称作关联属性。)模型会以一个java.beans.PropertyChangeEvent的形式产生状态通知。属性变化通知由一些对象来处理,这些对象的类实现j
5、ava.beans.PropertyChangeListener接口,接口总结3-2中总结了这个接口。import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.beans.*;public class Test extends JFrame JTree tree = new JTree();public Test() Container contentPane = getContentPane();JScrollPane scrollPane = new JScro
6、llPane(tree);contentPane.add(new ControlPanel(), BorderLayout.NORTH);contentPane.add(scrollPane, BorderLayout.CENTER);tree.addPropertyChangeListener(new PropertyListener();class ControlPanel extends JPanel JCheckBox showRoot = new JCheckBox(show root node);public ControlPanel() showRoot.setSelected(
7、tree.isRootVisible();setLayout(new FlowLayout();add(showRoot);showRoot.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) tree.setRootVisible(showRoot.isSelected(); );class PropertyListener implements PropertyChangeListener public void propertyChange(PropertyChangeEven
8、t e) String name = e.getPropertyName();if(name.equals(JTree.ROOT_VISIBLE_PROPERTY) String msg = Root Visible Property: +e.getNewValue().toString();JOptionPane.showMessageDialog(Test.this, / parent compmsg, / message Property Change, / titleJOptionPane.INFORMATION_MESSAGE);例4-7 不容透明Swing组件import java
9、x.swing.*;import java.awt.*; public class OpaqueTest extends JApplet public void init() Container contentPane = getContentPane();RainPanel rainPanel = new RainPanel();ColoredPanel opaque = new ColoredPanel(),transparent = new ColoredPanel();/ JComponents are opaque by default, so the opaque/ propert
10、y only needs to be set for transparenttransparent.setOpaque(false);rainPanel.add(opaque);rainPanel.add(transparent);contentPane.add(rainPanel, BorderLayout.CENTER);System.out.println(opaque.isOpaque();System.out.println(transparent.isOpaque();class RainPanel extends JPanel ImageIcon rain = new Image
11、Icon(this.getClass().getResource(rain.gif);private int rainw = rain.getIconWidth();private int rainh = rain.getIconHeight();public void paintComponent(Graphics g) Dimension size = getSize();for(int row=0; row size.height; row += rainh)for(int col=0; col size.width; col += rainw)rain.paintIcon(this,g
12、,col,row);class ColoredPanel extends JPanel public void paintComponent(Graphics g) super.paintComponent(g);Dimension size = getSize();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);public Dimension getPrefer
13、redSize() return new Dimension(100,100);4.3.7 立即绘制Swing组件例4-8 在事件处理方法中使用paintImmediately()import javax.swing.*;import java.awt.*;import java.awt.event.*; public class TestApplet extends JApplet public void init() Container contentPane = getContentPane();final JPanel panel = new JPanel();JButton butt
14、on = new JButton(repaint);panel.setBackground(Color.blue);panel.setPreferredSize(new Dimension(100,100);contentPane.setLayout(new FlowLayout();contentPane.add(button);contentPane.add(panel);button.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Color c = panel.getBackground();Dimension sz = panel.getSize();panel.setBackground(c = Color.blue ? Color.red : Color.blue);panel.paintImmediately(0,0,sz.width,sz.height);/ for
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1