加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JPanel里面的JFileChooser;如何让用户选择

发布时间:2020-12-15 08:43:52 所属栏目:Java 来源:网络整理
导读:默认的JFileChooser可以工作,但是我不喜欢的是弹出它的事实.我宁愿有一个GUI,其中所有的动作发生. 现在,我确实设法做到了.下面的代码将FileChooser菜单很好地放在GUI中,而不是在它上面弹出. 我正在努力的是如何获得所选文件.我知道JFileChooser没有嵌入Panel
默认的JFileChooser可以工作,但是我不喜欢的是弹出它的事实.我宁愿有一个GUI,其中所有的动作发生.

现在,我确实设法做到了.下面的代码将FileChooser菜单很好地放在GUI中,而不是在它上面弹出.

我正在努力的是如何获得所选文件.我知道JFileChooser没有嵌入Panel中时有效的代码,但是我无法使用它.

任何人?

PS.我确实尝试过查找,但是尽管Oracle确实提到了将它放在容器中的可能性,但它并没有提供一个例子. http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

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

class SplitPane extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JSplitPane splitPaneV;
    private JSplitPane splitPaneH;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;

    public SplitPane() {
        setTitle("Split Pane Application");
        setBackground(Color.gray);

        JPanel topPanel = new JPanel();

        topPanel.setLayout(new BorderLayout());
        topPanel.setPreferredSize(new Dimension(700,500));
        getContentPane().add(topPanel);

        // Create the panels
        createPanel1();
        createPanel2();
        createPanel3();

        // Create a splitter pane
        splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        topPanel.add(splitPaneV,BorderLayout.CENTER);

        splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPaneH.setLeftComponent(panel1);
        splitPaneH.setRightComponent(panel2);

        splitPaneV.setLeftComponent(splitPaneH);
        splitPaneV.setRightComponent(panel3);
    }

    public void createPanel1() {
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());

        // Add some buttons
        panel1.add(new JButton("North"),BorderLayout.NORTH);
        panel1.add(new JButton("South"),BorderLayout.SOUTH);
        panel1.add(new JButton("East"),BorderLayout.EAST);
        panel1.add(new JButton("West"),BorderLayout.WEST);
        panel1.add(new JButton("Center"),BorderLayout.CENTER);

    }

    public void createPanel2() {
        panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());

        panel2.add(new JButton("Button 1"));
        panel2.add(new JButton("Button 2"));
        panel2.add(new JButton("Button 3"));
    }

    public void createPanel3() {
        panel3 = new JPanel();
        panel3.setLayout(new BorderLayout());
        panel3.setPreferredSize(new Dimension(400,100));
        panel3.setMinimumSize(new Dimension(100,50));
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser
                .setDialogTitle("Browse naar de  locatie waar je de gesorteerde bestanden wil zetten en klik op "OPEN"");
        panel3.add(fileChooser,BorderLayout.NORTH);
    }

    // this is where my quest starts. Now,I would like to work with the file
    // chosen...
    // for my ordinary 'popup' fileChoosers the code below works,so I tried the
    // code below

    // int returnVal = fileChooser.showOpenDialog(panel3);
    // if (returnVal == JFileChooser.APPROVE_OPTION)
    // fileName = fileChooser.getSelectedFile().getPath();
    // System.out.println(fileName);

    // but in this case it messes everything up...,after uncommenting I lose
    // the frames,and get a popup again...
    // anybody a suggestion how to actually get the users chosen file?

    public static void main(String args[]) {
        // Create an instance of the test application
        SplitPane mainFrame = new SplitPane();
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
}

解决方法

请注意,您可以将ActionListener添加到将响应按钮按下的JFileChooser,并且ActionEvent的getActionCommand将告诉您按下了哪个按钮.例如.,

public void createPanel3() {
      panel3 = new JPanel();
      panel3.setLayout(new BorderLayout());
      panel3.setPreferredSize(new Dimension(400,100));
      panel3.setMinimumSize(new Dimension(100,50));
      final JFileChooser fileChooser = new JFileChooser();
      fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      fileChooser
               .setDialogTitle("Browse naar de  locatie waar je de gesorteerde bestanden wil zetten en klik op "OPEN"");
      panel3.add(fileChooser,BorderLayout.NORTH);
      fileChooser.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
               System.out.println("File selected: " + fileChooser.getSelectedFile());
            }
         }
      });
   }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读