我想首先说我是摇摆新手。因此,如果使用 JFileChooser 选择目录,我要做的所有事情。我可以打开、导航和选择一个目录。当我按下任何关闭对话框的按钮时,问题就出现了。当我这样做时,我的应用程序就会冻结。当它冻结时,只有对话框返回的面板变成白色。当我使用调试器时,在对话框关闭并且未到达 if 语句之后立即发生挂起。如果这有所不同,我也在 Eclipse 插件中执行此操作。特别是它托管在视图内部。下面的代码:
public class TexturePacker extends ViewPart {
public void createPartControl(Composite parent) {
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
frame = SWT_AWT.new_Frame(composite);
frame.add(new TexturePackerPanel(frame));
}
}
public class TexturePackerPanel extends JPanel {
//This is called from initialize(), which is called in the constructor
private void initializeConfigPanel() {
JPanel configPanel = new JPanel();
JTextBoxk outputDirectory = new JTextField();
configPanel.add(inputDirectory);
JButton fileButton = new JButton("Folder");
fileButton.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
JFileChooser file = new JFileChooser(outputDirectory.getText());
file.setDialogTitle("Select Output Directory");
file.setDialogType(JFileChooser.OPEN_DIALOG);
file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = file.showDialog(frame, "Choose");
if(returnVal == JFileChooser.APPROVE_OPTION) {
outputDirectory.setText(file.getSelectedFile().getAbsolutePath());
}
}
//Other blank MouseListener methods//
});
configPanel.add(fileButton);
}
}
系统信息:
Windows 8 64bit
Java 7
Eclipse 4.2 SR1 EE Edition
我很确定这个问题是由于 Swing 在 Eclipse 中不能很好地运行造成的。我已经使用 SWT 目录对话框成功地让它工作了。所以我打算将整个 JPanel 转换为 SWT。感谢大家的帮助,我现在对 Swing 的工作原理有了更多的了解。