javaa notes
javaa notes
1. Title Bar - Displays the name of the current project or file being worked on.
2. Menu Bar - Contains options such as File, Edit, View, Run, and more, allowing access to various
functionalities.
3. Toolbars - Provide quick access to frequently used tools and commands.
4. GUI Builder - Enables users to design graphical user interfaces in two modes: Design View (visual
interface layout) and Source View (where code is edited).
5. Palette - Contains essential GUI components such as buttons, text fields, labels, and more.
6. Inspector Window - Displays a hierarchy of all components placed in the current form, allowing
users to manage them effectively.
7. Properties Window - Enables customization of component properties, such as text, color, font, and
alignment.
8. Code Editor Window - The main area where developers write and edit Java code.
NetBeans provides various GUI components that can be used to create interactive applications. Below is a
summary of the most common components, their definitions, and frequently used properties and methods.
Now, a new file (CalculatorFrame.java) is created in your project with a graphical design interface.
java
CopyEdit
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// Code to handle button click event
}
3. Write Java Code inside the method to define the button's behavior.
Object-Oriented Programming (OOP)
OOP is a programming paradigm that focuses on using objects and classes. The key concepts include:
1. setText(String text)
📌 Use: Sets or updates the text of GUI components like JTextField, JLabel, and JButton.
📌 Example:
jTextField1.setText("Hello, Java!");
2. getText()
📌 Use: Retrieves the current text from text-based components like JTextField and JTextArea.
📌 Example:
📌 Explanation: This stores the user input from jTextField1 into the variable name.
3. getPassword()
📌 Use: Retrieves the text entered in a JPasswordField, returning it as a char[] for security.
📌 Example:
📌 Explanation: This gets the password input securely, instead of using getText() which is less secure.
4. isSelected()
📌 Use: Checks if a JRadioButton or JCheckBox is selected.
📌 Example:
if (jRadioButton1.isSelected()) {
System.out.println("Option Selected");
}
5. setEnabled(boolean value)
jButton1.setEnabled(false);
📌 Explanation: This disables jButton1, preventing the user from clicking it.
📌 Explanation: This asks the user "Are you sure?" and stores their choice in choice (Yes = 0, No = 1,
Cancel = 2).
📌 Use: Displays an input dialog box for the user to enter data.
📌 Example:
📌 Explanation: This prompts the user to enter their name and stores it in name.
9. Integer.parseInt(String text)
📌 Explanation: This converts the text from jTextField1 into an integer value.
📌 Explanation: This converts text input from jTextField1 into a decimal number.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
📌 Explanation: This closes the application when the JFrame window is closed.
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid day");
}
📌 Explanation: This prints the corresponding day of the week based on the value of day.
System.exit(0);
📌 Explanation: This closes the application immediately.
jCheckBox1.setSelected(true);
16. getSelectedItem()
📌 Explanation: This gets the currently selected option from a drop-down list.
Function Use
Explanation:
Explanation:
System.exit(0);
Important step :-
What type of data the variable will store (double, int, String, etc.).
What name the variable will have (num1, num2, etc.).
Sometimes, an initial value (like = Double.parseDouble(...)).
float Sum = 4; Declaration of a float variable named Sum which has an initial value of 4.0.
As mentioned above, each variable needs to have a name so that it can be referenced anywhere during the
application. Each programming language has its own set of rules for naming variables. The rules and conventions for
naming variables in Java are summarized below:
● Keywords or words, which have special meaning in java, should not be used as the variable names.
● All variable names must begin with a letter, an underscore (_) or a dollar sign ($). The convention is to always use a
letter and avoid starting variable names with underscore (_) and dollar sign ($).
● After the first initial letter, variable names may contain letters and digits (0 to ● 9) and (_,$), but no spaces or
special characters are allowed.
Java variable names are case sensitive, so sum1 and SUM1 aren't the same variable.
Example
Explanation:
int age = 25; declares an integer variable named age and assigns it a value of 25.
String name = "John"; declares a string variable to store text values.
boolean isStudent = true; declares a boolean variable that can hold either true or false.
Without declaring variables, you cannot store values and perform calculations.
The default flow of execution, where statements execute one after another in the order they are
written. (Each statement runs one after the other)
Example:
An if statement is a conditional statement that executes a block of code only if a specified condition is
true.
Syntax:
Example:
Explanation:
If the condition is false, the code inside the if block does not execute.
expression.
it in curly braces ({}) but if there are multiple statements then they must be enclosed
• The else clause is optional and needs to be included only when some action is to
An if-else statement is a conditional statement that executes one block of code if the condition is true
and another block if the condition is false.
Syntax:
Example:
Explanation:
This selection statement allows us to test the value of an expression with a series of
character or integer values.
On finding a matching value the control jumps to the statement pertaining to that
value and the statement is executed, till the break statement is encountered or the
end of switch is reached.
The expression must either evaluate to an integer value or a character value.
It cannot be a string or a real number.
SYNTAX
PRACTICAL 13 (SWITCH STATEMENT – DISCOUNT CALCULATOR)
DATA TYPES
States the way the valies of that data type are stored, the operations that can be done on
that type, and the range for that type.