Lesson-11-Password-Graphical-User-Interface
Lesson-11-Password-Graphical-User-Interface
bt1, bt2,
bt3, bt4,
bt5, bt6,
bt7, bt8,
bt9, bt0
enter clear
Code:
private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {
output.setText(output.getText()+"1");
}
Explanation
1. output:
o This is the reference to a Swing component that displays text. It
could be a JLabel, JTextField, JTextArea, or another component
that has a setText method.
2. output.getText():
o The getText() method retrieves the current text displayed by the
output component. For example, if output is a JTextField and it
currently displays "Hello", then output.getText() returns "Hello".
3. + "1":
o This part concatenates the string "1" to the end of the current text.
In Java, the + operator is used to concatenate (join) strings. If the
current text is "Hello", then output.getText() + "1" results in
"Hello1".
4. output.setText(...):
o The setText method updates the text displayed by the output
component with the new string provided as an argument. The new
string in this case is the result of the concatenation from step 3.
Putting It All Together
1. output:
o This is the reference to a Swing component that can display text.
For example, it could be an instance of JLabel, JTextField, or
JTextArea.
2. setText Method:
o The setText method is used to set the text content of the
component. The argument passed to setText determines the new
text that will be displayed by the component.
3. "" (Empty String):
o The argument "" is an empty string, which means that when it is
set as the text of the component, it effectively clears any existing
text.
What It Does
The line output.setText(""); clears any text that is currently displayed in the
output component. After this line is executed, the output component will show
no text (i.e., it will be empty).
String PW = output.getText();
This line retrieves the current text from the output component and
o
stores it in a string variable PW.
o output is typically a Swing component like JTextField or JTextArea
that displays text and allows for text retrieval with the getText()
method.
2. Checking the Text Against a Specific Value:
if (PW.equals("164325")) {
output.setText("Congratulations");
▪ This sets the text of the output component to
"Congratulations".
o If the condition is false (i.e., PW is not "164325"), the else block is
executed:
output.setText("Error");