我试图交换文本的文本,但只有一个文本字段被更改为其他一个留下相同:/,

我的代码是

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();
            }
        }
.

有帮助吗?

解决方案

在下面的代码:

  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);
    }
.

其他提示

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

问问自己 - 做第一行后txt1的值是什么?并且,因此,写入TXT2的是什么?

提示

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top