当用户点击按钮时复制文本框中的内容到标签中!怎么实现?importjava.awt.*;importjava.awt.event.*;publicclassButtonCopyimplementsActionListener,WindowListener{publicstaticvoi...
当用户点击按钮时 复制文本框中的内容到标签中!怎么实现?
import java.awt.*;
import java.awt.event.*;
public class ButtonCopy implements ActionListener,WindowListener{
public static void main(String args[]){
Frame f=new Frame("window");
Panel p=new Panel();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
Button b=new Button("COPY");
ButtonCopy bc = new ButtonCopy();
TextField text = new TextField("中国你好!");
text.setColumns(20);//设置输入框的大小,这个不能限制输入的字符数的。
p.setLayout(gbl);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=1;
gbl.setConstraints(text,gbc);
p.add(text);
gbl.setConstraints(b,gbc);
p.add(new Label());
p.add(b);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridwidth=1;
gbc.gridheight=2;
f.addWindowListener(new ButtonCopy());
b.addActionListener(new ButtonCopy());
f.add(p,"Center");
f.pack();
f.show();
}
public void actionPerformed(ActionEvent e){
Toolkit.getDefaultToolkit().beep();
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {System.exit(0);}
}
展开