我通过定制的JFileChooser打开文件然后添加JComboBox。当我从JComboBox中为特定文件执行特定操作时选择一个。例如,我的文本文件包含一些文本,如“一旦我们完成”。当我选择二进制形式JComboBox。它在textarea中显示,例如“63 6F FD 70 6C 65 74一旦我们完成”。在定制的JFileChooser中,从JComboBox中选择一个执行该特定动作
这里是我的代码,
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);
}
}
谢谢。
问题是什么? – kiheru 2014-09-12 10:02:43
笏正好你想实现你有错误FileChooser frame = new FileChooser();它应该是JFrame而不是FileChooser() – mussdroid 2014-09-12 10:45:33
恰好我需要的文件应该是以textarea二进制格式打开时从下拉框中选择的二进制文件。 aboue示例工作正常。@ mussdroid – user3531731 2014-09-12 10:55:22