编写一个程序,使之具有如右图所示的界面。按钮作用如下: Copy:将Source内容拷贝到Target; Clear: 清楚内容 Close:退出程序
import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class CopyDemo extends Frame implements ActionListener { Label lblSource, lblTarget; TextField txtSource, txtTarget; Button btnCopy, btnClear, btnClose; public CopyDemo() { //setLayout setLayout(new FlowLayout()); //create component lblSource = new Label("Source"); txtSource = new TextField(25); lblTarget = new Label("Target"); txtTarget = new TextField(25); btnCopy = new Button("Copy"); btnClear = new Button("Clear"); btnClose = new Button("Close"); //add component add(lblSource); add(txtSource); add(lblTarget); add(txtTarget); add(btnCopy); add(btnClear); add(btnClose); //addListener btnCopy.addActionListener(this); btnClear.addActionListener(this); btnClose.addActionListener(this); } @Override public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Copy")) { String str = txtSource.getText(); txtTarget.setText(str); } if(ae.getActionCommand().equals("Clear")) { txtSource.setText(" "); txtTarget.setText(" "); } if(ae.getActionCommand().equals("Close")) { System.exit(0); } } public static void main(String[] args) { CopyDemo cd = new CopyDemo(); cd.setSize(300, 200); cd.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); cd.setVisible(true); } }