java – JPanel里面的JFileChooser;如何让用户选择
作者:互联网
默认的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());
}
}
});
}
标签:jfilechooser,java,swing,jpanel 来源: https://codeday.me/bug/20190729/1576640.html