用JAVA做一个小程序,要求有A文本框和B文本框,还有一个按钮.

用JAVA做一个小程序,要求有A文本框和B文本框,还有一个按钮,在A文本框输入任意内容,然后点击按钮,A文本框中的内容就会复制到B文本框中!!!

//已调试
import javax.swing.*;
import java.awt.*;
public class CopyFrame extends JFrame{

private JTextField tf1 = new JTextField(10);
private JTextField tf2 = new JTextField(10);;
public CopyFrame() {
JButton btnCopy = new JButton("copy to other text");
btnCopy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
tf2.setText(tf1.getText());
}
});
setBounds(100,
100,
200,
300);
setLayout(new FlowLayout(FlowLayout.CENTER));
add(tf1);
add(btnCopy);
add(tf2);
setDefaultCloseOperation(EXIT_ON_CLOSE );
setVisible(true);

}
public static void main(String[] args) {
new CopyFrame();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-03-07
把上面一个文本框的内容复制到下面一个文本框
import javax.swing.*;
import java.awt.event.*;
class Test
{
public static void main(String[] args)
{
JFrame frame=new JFrame();
final JTextField field1=new JTextField(20);
final JTextField field2=new JTextField(20);
JButton button=new JButton("copy");
JPanel panel=new JPanel();
panel.add(field2);
panel.add(button);
frame.add(field1,"North");
frame.add(panel,"South");

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
field2.setText(field1.getText());

}

});
}
}
第2个回答  2012-03-07
上面正确。
第3个回答  2012-03-07
能先问问你的要求么?比如说要求拿什么代码去实现?swing行么?
相似回答