看来你的需求是使FileChoose用于选择目录,个人认为没有必要单独做个类,用户更愿意接受通用的对话框,如Windows对话框中用于选择目录时并没有把“文件名”改为“路径”,且默认情况下你没有对FIleChoose添加文件类型此选项相当于没有启用。你的需求仅需一个方法调用即可,演示程序如下:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&F: " + exc);
}
final JFrame f = new JFrame();
JButton b = new JButton("Test");
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//HERE
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retval = fc.showOpenDialog(f);
if (retval == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
JOptionPane.showMessageDialog(f,
"你选择的目录是: \n" + file.getPath());
} else if (retval == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(f,
"用户点击取消,未选择任何目录");
} else if (retval == JFileChooser.ERROR_OPTION) {
JOptionPane.showMessageDialog(f,
"发生错误,未选择任何目录");
} else {
JOptionPane.showMessageDialog(f,
"未知的操作");
}
}
});
f.getContentPane().add(b);
f.setSize(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}个人浅见。
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&F: " + exc);
}
final JFrame f = new JFrame();
JButton b = new JButton("Test");
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//HERE
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retval = fc.showOpenDialog(f);
if (retval == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
JOptionPane.showMessageDialog(f,
"你选择的目录是: \n" + file.getPath());
} else if (retval == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(f,
"用户点击取消,未选择任何目录");
} else if (retval == JFileChooser.ERROR_OPTION) {
JOptionPane.showMessageDialog(f,
"发生错误,未选择任何目录");
} else {
JOptionPane.showMessageDialog(f,
"未知的操作");
}
}
});
f.getContentPane().add(b);
f.setSize(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}个人浅见。
import javax.swing.filechooser.*;
import java.awt.*;
import javax.accessibility.*;public class TestFileChooser{
public static void getJComponent(AccessibleContext ac, int lev){
for(int j=0;j<lev;++j)
System.out.print(" ");
System.out.println(ac.getClass().getName() + " " + ac.getAccessibleText() + " " + ac.getAccessibleName());
int len = ac.getAccessibleChildrenCount();
for(int i=0;i<len;++i){
Accessible acc = ac.getAccessibleChild(i);
if(acc != null){
TestFileChooser.getJComponent(acc.getAccessibleContext(), lev+1);
}
}
}
public static void main(String[] args) {
JFileChooser fc = new JFileChooser();
//显示“文件名:”所在的树
TestFileChooser.getJComponent(fc.getAccessibleContext(), 0);
AccessibleContext lab = fc.getAccessibleContext().getAccessibleChild(2).getAccessibleContext().getAccessibleChild(0).getAccessibleContext().getAccessibleChild(0).getAccessibleContext();
lab.setAccessibleName("路径:");
fc.updateUI();
fc.showOpenDialog(null);
}
}
if(this.approveButtonText == approveButtonText) {
return;
}
String oldValue = this.approveButtonText;
this.approveButtonText = approveButtonText;
firePropertyChange(APPROVE_BUTTON_TEXT_CHANGED_PROPERTY, oldValue, approveButtonText);
}APPROVE_BUTTON_TEXT_CHANGED_PROPERTY="ApproveButtonTextChangedProperty";firePropertyChange是JComponent的方法,该方法又会调用内部一个SwingPropertyChangeSupport类对象的firePropertyChange方法,SwingPropertyChangeSupport居然继承的是java.beans.PropertyChangeSupport
,这里又涉及到javaBean的东西,所以你必须知道修改含用“文件名:”的那个JLabel的具体的Property的名字才允许修改,我再试试啊
{
public static void main(String[] args)
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
JLabel label = (JLabel) ((Container) ((Container) fc.getComponent(5)).getComponent(1)).getComponent(0);
label.setText("目录名:");
fc.showOpenDialog(null);
System.exit(0);
}
}
{
public static void main(String[] args)
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
replaceLabelText(fc, "文件名:", "目录名:");
fc.showOpenDialog(null);
System.exit(0);
} private static void replaceLabelText(Component c, String oldLabelText, String newLabelText)
{
if(c instanceof JLabel)
{
JLabel label = (JLabel) c;
if(label.getText().equals(oldLabelText))
{
label.setText(newLabelText);
return;
}
}
else if(c instanceof Container)
{
Container container = (Container) c;
int count = container.getComponentCount();
for(int i=0; i<count; i++)
replaceLabelText(container.getComponent(i), oldLabelText, newLabelText);
}
}
}
PropertyChangeListener[] pcl = fc.getPropertyChangeListeners();
for(int i=0; i<pcl.length; ++i)
System.out.println(pcl[i].getClass().getName());
BasicDirectoryModel bdm = (BasicDirectoryModel) pcl[0];
MetalFileChooserUI mfc = (MetalFileChooserUI) pcl[1]; //出错<-----
|
/* 输出结果 |
|
java.lang.ClassCastException |
at test.TestFileChooser.main(TestFileChooser.java:31) ----
javax.swing.plaf.basic.BasicDirectoryModel
javax.swing.plaf.metal.MetalFileChooserUI$12
javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel
Exception in thread "main" */后来我看了一下第三个MetalFileChooserUI$FilterComboBoxModel,这是MetalFileChooserUI类里的一个proctect内部类叫FilterComboBoxModel,
所以MetalFileChooserUI$12应该是MetalFileChooserUI里的一个叫12的内部类,但是Java规定所有的类名都要以“字母”开头(我确定了一下,那第一个字符不是字母l,就是数字1)!!!所以第二个对象根本就得不到啊!!!估计那是Java虚拟机在运行时临时生成的一个类,用完后马上撤消。但是下面这段代码却又表明这不一定就是临时生成的 JFileChooser fc = new JFileChooser();
PropertyChangeListener[] pcl = fc.getPropertyChangeListeners();
System.out.println(pcl[1].getClass().getName());
JFileChooser fc2 = new JFileChooser();
pcl = fc2.getPropertyChangeListeners();
System.out.println(pcl[1].getClass().getName());
/* 输出结果javax.swing.plaf.metal.MetalFileChooserUI$12
javax.swing.plaf.metal.MetalFileChooserUI$12*/
按理说临时生成的就应该每次生成的名字都不一样吧,可他就认准12了不知道怎么回事了,哪位高手帮忙解释一下啊,关注ing
public class TestFileChooser{
public static void main(String[] args) {
JFileChooser fc = new JFileChooser();
//删除含有“文件类型”的组件
((Container)(((Container)fc).getComponent(2))).remove(2);
//将含有“文件名:”的组件内容改为“路径名:”
((JLabel)((Container)((Container)(((Container)fc).getComponent(2))).getComponent(0)).getComponent(0)).setText("路径名:");
fc.showOpenDialog(null);
System.exit(0);
}
}原因你可以运行一下下面这个函数
/**
初始调用为
JFileChooser fc = new JFileChooser();
prnCop(fc, 0);
*/ private static void prnCop(Component c, int lev) {
for (int i = 0; i < lev; ++i)
System.out.print(" ");
System.out.println(c);
if (c instanceof Container) {
Container container = (Container) c;
int count = container.getComponentCount();
for (int i = 0; i < count; i++)
prnCop(container.getComponent(i), lev + 1);
}
if(c instanceof JLabel && ((JLabel)c).getText().equals("文件名:"))
System.out.println("-----我在这哪!------");
}
((Container)(((Container)fc).getComponent(2))).remove(2);
//将含有“文件名:”的组件内容改为“路径名:”
((JLabel)((Container)((Container)(((Container)fc).getComponent(2))).getComponent(0)).getComponent(0)).setText("路径名:");
我不能用这段代码,总是报错。请问里边的数字“2”和“0”是什么意思?