Copying the Contents of a Text Field

Asked 2 months ago, Updated 2 months ago, 3 views

You have prepared two text fields and are creating a program to copy the contents of the text field when you press the copy button.Here are three things to do:

テキスト When characters are entered only in the text field 1, the contents of the text field 1 are copied into the text field 2 by pressing the copy button.Contents of the text field 1 are left displayed.
②If a copy button is pressed when characters are inputted only to a text field 2, the contents of the text field 2 are copied to the text field 1.Contents of the text field 2 are left displayed.
③Replace text field 1 and text field 2 when characters are entered in text field 1 and text field 2.

As for 、 and に, for some reason, the contents of text field 1 disappeared only when の, and the contents of text field 1 are displayed in text field 2.①Since 。 and わかりません are the same, I don't understand why が can't be done and が can't be done.The code is as follows:①Could you advise me on how to make it happen?

import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class COPY extensions Appletives ActionListener {          
    // Fields

    private TextField Text1 = new TextField(", 20); // button, text field
    private Button btn1 = new Button("COPY");                     
    private TextField Text2 = new TextField(", 20);    

    public COPY(){

        // Register components (common with Applet)                
        // Configure Layout Manager     
        setLayout (new FlowLayout (FlowLayout.CENTER);//    

        // Registering components  
        add(Text1);
        add(btn1); 
        add(Text2);

        // Register Event Listener                
        btn1.addActionListener(this);               
        Text1.addActionListener(this);
        Text2.addActionListener(this);
    }
    // actionPerformed method        
    public void actionPerformed (ActionEvent arg0) {      
        // When btn1 is pressed, select a copy pattern depending on the text field situation
        if(arg0.getSource()==btn1){           
            String str1 = Text1.getText();
            String str2 = Text2.getText();
            if("".equals(str2)){
                Text2.setText(str1);
                Text1.setText(str1);
            }
            if("".equals(str1)){
                Text1.setText(str2);
                Text2.setText(str2);
            }
            else{
                Text2.setText(str1);
                Text1.setText(str2);
            }
        }                   
    }
}

java

2022-09-30 11:32

1 Answers

To put it simply, I think the reason is that the else clause is missing before the two if statements.Therefore,
If you do the following, it will work as expected.

if("".equals(str2))){
    Text2.setText(str1);
    Text1.setText(str1);
}
else if("".equals(str1))) {
    Text1.setText(str2);
    Text2.setText(str2);
}
else{
    Text2.setText(str1);
    Text1.setText(str2);
}

Supplementary
In the conditional statement in the question sentence, regardless of the result of the first if statement (if("".equals(str2)){), the second if statement
(if("".equals(str1)){) will run.
(In the case of の, the conditional expression is false, so the else section is processed and the contents of Text1 and Text2 are replaced.)
If the conditional expression for the first if statement is true, you must add the else clause because you do not want to process the following if statement:(This will execute the second if statement only if the conditional expression for the first if statement is false)


2022-09-30 11:32

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.