我没有  Core Java 2 Volume II请把相关 FileNameBean 的代码贴出来

解决方案 »

  1.   

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

  2.   

    Forte 的信息显示好像没有读写文件的权限, 等待