JFileChooser 启动问题

发布于 2024-11-05 19:02:43 字数 2191 浏览 0 评论 0原文

我在开发机器上启动任何使用“JFileChooser”Swing 对象的应用程序时遇到问题。当我最初开发该应用程序并对其进行测试时,文件选择器窗口打开,一切都很好,但一周后,我尝试再次运行该应用程序,但什么也不会显示。

起初我以为这是一个线程问题(我原来的应用程序使用了一点多线程),所以我复制并粘贴了以下代码(我从互联网上获得了代码)来测试它:

package com.kwm.util.test;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Main {

public static void main(String[] args) {
    final JFrame frame = new JFrame("JFileChooser Demo");


    final JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);
    fc.setCurrentDirectory(new File("C:\\tmp"));

    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            fc.showDialog(frame, "Choose");
        }
    });
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int retVal = fc.showOpenDialog(frame);
            if (retVal == JFileChooser.APPROVE_OPTION) {
                File[] selectedfiles = fc.getSelectedFiles();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < selectedfiles.length; i++) {
                    sb.append(selectedfiles[i].getName() + "\n");
                }
                JOptionPane.showMessageDialog(frame, sb.toString());
            }

        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            fc.showSaveDialog(frame);

        }
    });

    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);
}
}

但我仍然什么也没看到。我猜测机器有问题,但无法确定问题是什么。

任何对此的帮助将不胜感激。

编辑1 操作系统版本是 Windows Server 2003 Enterprise Version, SP1

Java 版本是:1.5.0_12(JDK 和 JRE)

我也担心这可能与网络问题有关... JFileChooser 正在寻找网络目录和DNS 可能有冲突。有办法检查吗?也许检查一下 JVM 正在记录什么?

I am having problems starting any application on my development machine that uses the 'JFileChooser' Swing object. When I originally developed the application and tested it , the File Chooser window opened and everything was fine but like after a week , I tried running the app again and nothing would show at all.

At first I thought it was a Threading problem( My original app used a little multi-threading), so I coped and pasted the following code ( I got the code from the Internet ) to test it out:

package com.kwm.util.test;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Main {

public static void main(String[] args) {
    final JFrame frame = new JFrame("JFileChooser Demo");


    final JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);
    fc.setCurrentDirectory(new File("C:\\tmp"));

    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            fc.showDialog(frame, "Choose");
        }
    });
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int retVal = fc.showOpenDialog(frame);
            if (retVal == JFileChooser.APPROVE_OPTION) {
                File[] selectedfiles = fc.getSelectedFiles();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < selectedfiles.length; i++) {
                    sb.append(selectedfiles[i].getName() + "\n");
                }
                JOptionPane.showMessageDialog(frame, sb.toString());
            }

        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            fc.showSaveDialog(frame);

        }
    });

    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);
}
}

And still I see nothing. I am guessing the Machine has a problem but am unable to identify what the problem is.

Any help with this would be greatly appreciated.

EDIT 1
The OS Version is Windows Server 2003 Enterprise Version, SP1

The Java version is : 1.5.0_12 ( both JDK and JRE)

I am also afraid that this may be related to a Network problem... JFileChooser is looking for a network directory and the DNS may be conflicting. Is there a way to check this? Maybe check what JVM is logging?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紧拥背影 2024-11-12 19:02:43

@Andrew Thompson 是对的:从 事件调度线程 开始可能会这不是问题所在,但众所周知,相关的错误千变万化且难以重现。作为参考,我在下面展示了一个常见的重构。它在 Mac OS X 10.5 上测试成功,使用

$ java -version
java version "1.5.0_28"
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                createGUI();
            }
        });
    }

    private static void createGUI() throws HeadlessException {
        final JFrame frame = new JFrame("JFileChooser Demo");
        final JFileChooser fc = new JFileChooser();
        fc.setMultiSelectionEnabled(true);
        fc.setCurrentDirectory(new File("C:\\tmp"));
        JButton btn1 = new JButton("Show Dialog");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fc.showDialog(frame, "Choose");
            }
        });
        JButton btn2 = new JButton("Show Open Dialog");
        btn2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int retVal = fc.showOpenDialog(frame);
                if (retVal == JFileChooser.APPROVE_OPTION) {
                    File[] selectedfiles = fc.getSelectedFiles();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < selectedfiles.length; i++) {
                        sb.append(selectedfiles[i].getName());
                        sb.append("\n");
                    }
                    JOptionPane.showMessageDialog(frame, sb.toString());
                }
            }
        });
        JButton btn3 = new JButton("Show Save Dialog");
        btn3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fc.showSaveDialog(frame);
            }
        });
        Container pane = frame.getContentPane();
        pane.setLayout(new GridLayout(3, 1, 10, 10));
        pane.add(btn1);
        pane.add(btn2);
        pane.add(btn3);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

@Andrew Thompson is right: starting on the event dispatch thread may not be the problem, but related bugs are notoriously protean and difficult to reproduce. For reference, I've a shown a common re-factoring below. It tested successfully on Mac OS X 10.5, using

$ java -version
java version "1.5.0_28"
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                createGUI();
            }
        });
    }

    private static void createGUI() throws HeadlessException {
        final JFrame frame = new JFrame("JFileChooser Demo");
        final JFileChooser fc = new JFileChooser();
        fc.setMultiSelectionEnabled(true);
        fc.setCurrentDirectory(new File("C:\\tmp"));
        JButton btn1 = new JButton("Show Dialog");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fc.showDialog(frame, "Choose");
            }
        });
        JButton btn2 = new JButton("Show Open Dialog");
        btn2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int retVal = fc.showOpenDialog(frame);
                if (retVal == JFileChooser.APPROVE_OPTION) {
                    File[] selectedfiles = fc.getSelectedFiles();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < selectedfiles.length; i++) {
                        sb.append(selectedfiles[i].getName());
                        sb.append("\n");
                    }
                    JOptionPane.showMessageDialog(frame, sb.toString());
                }
            }
        });
        JButton btn3 = new JButton("Show Save Dialog");
        btn3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fc.showSaveDialog(frame);
            }
        });
        Container pane = frame.getContentPane();
        pane.setLayout(new GridLayout(3, 1, 10, 10));
        pane.add(btn1);
        pane.add(btn2);
        pane.add(btn3);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
我很OK 2024-11-12 19:02:43

Had a similar problem with JFileChooser when using threads, uploaded a post here. I got a lot of help by reading this wiki http://en.wikipedia.org/wiki/Event_dispatching_thread (although its already been mentiond).
My problem was that I had a scanner that was running in the thread, always waiting for input before showing GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文