簡體   English   中英

我想交換文本TextFields

[英]I want to swap text TextFields

我嘗試交換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();
            }
        }
txt1.setText(txt2.getText());
txt2.setText(txt1.getText());

問問自己-完成第一行后txt1的值是什么? 因此,什么寫入txt2?

暗示

對於下面的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM