copy clear close

/*
 *编写一个程序,使之具有上图所示的界面,按Clear按钮清空两个文本框的内容:按Copy
 *按钮 时将Source文本框的内容复制到Target文本框;按Close按钮则结束程序的运行。
 *提示:关闭窗体可用JFrame类的setDefaultCloseOperation(EXIT_ON_CLOSE)方法*/

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class GUI extends JFrame{
	JButton button[]=new JButton[3];
	Container container;
	GUI(){
		setTitle("this is a gui");
		
		JLabel l1=new JLabel("Source");
		JLabel l2=new JLabel("Target");
		JPanel p1=new JPanel();
		p1.setLayout(new BorderLayout(1,1));//水平间距,垂直间距
		p1.add(l1, BorderLayout.NORTH);
		p1.add(l2,BorderLayout.SOUTH);
		
		
		final JTextArea t1=new JTextArea(3,20);  //指定列数和行数
		final JTextArea t2=new JTextArea(3,20);  //if not final: Cannot refer to the non-final local variable t2 defined in an enclosing scope
		JPanel p2=new JPanel();
		p2.setLayout(new BorderLayout(5,5)); 
		p2.add(t1,BorderLayout.NORTH);
		p2.add(t2,BorderLayout.SOUTH);
		
		button[0]=new JButton("Clear");
		button[1]=new JButton("Copy");
		button[2]=new JButton("Close");
		JPanel p3=new JPanel();
		p3.setLayout(new FlowLayout(FlowLayout.CENTER,3,3)); //对齐方式,水平间距,垂直间距
		p3.add(button[0]);
		p3.add(button[1]);
		p3.add(button[2]);
		
		
		container=this.getContentPane();//初始化一个容器
		container.add(p1,BorderLayout.WEST);
		container.add(p2,BorderLayout.EAST);
		container.add(p3,BorderLayout.SOUTH);
		
		setVisible(true);
		setSize(400,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		for(int i=0;i<3;i++){
			button[i].addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					if(e.getSource().equals(button[0])){
						t1.setText("");
						t2.setText("");
					}else if(e.getSource().equals(button[1])){
						t2.setText(t1.getText());
					}else if(e.getSource().equals(button[2])){
						dispose();   //用在按钮事件中,关闭窗口
					}
				}
			});
		}
	}
	
	public static void main(String[] args){
		GUI gui=new GUI();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值