我正在使用 java swing 将代码从一个位置复制到另一个位置。我在这里做了Browse
。但我不知道如何为复制按钮添加功能。请帮我写代码。提前致谢。这是我的完整代码。(对不起,如果我错了,我是第一次使用这个)
/*
For Browse button.
*/
package com.design;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Browse extends JFrame implements
ActionListener {
JButton button1, button2 ;
JTextField field1, field2;
public Browse () {
this.setLayout(null);
button1 = new JButton("Browse");
field1 = new JTextField();
field1.setBounds(30, 50, 200, 25);
button1.setBounds(240, 50, 100, 25);
this.add(field1);
this.add(button1);
button2 = new JButton("Copy");
button2.setBounds(150, 150, 100, 25);
this.add(button2);
button1.addActionListener(this);
setDefaultCloseOperation
(javax.swing.WindowConstants.EXIT_ON_CLOSE
);
}
public void actionPerformed(ActionEvent e) {
Chooser frame = new Chooser();
field1.setText(frame.fileName);
}
public static void main(String args[]) {
Browse frame = new Browse ();
frame.setSize(400, 300);
frame.setLocation(200, 100);
frame.setVisible(true);
}
}
class Chooser extends JFrame {
JFileChooser chooser;
String fileName;
public Chooser() {
chooser = new JFileChooser();
int r = chooser.showOpenDialog(new JFrame());
if (r == JFileChooser.APPROVE_OPTION) {
fileName = chooser.getSelectedFile().getPath();
System.out.println(fileName);
}
}
}