2014-02-18 43 views
0

我正在尝试将JFileChooser添加到JPanel。有时它可以正常工作,有时只显示没有JFileChooser对话框的JPanel。现在我实际上不知道该怎么做。任何人都可以帮助我吗?java GUI中的JFileChooser问题

这里是我的代码(这是一种方法):

public File chooseFileFromComputer(){ 

    methodPanel = new JPanel(); 
    methodPanel.setLayout(null); 


    methodFrame = new JFrame(); 
    methodFrame.setTitle("File Chooser"); 
    methodFrame.setVisible(true); 

    BufferedImage removeJavaImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); 
    methodFrame.setIconImage(removeJavaImage); 

    methodLabel = new JLabel("Choose a file: "); 
    methodLabel.setBounds(10, 10, 80, 20); 
    methodPanel.add(methodLabel); 

    fileChooser = new JFileChooser(); 
    fileChooser.setMultiSelectionEnabled(false); 
    fileChooser.setBounds(10, 35, 550, 500); 
    fileChooser.setVisible(true); 
    add(fileChooser); 
     /** 
    * Action Events             #********AE*******# 
    **/ 

    fileChooser.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent actionEvent) { 
      String command = actionEvent.getActionCommand(); 
     if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
      selectedFile = fileChooser.getSelectedFile(); 
      methodFrame.setVisible(false);  
     } else if (command.equals(JFileChooser.CANCEL_SELECTION)) { 
      methodFrame.setVisible(false); 
     } 
     } 
    }); 

    //End of Action Events            #________AE_______# 


    methodPanel.add(fileChooser); 
    methodFrame.setContentPane(methodPanel); 
    methodFrame.setResizable(false); 
    methodFrame.setSize(600, 600); 
    methodFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); 

    return selectedFile; 
} 
+0

见编辑回答。 –

+0

请参阅第二编辑回答。我很害怕在这里挑选很多。 –

回答

3
  1. 您使用的是空的布局,并呼吁您的组件setBounds(...)。虽然这对新手来说可能是创建复杂GUI的更好方法,但这是一个谬误,您创建Swing GUI的次数越多,您越能学会尊重和使用布局管理器,并发现这些生物能够极大地帮助您创建灵活,美观且如果需要是复杂的GUI。
  2. 将您的组件添加到JFrame,然后在JFrame上调用setVisible(true)
  3. 如果您将它设置为可见和不可见,它看起来不像应该使用JFrame。也许你真的想要使用JDialog,甚至是独立的JFileChooser对话框。

编辑
你加入JFileChooser,以超过一个容器:

add(fileChooser); // ******************* here ************* 

    fileChooser.addActionListener(new ActionListener(){ 
     private File selectedFile; 

    public void actionPerformed(ActionEvent actionEvent) { 
      String command = actionEvent.getActionCommand(); 
     if (command.equals(JFileChooser.APPROVE_SELECTION)) { 
     selectedFile = fileChooser.getSelectedFile(); 
     methodFrame.setVisible(false);  
     } else if (command.equals(JFileChooser.CANCEL_SELECTION)) { 
      methodFrame.setVisible(false); 
     } 
    } 
    }); 

    methodPanel.add(fileChooser); // ******** here ******* 

你不能做到这一点。只添加到一个容器,否则它可能无法正确显示或显示。


编辑2

你回来从你的方法错误的结果。您返回selectedFile变量,但是在它被设置之前这样做,因为它是由ActionListener设置的,在此方法返回之后,它被称为long

解决方案:再次,不要在这里使用JFrame,其中模态JDialog会更好。如果您使用了模式对话框,并在之后返回,则ActionListener完成后,您的代码将会工作。


编辑3
但同样我的钱,我只是用一个JFileChooser一个模式对话框。例如:

JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.setDialogTitle("Choose a File"); 

    // don't use null in the method below but rather a reference to your current GUI 
    int response = fileChooser.showOpenDialog(null); 
    if (response == JFileChooser.APPROVE_OPTION) { 
    File file = fileChooser.getSelectedFile(); 
    System.out.println(file); 
    } 
0

你的JPanel - methodPanel都是空的布局methodPanel.setLayout(null);尝试用例如与FlowLayout

0

我并不确切地知道你的类wherether扩展JPanel或没有,但你应该添加文件选择到methodPanel和label一起使用。除了actionPerform,我测试了你的代码。

methodPanel.add(fielChooser); 

我建议,试试吧。