Java Swing之按钮点击选择文件与获取选中文件绝对路径

2 篇文章 0 订阅
button.addActionListener(new ActionListener() {      
public void actionPerformed(ActionEvent e) {
                                                      //按钮点击事件


JFileChooser chooser = new JFileChooser();             //设置选择器
 chooser.setMultiSelectionEnabled(true);             //设为多选
int returnVal = chooser.showOpenDialog(button);        //是否打开文件选择框
System.out.println("returnVal="+returnVal);

if (returnVal == JFileChooser.APPROVE_OPTION) {          //如果符合文件类型

String filepath = chooser.getSelectedFile().getAbsolutePath();      //获取绝对路径
System.out.println(filepath);


System.out.println("You chose to open this file: "+ chooser.getSelectedFile().getName());  //输出相对路径

}
}
});

  • 9
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的Java Swing程序,其中包含两个文本框、一个文件选中按钮、一个密码框和一个提交按钮点击文件选择按钮后,将获取选中文件绝对路径,并将其填充到第二个文本框中。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; public class FileChooserExample extends JFrame implements ActionListener { private JTextField textField1; private JTextField textField2; private JPasswordField passwordField; private JButton fileChooserButton; private JButton submitButton; public FileChooserExample() { super("File Chooser Example"); // Create components JLabel label1 = new JLabel("Text Field 1:"); textField1 = new JTextField(20); JLabel label2 = new JLabel("Text Field 2:"); textField2 = new JTextField(20); passwordField = new JPasswordField(20); fileChooserButton = new JButton("Choose File"); submitButton = new JButton("Submit"); // Set file chooser button action listener fileChooserButton.addActionListener(this); // Set submit button action listener submitButton.addActionListener(this); // Create layout JPanel panel = new JPanel(new GridLayout(4, 2)); panel.add(label1); panel.add(textField1); panel.add(label2); panel.add(textField2); panel.add(new JLabel("Password:")); panel.add(passwordField); panel.add(fileChooserButton); panel.add(submitButton); // Set window properties setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setContentPane(panel); pack(); setVisible(true); } // Handle button clicks public void actionPerformed(ActionEvent e) { if (e.getSource() == fileChooserButton) { // Show file chooser dialog JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { // Get selected file and set text field value File selectedFile = fileChooser.getSelectedFile(); textField2.setText(selectedFile.getAbsolutePath()); } } else if (e.getSource() == submitButton) { // Handle submit button click String value1 = textField1.getText(); String value2 = textField2.getText(); char[] password = passwordField.getPassword(); // Do something with the values and password System.out.println("Value 1: " + value1); System.out.println("Value 2: " + value2); System.out.println("Password: " + new String(password)); } } public static void main(String[] args) { new FileChooserExample(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值