import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
classMyFrame extensionsJFrame{//Frame Class
private JButton b1;
private JPanel panel;
private JLabel label;
private JTextField t1;
private JTextField resultTextField;
public MyFrame(){
setSize (600,500); // Resize the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle ("♥Guess my initial♥");
panel = new JPanel();
panel.setLayout(null);
label = new JLabel (" 내 Enter my initials.");
t1 = new JTextField(5); // Text field to hold distance in miles
b1 = new JButton ("input");
b1.setBackground(Color.ORANGE); //Change the color of the button to orange
resultTextField = new JTextField (30); //The result of converting miles to kilometers is a text field for sugar
b1.addActionListener(newMyListener()); //Create an event-generated object on the button
label.setBounds(10, 10, 300, 70);
t1.setBounds(320, 25, 100, 30);
b1.setBounds(430, 22, 70, 35);
resultTextField.setBounds (55, 109, 200, 30); //Place functions in the correct location
panel.add(label,BorderLayout.LINE_START);
panel.add(t1,BorderLayout.LINE_START);
panel.add(b1,BorderLayout.CENTER);
panel.add(resultTextField,BorderLayout.CENTER); //Add functionality to panel
add(panel);
setVisible(true);
}
private class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if( e.getSource() == b1){
resultTextField.setText("");
String name = t1.getText(); //put the value of the text field into the string name
final String myName = "KJW";
System.out.println(name);
System.out.println(myName);
if(name != myName )
resultTextField.setText("Isn't it KJW?"ㅜㅜ");
else
resultTextField.setText("Correct!");
}
}
}
}
public class ActionEventListener{
public static void main(String[] args) {
// // TODO Auto-generated method stub
MyFrame f = new MyFrame();
}
}
This code is used when you enter your initial (KJW) and press the button, and the message that it was correct is displayed in the text field It's a program that pops up! It clearly says that I typed KJW, but I didn't type KJW, but why on earth is that?
if(name != myName ) resultTextField.setText("Isn't it KJW?"ㅜㅜ"); else resultTextField.setText("Correct!");
Is this part wrong? Please give me an answer me. I'm dying of curiosity.Hahaha Please, Javanese masters!
java
In Java objects, ==
operation or !The =
operation means exactly the same instance (instance with the same memory address).
Therefore, try using equals to verify that the string (object) has the same value.
if( myName.equals(name) ) {
....
}
© 2024 OneMinuteCode. All rights reserved.