楼主: ReneeBK
1192 5

Core Java™ 2 Volume II - Advanced Features, Seventh [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4898份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49640 个
通用积分
55.8137
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2015-7-27 01:04:13 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

  • Core Java™ 2 Volume II - Advanced Features, Seventh Edition
  • By: Cay S. Horstmann; Gary Cornell

  • Publisher: Prentice Hall

  • Pub. Date: November 22, 2004   Most Recent Edition

  • Print ISBN-10: 0-13-111826-9

  • Print ISBN-13: 978-0-13-111826-3

  • Pages in Print Edition: 1024

  • Subscriber Rating: [0 Ratings]



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:features Advanced Seventh ADVANCE feature Java

本帖被以下文库推荐

沙发
ReneeBK 发表于 2015-7-27 01:10:21

The Bean-Writing Process

  1. Code View: Scroll / Show All
  2. 1. package com.horstmann.corejava;
  3. 2.
  4. 3. import java.awt.*;
  5. 4. import java.io.*;
  6. 5. import javax.imageio.*;
  7. 6. import javax.swing.*;
  8. 7.
  9. 8. /**
  10. 9.    A bean for viewing an image.
  11. 10. */
  12. 11. public class ImageViewerBean extends JLabel
  13. 12. {
  14. 13.
  15. 14.    public ImageViewerBean()
  16. 15.    {
  17. 16.       setBorder(BorderFactory.createEtchedBorder());
  18. 17.    }
  19. 18.
  20. 19.    /**
  21. 20.       Sets the fileName property.
  22. 21.       @param fileName the image file name
  23. 22.    */
  24. 23.    public void setFileName(String fileName)
  25. 24.    {
  26. 25.       try
  27. 26.       {
  28. 27.          file = new File(fileName);
  29. 28.          setIcon(new ImageIcon(ImageIO.read(file)));
  30. 29.       }
  31. 30.       catch (IOException e)
  32. 31.       {
  33. 32.          file = null;
  34. 33.          setIcon(null);
  35. 34.       }
  36. 35.    }
  37. 36.
  38. 37.    /**
  39. 38.       Gets the fileName property.
  40. 39.       @return the image file name
  41. 40.    */
  42. 41.    public String getFileName()
  43. 42.    {
  44. 43.       if (file == null) return null;
  45. 44.       else return file.getPath();
  46. 45.    }
  47. 46.
  48. 47.    public Dimension getPreferredSize()
  49. 48.    {
  50. 49.       return new Dimension(XPREFSIZE, YPREFSIZE);
  51. 50.    }
  52. 51.
  53. 52.    private File file = null;
  54. 53.    private static final int XPREFSIZE = 200;
  55. 54.    private static final int YPREFSIZE = 200;
  56. 55. }


  57.                                           
复制代码

藤椅
ReneeBK 发表于 2015-7-27 01:14:34
Simple Properties

A simple property is one that takes a single value such as a string or a number. The fileName property of the ImageViewer is an example of a simple property. Simple properties are easy to program: Just use the set/get naming convention we indicated earlier. For example, if you look at the code in Example 8-1, you can see that all it took to implement a simple string property is the following:

[size=28.3500003814697px]
  1. public void setFileName(String f)
  2. {
  3.    fileName = f;
  4.    image = . . .
  5.    repaint();
  6. }

  7. public String getFileName()
  8. {
  9.    if (file == null) return null;
  10.    else return file.getPath();
  11. }
复制代码



板凳
ReneeBK 发表于 2015-7-27 01:16:55
Indexed Properties

An indexed property is one that gets or sets an array. A chart bean (see below) would use an indexed property for the data points. With an indexed property, you supply two pairs of get and set methods: one for the array and one for individual entries. They must follow this pattern:


Type[] getPropertyName()
void setPropertyName(Type[] x)
Type getPropertyName(int i)
void setPropertyName(int i, Type x)

Here's an example of the indexed property we use in the chart bean that you will see later in this chapter.

  1. public double[] getValues() { return values; }
  2. public void setValues(double[] v) { values = v; }
  3. public double getValues(int i)
  4. {
  5.    if (0 <= i && i < values.length) return values[i];
  6.    return 0;
  7. }
  8. public void setValues(int i, double value)
  9. {
  10.    if (0 <= i && i < values.length) values[i] = value;
  11. }
  12. . . .
  13. private double[] values;
复制代码


The


setPropertyName(int i, Type x)

method cannot be used to grow the array. To grow the array, you must manually build a new array and then pass it to this method:



报纸
ReneeBK 发表于 2015-7-27 01:20:00
Constrained Properties

[size=28.3500003814697px]A constrained property is constrained by the fact that any listener can “veto” proposed changes, forcing it to revert to the old setting. The Java library contains only a few examples of constrained properties. One of them is the closed property of the JInternalFrame class. If someone tries to callsetClosed(true) on an internal frame, then all of its VetoableChangeListeners are notified. If any of them throws a PropertyVetoException, then the closed property is not changed, and the setClosedmethod throws the same exception. For example, a VetoableChangeListener may veto closing the frame if its contents have not been saved.

[size=28.3500003814697px]To build a constrained property, your bean must have the following two methods to manageVetoableChangeListener objects:

[size=28.3500003814697px]public void addVetoableChangeListener(VetoableChangeListener listener);public void removeVetoableChangeListener(VetoableChangeListener listener);

[size=28.3500003814697px]Just as there is a convenience class to manage property change listeners, there is a convenience class, called VetoableChangeSupport, that manages vetoable change listeners. Your bean should contain an object of this class.

[size=28.3500003814697px]
private VetoableChangeSupport vetoSupport = new VetoableChangeSupport(this);


[size=28.3500003814697px]Adding and removing listeners should be delegated to this object. For example:

[size=28.3500003814697px]
  1. public void addVetoableChangeListener(VetoableChangeListener listener)
  2. {
  3.    vetoSupport.addVetoableChangeListener(listener);
  4. }
  5. public void removeVetoableChangeListener(VetoableChangeListener listener)
  6. {
  7.    vetoSupport.removeVetoableChangeListener(listener);
  8. }
复制代码



地板
ReneeBK 发表于 2015-7-27 01:21:18

Example 8-2. FileNameBean.java

  1. 1. package com.horstmann.corejava;
  2.   2.
  3.   3. import java.awt.*;
  4.   4. import java.awt.event.*;
  5.   5. import java.beans.*;
  6.   6. import java.io.*;
  7.   7. import javax.swing.*;
  8.   8.
  9.   9. /**
  10. 10.    A bean for specifying file names.
  11. 11. */
  12. 12. public class FileNameBean extends JPanel
  13. 13. {
  14. 14.    public FileNameBean()
  15. 15.    {
  16. 16.       dialogButton = new JButton("...");
  17. 17.       nameField = new JTextField(30);
  18. 18.
  19. 19.       chooser = new JFileChooser();
  20. 20.
  21. 21.       chooser.setFileFilter(new
  22. 22.          javax.swing.filechooser.FileFilter()
  23. 23.          {
  24. 24.             public boolean accept(File f)
  25. 25.             {
  26. 26.                String name = f.getName().toLowerCase();
  27. 27.                return name.endsWith("." + defaultExtension) || f.isDirectory();
  28. 28.             }
  29. 29.             public String getDescription()
  30. 30.             {
  31. 31.                return defaultExtension + " files";
  32. 32.             }
  33. 33.          });
  34. 34.
  35. 35.       setLayout(new GridBagLayout());
  36. 36.       GridBagConstraints gbc = new GridBagConstraints();
  37. 37.       gbc.weightx = 100;
  38. 38.       gbc.weighty = 100;
  39. 39.       gbc.anchor = GridBagConstraints.WEST;
  40. 40.       gbc.fill = GridBagConstraints.BOTH;
  41. 41.       gbc.gridwidth = 1;
  42. 42.       gbc.gridheight = 1;
  43. 43.       add(nameField, gbc);
  44. 44.
  45. 45.       dialogButton.addActionListener(
  46. 46.          new ActionListener()
  47. 47.          {
  48. 48.             public void actionPerformed(ActionEvent event)
  49. 49.             {
  50. 50.                int r = chooser.showOpenDialog(null);
  51. 51.                if(r == JFileChooser.APPROVE_OPTION)
  52. 52.                {
  53. 53.                   File f = chooser.getSelectedFile();
  54. 54.                   try
  55. 55.                   {
  56. 56.                      String name = f.getCanonicalPath();
  57. 57.                      setFileName(name);
  58. 58.                   }
  59. 59.                   catch (IOException e)
  60. 60.                   {
  61. 61.                   }
  62. 62.                }
  63. 63.             }
  64. 64.          });
  65. 65.       nameField.setEditable(false);
  66. 66.
  67. 67.       gbc.weightx = 0;
  68. 68.       gbc.anchor = GridBagConstraints.EAST;
  69. 69.       gbc.fill = GridBagConstraints.NONE;
  70. 70.       gbc.gridx = 1;
  71. 71.       add(dialogButton, gbc);
  72. 72.    }
  73. 73.
  74. 74.    /**
  75. 75.       Sets the fileName property.
  76. 76.       @param newValue the new file name
  77. 77.    */
  78. 78.    public void setFileName(String newValue)
  79. 79.    {
  80. 80.       String oldValue = nameField.getText();
  81. 81.       nameField.setText(newValue);
  82. 82.       firePropertyChange("fileName", oldValue, newValue);
  83. 83.    }
  84. 84.
  85. 85.    /**
  86. 86.       Gets the fileName property.
  87. 87.       @return the name of the selected file
  88. 88.    */
  89. 89.    public String getFileName()
  90. 90.    {
  91. 91.       return nameField.getText();
  92. 92.    }
  93. 93.
  94. 94.    /**
  95. 95.       Sets the defaultExtension property.
  96. 96.       @param s the new default extension
  97. 97.    */
  98. 98.    public void setDefaultExtension(String s)
  99. 99.    {
  100. 100.       defaultExtension = s;
  101. 101.    }
  102. 102.
  103. 103.    /**
  104. 104.       Gets the defaultExtension property.
  105. 105.       @return the default extension in the file chooser
  106. 106.    */
  107. 107.    public String getDefaultExtension()
  108. 108.    {
  109. 109.       return defaultExtension;
  110. 110.    }
  111. 111.
  112. 112.    public Dimension getPreferredSize()
  113. 113.    {
  114. 114.       return new Dimension(XPREFSIZE, YPREFSIZE);
  115. 115.    }
  116. 116.
  117. 117.    private static final int XPREFSIZE = 200;
  118. 118.    private static final int YPREFSIZE = 20;
  119. 119.    private JButton dialogButton;
  120. 120.    private JTextField nameField;
  121. 121.    private JFileChooser chooser;
  122. 122.    private String defaultExtension = "gif";
  123. 123. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-10 05:45