JFileChooser 启动问题
我在开发机器上启动任何使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Andrew Thompson 是对的:从 事件调度线程 开始可能会这不是问题所在,但众所周知,相关的错误千变万化且难以重现。作为参考,我在下面展示了一个常见的重构。它在 Mac OS X 10.5 上测试成功,使用
@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
使用线程时 JFileChooser 也遇到类似问题,在此处上传了一篇文章。我通过阅读此 wiki http://en.wikipedia.org/wiki/Event_dispatching_thread< 获得了很多帮助/a> (尽管已经提到过)。
我的问题是我有一个在线程中运行的扫描仪,在显示 GUI 之前总是等待输入。
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.