编写一个简单的Java程序,在程序中建立一个窗口,有两个文本框和一个按钮,单击按钮,能把左边文本框的内容复制到右边的文本框中。
下面是我写的代码,注释的地方是不会的:
package com.Experiment_67;
import java.awt.*;
import java.awt.event.*;
public class Copy implements ActionListener 
{ Button b;
TextField L,R;
public void display()
{
Frame f=new Frame("复制");
f.setSize(300,150);
f.setBackground(Color.lightGray);
f.setLayout(new FlowLayout());
L=new TextField(10);
R=new TextField(10);
b=new Button("复制");
f.add(L);
f.add(R);
f.add(b);
b.addActionListener(Copy());//MyEclipse报错
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
//把左边文本框的内容复制到右边的文本框中
}

}
public static void main(String[] args)
{
(new Copy()).display();
}
}

解决方案 »

  1.   


    import java.awt.*;
    import java.awt.event.*;public class Copy implements ActionListener { Button b; TextField L, R; public void display() {
    Frame f = new Frame("复制");
    f.setSize(300, 150);
    f.setBackground(Color.lightGray);
    f.setLayout(new FlowLayout());
    L = new TextField(10);
    R = new TextField(10);
    b = new Button("复制");
    f.add(L);
    f.add(R);
    f.add(b);
    b.addActionListener(this);// MyEclipse报错
    f.setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b) {
    // 把左边文本框的内容复制到右边的文本框中
    R.setText(L.getText());
    } } public static void main(String[] args) {
    (new Copy()).display();
    }
    }
      

  2.   


    public class Copy extends Frame { Button b;
    TextField L, R; public void display() {
    Frame f = new Frame("复制");
    f.setSize(300, 150);
    f.setBackground(Color.lightGray);
    f.setLayout(new FlowLayout());
    L = new TextField(10);
    R = new TextField(10);
    b = new Button("复制");
    f.add(L);
    f.add(R);
    f.add(b);
    b.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    R.setText(L.getText());
    }
    });
    f.setVisible(true);
    } public static void main(String[] args) {
    (new Copy()).display();
    }
    }