高分请教 关于 Core Java 2 Volume II 中的 FileNameBean 无法实例化的问题 我没有 Core Java 2 Volume II请把相关 FileNameBean 的代码贴出来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 file105. */106. public String getFileName()107. {108. return nameField.getText();109. }110.111. /**112. Sets the defaultExtension property.113. @param s the new default extension114. */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 chooser123. */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. } Forte 的信息显示好像没有读写文件的权限, 等待 Java中抽象类不能实例化,这边是不是进行实例化了?不懂 面临工作选择,各位能指点指点吗? JAVA防止内存溢出和加快内存回收 工具的选择..(菜鸟哦) 什么是参考变量?急!谢谢大家 这是一段UDP server/client 的echo,大家谁能帮我将他编辑的能运行了,好象要加些语句就可以,谢谢 关于MessageFormat中小数点后面0的问题---急! JAVA中如何得到系统的日期和时间? 以下的各句代码什么意思.. 新人运行程序,package 报错,求解答 printWriter的问题 谁能帮我介绍几个关于java 或vc/c++的网站或论坛
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. }