第三次作业

编写一个程序,使之具有如右图所示的界面。按钮作用如下: 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); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值