【发布时间】:2014-11-05 07:59:40
【问题描述】:
当我通过 CustomizedJFileChooser 打开对话框时。 JfileChooser 的外观和感觉都不好。所以,为了外观和感觉,我正在添加代码
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
它引发了异常。
这是我的代码,
public class FileChooser extends JFrame {
private JPanel contentPane;
MyFileChooser jc;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
FileChooser frame = new FileChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FileChooser() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
jc = new MyFileChooser();
JButton btnOpen = new JButton("open");
contentPane.add(btnOpen, BorderLayout.NORTH);
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = jc.showOpenDialog(FileChooser.this);
}
});
pack();
}
}
class MyFileChooser extends JFileChooser{
public MyFileChooser() {
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] { "text", "binary" }));
JPanel panel1 = (JPanel)this.getComponent(3);
JPanel panel2 = (JPanel) panel1.getComponent(3);
Component c1=panel2.getComponent(0);
Component c2=panel2.getComponent(1);
panel2.removeAll();
panel2.add(new JLabel("Document Name: "));
panel2.add(comboBox);
panel2.add(c1);
panel2.add(c2);
}
}
这里是异常的堆栈跟踪:
java.lang.ArrayIndexOutOfBoundsException: No such child: 3
at java.awt.Container.getComponent(Container.java:327)
at MyFileChooser.<init>(FileChooser.java:62)
at FileChooser.<init>(FileChooser.java:41)
at FileChooser$1.run(FileChooser.java:27)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
谢谢。
【问题讨论】:
-
你能添加堆栈跟踪吗?
-
java.lang.ArrayIndexOutOfBoundsException: No such child: 3我认为你的问题在于JPanel panel1 = (JPanel)this.getComponent(3);你的JPanel 不包含第三个元素。 -
是的,你的正确。但是当我删除上面的单行代码时,它工作正常,但外观和感觉都不好。但是,我需要外观和感觉。如何为自定义文件选择器添加外观。 @Alexander_Winter
-
不同的外观和感觉会以不同的方式对待组件的布局。您的自定义实现可能不适用于所有外观。考虑也许只是使用
JFileChooser.setAcessory来避免这个问题。如here所见。或见the tutorial -
您能否编辑您的帖子以显示新代码以便我们进行测试。
标签: java swing indexoutofboundsexception look-and-feel jfilechooser