当单击按钮时,可以把一个文本框中的内容复制到另一个文本框中。要求用Swing组件实现。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Test {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextArea textArea1 = new JTextArea(20, 30);
final JTextArea textArea2 = new JTextArea(20, 30);
JButton button = new JButton("=>");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textArea2.setText(textArea1.getText());
}
});
frame.add(textArea1);
frame.add(button);
frame.add(textArea2);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Test {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextArea textArea1 = new JTextArea(20, 30);
final JTextArea textArea2 = new JTextArea(20, 30);
JButton button = new JButton("=>");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textArea2.setText(textArea1.getText());
}
});
frame.add(textArea1);
frame.add(button);
frame.add(textArea2);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
温馨提示:答案为网友推荐,仅供参考