日期:2014-05-20 浏览次数:20837 次
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.JPanel;
import javax.swing.JTextField;
public class ExampleFrame_09 extends JFrame {
private JTextField textField;
public static void main(String args[]) {
ExampleFrame_09 frame = new ExampleFrame_09();
frame.setVisible(true);
}
public ExampleFrame_09() {
super();
setTitle("文件选择对话框");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);
final JLabel label = new JLabel();
label.setText("文件:");
panel.add(label);
textField = new JTextField();
textField.setColumns(20);
panel.add(textField);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int i = fileChooser.showOpenDialog(ExampleFrame_09.this);
if (i == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
textField.setText(selectedFile.getName());
}
}
});
button.setText("上传");
panel.add(button);
//
}
}