编写一个图形界面的Application程序,包括两个文本框和一个按钮。

来源:百度知道 编辑:UC知道 时间:2024/07/04 06:37:22
当单击按钮时,可以把一个文本框中的内容复制到另一个文本框中。要求用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);
}
}