java2图形设计卷Ⅱ学习小结.doc_第1页
java2图形设计卷Ⅱ学习小结.doc_第2页
java2图形设计卷Ⅱ学习小结.doc_第3页
java2图形设计卷Ⅱ学习小结.doc_第4页
java2图形设计卷Ⅱ学习小结.doc_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java 2 图形设计卷学习小结4.轻量事件通知由“模型”一节我们知道,模型能够提供轻量通知和状态通知两种通知。轻量通知使用一个只知道事件源的ChangeEvent(变化事件),状态通知则使用提供有关变化的更多信息的事件。变化事件由一些事件来处理,这些对象的类实现ChangeListener接口。接口总结3-1对ChangeListener接口进行了总结。接口总结3-1 ChangeListenerpublic abstract void stateChanged (ChageEvent)与大多数监听器一样,ChangeListener接口只定义了一个方法。StateChanged方法以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一个滑杆的轻量通知 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 s = (JSlider) e.getSource();showStatus(Integer.toString(s.getValue();); 5.状态事件通知对不经常变化的模型属性,模型使用状态通知。状态通知激发所有类型的事件,并且提供比事件源更多信息(轻量事件通知仅提供事件源一种信息)。例如,当选取或取消选取一个单选按钮时,按钮模型将激发一个项事件。6.属性变化通知当模型的关联属性变化时(当一个属性的变化激发一个属性变化事件,则这个属性称作关联属性。)模型会以一个java.beans.PropertyChangeEvent的形式产生状态通知。属性变化通知由一些对象来处理,这些对象的类实现java.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 JScrollPane(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(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(PropertyChangeEvent 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 javax.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/ property 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 ImageIcon(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 bounds.x + bounds.width) / scroll rightviewPos.x -= increment;setViewPosition(viewPos);if(drag.x bounds.y + bounds.height) / scroll downviewPos.y -= increment;setViewPosition(viewPos);if(drag.y 0) / scroll upviewPos.y += increment;setViewPosition(viewPos););import javax.swing.*;public class AutoscrollTest extends JFramepublic AutoscrollTest() JLabel label = new JLabel(new ImageIcon(pic.gif);JViewport vp = new AutoscrollViewport(label, 3);getContentPane().add(vp, Center); this.setVisible(true); public static void main(String args) new AutoscrollTest(); 例4-21 用客户属性把一个动态目标分配给一个按钮import javax.swing.*;import java.util.*;import java.awt.*;import java.awt.event.*;import java.beans.*;public class ClientPropertiesTest extends JApplet JButton button = new JButton(toggle target color);JComboBox targetCombo = new JComboBox();JPanel targets = new JPanel(),new JPanel(),new JPanel() ;public void init() Container contentPane = getContentPane();Dimension targetPreferredSize = new Dimension(100,100);JPanel targetPanel = new JPanel();for(int i=0; i targets.length; +i) targetsi.setBackground(Color.blue);targetsi.setPreferredSize(targetPreferredSize);targetPanel.add(targetsi);targetCombo.addItem(left);targetCombo.addItem(center);targetCombo.addItem(right);contentPane.setLayout(new FlowLayout();contentPane.add(button);contentPane.add(targetCombo);contentPane.add(targetPanel);button.putClientProperty(target, targets0);button.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Component c = (Component)button.getClientProperty(target);if(c != null) Color bg = c.getBackground();c.setBackground(bg = Color.blue ?Color.red : Color.blue);c.repaint(););targetCombo.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) button.putClientProperty(target, targetstargetCombo.getSelectedIndex(););button.addPropertyChangeListener(new PropertyChangeListener() public void propertyChange(PropertyChangeEvent e) if(e.getPropertyName().equals(target) showStatus(String)targetCombo.getSelectedItem() + panel set as target););public static void main(String args) final JFrame f = new JFrame();JApplet applet = new ClientPropertiesTest();applet.init();f.setContentPane(applet.getContentPane();f.setBounds(100,100,300,250);f.setTitle(ClientPropertiesTest);f.setVisible(true);f.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) f.dispose();System.exit(0););class PanelWithTitle extends JPanel private String title;public PanelWithTitle(String title) this.title = title;public void paintComponent(Graphics g) FontMetrics fm = g.getFontMetrics();Dimension size = getSize();int titleW = fm.stringWidth(title);g.setColor(Color.black);g.drawString(title, size.width/2 - titleW/2, size.height/2);例5-6 使用定制边框import java.awt.*;import javax.swing.*;import javax.swing.border.*;public class HandleBorder extends AbstractBorder protected Color lineColor;protected int thick;public HandleBorder() this(Color.black, 6);public HandleBorder(Color lineColor, int thick) this.lineColor = lineColor;this.thick = thick;public void paintBorder(Component c, Graphics g, int x,int y, int w, int h) Graphics copy = g.create();if(copy != null) try copy.translate(x,y);paintRectangle(c,copy,w,h);paintHandles(c,copy,w,h);finally copy.dispose();public Insets getBorderInsets() return new Insets(thick,thick,thick,thick);protected void paintRectangle(Component c, Graphics g,int w, int h) g.setColor(lineColor);g.drawRect(thick/2,thick/2,w-thick-1,h-thick-1); /g.drawLine(w-thick,0,w-thick,h); g.drawLine(0,20,w,20);protected void paintHandles(Component c, Graphics g,int w, int h) g.setColor(lineColor);g.fillRect(0,0,thick,thick); / upper leftg.fillRect(w-thick,0,thick,thick); / upper right g.fillRect(0,h-thick,thick,thick); / lower leftg.fillRect(w-thick,h-thick,thick,thick); / lower rightg.fillRect(w/2-thick/2,0,thick,thick); / mid topg.fillRect(0,h/2-thick/2,thick,thick); / mid leftg.fillRect(w/2-thick/2,h-thick,thick,thick); / mid bottomg.fillRect(w-thick,h/2-thick/2,thick,thick); / mid right import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;public class Test extends JApplet public void init() Container contentPane = getContentPane();JPanel panels = new JPanel(), new JPanel(), new JPanel() ;Border borders = new HandleBorder(),new HandleBorder(Color.red, 8),new HandleBorder(Color.blue, 10) ;contentPane.setLayout(new FlowLayout(FlowLayout.CENTER,20,20);for(int i=0; i panels.length; +i) panelsi.setPreferredSize(new Dimension(100,100);panelsi.setBorder(bordersi);contentPane.add(panelsi);例5-8 ColorIcon类清单import com.sun.java.swing.JApplet;import java.awt.Color;import java.awt.Graphics;public class IconTest extends JApplet ColorIcon redIcon; ColorIcon blueIcon; ColorIcon yellowIcon; public void paint(Graphics g) redIcon.paintIcon(this, g, 0, 0); blueIcon.paintIcon(this, g, redIcon.getIconWidth() + 10, 0); yellowIcon.paintIcon(this, g, redIcon.getIconWidth() + 10 + blueIcon.getIconWidth() + 10, 0); public IconTest() redIcon = new ColorIcon(Color.red, 50, 50); blueIcon = new ColorIcon(Color.blue, 60, 60); yellowIcon = new ColorIcon(Color.yellow, 70, 70); import java.awt.*;import javax.swing.*;class ColorIcon implements Icon private Color fillColor;private int w, h;public ColorIcon(Color fillColor, int w, int h) this.fillColor = fillColor;this.w = w;this.h = h;public void paintIcon(Component c, Graphics g, int x, int y) g.setColor(Color.black);g.drawRect(x, y, w-1, h-1);g.setColor(fillColor);g.fillRect(x+1, y+1, w-2, h-2);public int getIconWidth() return w;public int getIconHeight() return h;例5-9 菜单项中的图标import java.awt.*;import javax.swing.*;class ColorIcon implements Icon private Color fillColor;private int w, h;public ColorIcon(Color fillColor, int w, int h) this.fillColor = fillColor;this.w = w;this.h = h;public void paintIcon(Component c, Graphics g, int x, int y) g.setColor(Color.black);g.drawRect(x, y, w-1, h-1);g.setColor(fillColor);g.fillRect(x+1, y+1, w-2, h-2);public int getIconWidth() return w;public int getIconHeight() return h;import java.awt.*;import javax.swing.*;public class Test extends JApplet ColorIcon redIcon = new ColorIcon(Color.red, 40, 15),blueIcon = new ColorIcon(Color.blue, 40, 15),yellowIcon = new ColorIcon(Color.yellow, 40, 15);public void init() JMenuBar mb = new JMenuBar();JMenu colors = new JMenu(Colors);colors.add(new JMenuItem(redIcon);colors.add(new JMenuItem(blueIcon);colors.add(new JMenuItem(yellowIcon);mb.add(colors);setJMenuBar(mb);例5-11 在许多组件中共享单图标的小应用程序import java.awt.*;import javax.swing.*;public class ColorIcon implements Icon private int w, h;public ColorIcon(int w, int h) this.w = w;this.h = h;public void paintIcon(Component c, Graphics g, int x, int y) Color fillColor = Color.lightGray;g.setColor(Color.black);g.drawRect(x, y, w-1, h-1);if(c instanceof JComponent) JComponent jc = (JComponent)c;fillColor = (Color)jc.getClientProperty(fill color);g.setColor(fillColor);g.fillRect(x+1, y+1, w-2, h-2);public int getIconWidth() return w;public int getIconHeight() return h;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JApplet private ColorIcon colorIcon = new ColorIcon(40, 15); private JPopupMenu popup = new JPopupMenu();private JButton button = new JButton(select a color .,colorIcon);public void init() addPopupMenuItems();button.putClientProperty(fill color, Color.red);Container contentPane = getContentPane();contentPane.setLayout(new FlowLayout();contentPane.add(button);button.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Dimension buttonsz = button.getSize();popup.show(button,buttonsz.width,buttonsz.height););private void addPopupMenuItems() JMenuItem redItem = new JMenuItem(colorIcon),blueItem = new JMenuItem(colorIcon),grayItem = new JMenuItem(colorIcon),yellowItem = new JMenuItem(colorIcon),blackItem = new JMenuItem(colorIcon),whiteItem = new JMenuItem(colorIcon),orangeItem = new JMenuItem(colorIcon);MenuItemListener listener = new MenuItemListener();redItem.putClientProperty(fill color, Color.red);redItem.addActionListener(listener);popup.add(redItem);blueItem.putClientProperty(fill color, Color.blue);blueItem.addActionListener(listener);popup.add(blueItem);grayItem.putClientProperty(fill color, Color.gray);grayItem.addActionListener(listener);popup.add(grayItem);yellowItem.putClientProperty(fill color, Color.yellow);yellowItem.addActionListener(listener);popup.add(yellowItem);blackItem.putClientProperty(fill color, Color.black);blackItem.addActionListener(listener);popup.add(blackItem);whiteItem.putClientProperty(fill color, Color.white);whiteItem.addActionListener(listener);popup.add(whiteItem);orangeItem.putClientProperty(fill color, Color.orange);orangeItem.addActionListener(listener);popup.add(orangeItem);class MenuItemListener implements ActionListener public void actionPerformed(ActionEvent e) JComponent jc = (JComponent)e.getSource();button.putClientProperty(fill color,jc.getClientProperty(fill color);button.repaint();import java.awt.*;import javax.swing.*;public class Test extends JFrame public Test() this.setSize(300,300); JPanel panel = new MyJP

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论