0% found this document useful (0 votes)
7 views

Lesson-11-Password-Graphical-User-Interface

Yes

Uploaded by

araceli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson-11-Password-Graphical-User-Interface

Yes

Uploaded by

araceli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

output

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

Components of the Code

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

When you combine these parts, the line output.setText(output.getText() + "1");


performs the following actions:

1. Retrieves the current text from the output component.


2. Appends the string "1" to this text.
3. Sets the updated text back to the output component.

private void bt2ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"2");
}

private void bt3ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"3");
}

private void bt4ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"4");
}

private void bt5ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"5");
}

private void bt6ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"6");
}

private void bt7ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"7");
}

private void bt8ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"8");
}

private void bt9ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"9");
}

private void bt0ActionPerformed(java.awt.event.ActionEvent evt) {


output.setText(output.getText()+"0");
}

private void clearActionPerformed(java.awt.event.ActionEvent evt) {


output.setText("");
}
Explanation

Components of the Code

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

private void enterActionPerformed(java.awt.event.ActionEvent evt) {


String PW = output.getText();
if(PW.equals("164325")){
output.setText("Congratulations");
}
else{
output.setText("Error");
}
}
Explanation

1. Getting the Text from the Component:

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")) {

This line checks if the string stored in PW is equal to the string


o
"164325".
o The equals method is used to compare the contents of two strings
for equality.
3. Conditional Actions:
o If the condition in the if statement is true (i.e., PW is "164325"), the
following block is executed:

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

▪ This sets the text of the output component to "Error".

You might also like