【发布时间】:2014-09-12 10:01:54
【问题描述】:
我通过自定义 JFileChooser 打开文件然后我添加 JComboBox。当我从 JComboBox 中选择一个为特定文件执行的特定操作时。例如,我的文本文件包含一些文本,如“一旦我们完成”。当我选择二进制形式 JComboBox。它显示在文本区域中,如“ 63 6F FD 70 6C 65 74 一旦我们完成”。
这是我的代码,
public class CustomJFileChooser {
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();
}
}
});
final JFileChooser chooser = new JFileChooser();
JComponent panel = new JPanel((LayoutManager) new FlowLayout( FlowLayout.LEFT));
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] { "text", "binary" }));
panel.add(new JLabel("FileFormat: "));
panel.add(comboBox);
chooser.setAccessory(panel);
JComponent center = null;
BorderLayout layout = (BorderLayout) chooser.getLayout();
for (Component child : chooser.getComponents()) {
if (BorderLayout.CENTER == layout.getConstraints(child)) {
center = (JComponent) child;
}
}
if (center != null)
center.add(panel, BorderLayout.SOUTH);
final JFrame frame = new JFrame();
JButton button = new JButton("Open File Chooser");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooser.showOpenDialog(frame);
}
});
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
谢谢。
【问题讨论】:
-
问题是什么?
-
wat 正是你想要实现的,你有一个错误 FileChooser frame = new FileChooser();它应该是 JFrame 而不是 FileChooser()
-
当从下拉框中选择二进制项目时,我需要以 textarea 二进制格式打开文件。 aboue 示例正常工作。@mussdroid
标签: java swing action customization jfilechooser