日期:2014-05-20 浏览次数:20738 次
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.UIManager; public class JFileChooserTest extends JFrame{ public JFileChooserTest() { super(); // 使用Windows的界面风格 try { // 是windows if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } } catch (Exception e) { System.out.println("设置界面感官异常!"); e.printStackTrace(); } setTitle("JFileChooserTest"); setBounds(100,100,350,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton(); final JLabel label = new JLabel(); button.addActionListener(new ActionListener(){ //监听事件 public void actionPerformed(ActionEvent e){ JFileChooser fileChooser = new JFileChooser(); //对话框 int i = fileChooser.showOpenDialog(getContentPane()); //opendialog if(i==JFileChooser.APPROVE_OPTION) //判断是否为打开的按钮 { File selectedFile = fileChooser.getSelectedFile(); //取得选中的文件 label.setText(selectedFile.getPath()); //取得路径 } } }); getContentPane().add(button,BorderLayout.NORTH); //布局处理 getContentPane().add(label,BorderLayout.CENTER); button.setText("上传"); } public static void main(String[] args) { JFileChooserTest jFileChooserTest = new JFileChooserTest(); jFileChooserTest.setVisible(true); } }