我想交换文本TextFields

时间:2013-11-15 17:21:12

标签: java awt gettext settext

我尝试交换TextField的文本但只更改了一个TextField,其他一个保持不变:/,

我的代码是

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

        class mywindowclosing extends WindowAdapter {
                public void windowClosing(WindowEvent we) {
                        System.exit(0);
                }       
        }
        class Toggle extends Frame implements ActionListener {
                Label lb1, lb2;
                TextField txt1, txt2;
                Button bt1, bt2, bt3, bt4,bt5;
                public Toggle(){
                    super(".::Assignment::.");
                    setBounds(200,200,400,210);
                    setBackground(new Color(75,0,130));
                    setVisible(true);
                    setLayout(new FlowLayout(FlowLayout.LEFT));
                    //First Row
                    lb1=new Label("Label 1 ");
                    add(lb1);
                    txt1=new TextField(30);
                    add(txt1);
                    bt1=new Button("Button 1");
                    add(bt1);
                    bt1.addActionListener(this);
                    //2nd Row
                    lb2=new Label("Label 2 ");
                    add(lb2);
                    txt2=new TextField(30);
                    add(txt2);
                    bt2=new Button("Button 2");
                    add(bt2);
                    bt2.addActionListener(this);
                    //3rd Row
                    setLayout(new FlowLayout(FlowLayout.CENTER));
                    bt3=new Button("Clear");
                    add(bt3);
                    bt3.addActionListener(this);
                    bt4=new Button("Toggle");
                    add(bt4);
                    bt4.addActionListener(this);
                    bt5=new Button("Exit");
                    add(bt5);
                    bt5.addActionListener(this);
                    addWindowListener(new mywindowclosing());
                }

                public void actionPerformed(ActionEvent ae) {
                        if(ae.getSource()==bt1) {
                        txt1.setText("Button 1 pressed");
                        }
                        if(ae.getSource()==bt2) {
                        txt2.setText("Button 2 pressed");
                        }
                        if(ae.getSource()==bt3) {
                        txt1.setText("");
                        txt2.setText("");
                        }
                        if(ae.getSource()==bt4) {
                        txt1.setText(txt2.getText());
                        txt2.setText(txt1.getText());
                        }
                        if(ae.getSource()==bt5) {
                        System.exit(0);
                        }
                }       
        }       
        public class Toggler {
            public static void main(String args[]) {
                new Toggle();
            }
        }

2 个答案:

答案 0 :(得分:2)

txt1.setText(txt2.getText());
txt2.setText(txt1.getText());

问问自己 - 第一行后txt1的价值是多少?那么,什么写入txt2?

Hint

答案 1 :(得分:2)

以下代码:

  if(ae.getSource()==bt4) {
                txt1.setText(txt2.getText());
                txt2.setText(txt1.getText());
                }

执行此操作后,txt1(TextField)和txt2(TextField)中的文本将相同。

如果您想在它们之间交换文本,则需要使用临时变量。

试试这个:

    if (ae.getSource() == bt4) {

        String saveText1 = txt1.getText();
        txt1.setText(txt2.getText());
        txt2.setText(saveText1);
    }