gpt4 book ai didi

java - 更改自定义 JFileChooser 的外观会引发异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:34:16 25 4
gpt4 key购买 nike

当我通过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)

谢谢。

最佳答案

组件列表具有从零开始的索引。像下面这样修改这两行就可以了。 MyFileChooserpanel1 位于索引 2 处,而 panel1 的另一个面板也位于索引 2 处。

JPanel panel1 = (JPanel) this.getComponent(2);
JPanel panel2 = (JPanel) panel1.getComponent(2);

您忘记将第三个组件添加到panel2。组合框已撕裂,但您可以随后修复其高度。

我的错误,我忘记添加包含按钮的面板。我还将组合框添加到了自己的面板中,现在它不再撕裂了。现在应该可以正常工作了。

class MyFileChooser extends JFileChooser {
public MyFileChooser() {
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[]{"text", "binary"}));

JPanel panel1 = (JPanel) this.getComponent(2);
JPanel panel2 = (JPanel) panel1.getComponent(2);

Component c1=panel2.getComponent(0);
Component c2=panel2.getComponent(1);
Component c3=panel2.getComponent(2);
Component c4=panel2.getComponent(3);
Component c5=panel2.getComponent(4);

JPanel comboboxPanel = new JPanel();
comboboxPanel.setLayout(new FlowLayout());
comboboxPanel.add(new JLabel("Document Name: "));
comboboxPanel.add(comboBox);

panel2.removeAll();
panel2.add(comboboxPanel);
panel2.add(Box.createRigidArea(new Dimension(5,5)));
panel2.add(c1);
panel2.add(c2);
panel2.add(c3);
panel2.add(c4);
panel2.add(c5);
}

}

关于java - 更改自定义 JFileChooser 的外观会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25786269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com