0

我想首先说我是摇摆新手。因此,如果使用 JFileChooser 选择目录,我要做的所有事情。我可以打开、导航和选择一个目录。当我按下任何关闭对话框的按钮时,问题就出现了。当我这样做时,我的应用程序就会冻结。当它冻结时,只有对话框返回的面板变成白色。当我使用调试器时,在对话框关闭并且未到达 if 语句之后立即发生挂起。如果这有所不同,我也在 E​​clipse 插件中执行此操作。特别是它托管在视图内部。下面的代码:

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 的工作原理有了更多的了解。

4

1 回答 1

0

为此,您确实应该使用 ActionListener 而不是 MouseListener。

除此之外,我认为这是一个线程问题。您应该在swing中阅读有关并发的文档。

JFileChooser_

SwingUtilities.invokeLater(new Runnable() {
public void run() {
    \\Your JFileChooser bit
}

编辑1:

我已经运行了以下稍微编辑过的代码,但无法重现您的问题

public static void main(final String[] args){

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            final JPanel configPanel = new JPanel();
            final JButton fileButton = new JButton("Folder");
            final JFrame frame = new JFrame();
            frame.setVisible(true);
            frame.add(configPanel);
            //      JButton outputDirectory = new JButton("XX");
            fileButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(final ActionEvent arg0) {
                    final JFileChooser file = new JFileChooser();
                    file.setDialogTitle("Select Output Directory");
                    file.setDialogType(JFileChooser.OPEN_DIALOG);
                    file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                    final int returnVal = file.showDialog(frame, "Choose");
                    if(returnVal == JFileChooser.APPROVE_OPTION) {
                        // outputDirectory.setText(file.getSelectedFile().getAbsolutePath());
                        System.out.println(returnVal);
                    }

                }
            });
            configPanel.add(fileButton);
        }
    });
}
于 2013-01-09T14:58:10.023 回答