使用JFileChooser而不使用主要声明 [英] Use JFileChooser without using a main declaration
问题描述
继我以前的帖子在这个链接我还有一个问题。
给定以下代码:
public class GuiHandler扩展了javax。 swing.JFrame {
// GuiHandler是运行整个项目的类
//这里是我在代码中使用的两个私有字段,我有更多但他们
//不相关的时刻
final JFileChooser openFiles = new JFileChooser();
private DataParser xmlParser = new DataParser();
//稍后我有这个方法 - XMLfilesBrowserActionPerformed
private void XMLfilesBrowserActionPerformed(java.awt.event.ActionEvent evt){
//设置文件选择器
openFiles.setFileSelectionMode(JFileChooser.FILES_ONLY);
openFiles.setAcceptAllFileFilterUsed(false);
if(openFiles.showOpenDialog(this)== JFileChooser.APPROVE_OPTION){
//获取所选文件
File selected = openFiles.getSelectedFile();
//从这里解析我用JFileChooser
打开的XML文件,在我的一个方法的帮助下getNodeListFromFile
xmlParser.getNodeListFromFile(selected);
问题是我不能使用 我需要的是使用 如果有人可以解释我怎样才能使用上面的代码(或其他任何东西)与我的代码。 最好的祝福 编辑: 如果我使用代码喜欢这个 ; here 然后打开文件后,代码崩溃了。 如果我把 谢谢 包含的例子。 您应该在 Following my previous post in this link I have another problem . Given the following code : The problem is that I cannot use the declaration of And the use of What I need is to use the "simple" browser of I'd appreciate if someone can explain how can I use the above code (or anything else) with my code . Best regards EDIT: If I use the code like this ; here then after opening the file , the code crashes . If I make thanks I am sure that code was simply a self contained example. You should work the
这篇关于使用JFileChooser而不使用主要声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! main
,这是在上面的链接建议之前(我必须添加,这是一个相当不错的:)),所发布的代码是:
pre > public class NativeFileChooser {
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e){
e ();
jfc.showOpenDialog(null);
}
});
$ / code $ / pre
使用 main
使得稍后操作 XML
文件非常困难,我的
方法 getNodeListFromFile
$ b JFileChooser
的简单浏览器,然后使用而不涉及到 main
。
public void launchFileChooser(){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e){
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setAcceptAllFileFilterUsed(false);
if(jfc.showOpenDialog(null)== JFileChooser.APPROVE_OPTION)
newFile = jfc.getSelectedFile();
});
$ b newFile
是一个数据成员。
jfc
数据成员,代码打开常规浏览器。任何想法如何解决它?
GuiHandler
中以某种方式处理 JFileChooser
。如果有的话,你可以将这个代码添加到你的 GuiHandler
的初始化方法中。
public class GuiHandler extends javax.swing.JFrame {
//很多其他代码
public void launchFileChooser(){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e ){
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
}
} );
public static void main(String [] args){
GuiHandler handler = new GuiHandler();
handler.launchFileChooser();
}
}
public class GuiHandler extends javax.swing.JFrame {
// GuiHandler is the class that runs the entire project
// here are two private fields that I use in the code , I have more but they
// irrelevant for the moment
final JFileChooser openFiles = new JFileChooser();
private DataParser xmlParser = new DataParser();
// later on I have this method - XMLfilesBrowserActionPerformed
private void XMLfilesBrowserActionPerformed(java.awt.event.ActionEvent evt) {
//setting the file chooser
openFiles.setFileSelectionMode(JFileChooser.FILES_ONLY);
openFiles.setAcceptAllFileFilterUsed(false);
if (openFiles.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//getting the selected file
File selected = openFiles.getSelectedFile();
// from here I parse the XML file that I opened with JFileChooser
// with the help of one of my methods getNodeListFromFile
xmlParser.getNodeListFromFile(selected);
main
that was suggested before in the link above (and I must add that was a pretty nice one:)), the code that was posted is : public class NativeFileChooser {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
}
});
}
}
main
makes it very difficult for manipulating the XML
file later on ,with my
method getNodeListFromFile
.JFileChooser
, and then use the chosen file , without involving the main
into that . public void launchFileChooser() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setAcceptAllFileFilterUsed(false);
if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
newFile = jfc.getSelectedFile();
}
});
}
newFile
is a data member . jfc
a data member , the code opens the regular browser . Any idea how to fix it ? JFileChooser
into your GuiHandler
somehow. If anything, you could add that code to an init method of your GuiHandler
public class GuiHandler extends javax.swing.JFrame {
//lots of your other code
public void launchFileChooser() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
}
});
}
public static void main(String[] args) {
GuiHandler handler = new GuiHandler();
handler.launchFileChooser();
}
}