编写程序,包括一个标签、一个文本框和一个按钮,当用户单击按钮时,程序把文本框中的内容复制到标签中。

实现,从键盘输入点击’Copy按钮’输出。

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

public class lyy extends Frame implements ActionListener {

    private Button copy = new Button("Copy");
    private TextField text = new TextField(20);
    private Label label = new Label("");
    public lyy() {
        super("Example");
        setLayout(new GridLayout(3,0));
        add(text);
        add(label);
        add(copy);
        label.setBackground(Color.BLUE);
        copy.addActionListener(this);
        text.addActionListener(this);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub
        if(e.getSource() == text) {
            text.getText().trim();
        }else if(e.getSource() == copy){
        //text.setText(str);
        label.setText(text.getText());
        }
    }

    public static void main(String[] args){
        lyy click = new lyy();

    }
}

测试截图
测试截图

猜你喜欢

转载自blog.csdn.net/inuyasha_1314/article/details/80237663