0% found this document useful (0 votes)
9 views5 pages

Oopex 10

The document outlines the design and implementation of a Java Swing GUI application for login functionality, where a message box indicates success or failure based on the username and password. It includes code examples for creating a login interface and a radio button demo, demonstrating event-driven programming in Java. The programs utilize components like JFrame, JButton, JLabel, and ActionListener to create interactive user interfaces.

Uploaded by

isaacbenjamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Oopex 10

The document outlines the design and implementation of a Java Swing GUI application for login functionality, where a message box indicates success or failure based on the username and password. It includes code examples for creating a login interface and a radio button demo, demonstrating event-driven programming in Java. The programs utilize components like JFrame, JButton, JLabel, and ActionListener to create interactive user interfaces.

Uploaded by

isaacbenjamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex. No.

10 JAVA SWINGS

18/10/24 URK23AI1035

Aim :
To Design a java swing GUI application for the Login functionality as per the sample

design given below. Show a message box “Login Successful” if username and password is

“Karunya”, otherwise show a message box “Login Failed”.

Description :

In Java Swing, you can create Graphical User Interface (GUI) programs with components
like buttons, labels, text fields, and choice lists. Here’s how to create two GUI programs
based on your description:
1. Program with "Yes", "No", and "Close" Buttons:
This program creates a window with three buttons ("Yes", "No", "Close"). When the "Yes" or
"No" button is pressed, the message "Button Yes/No is pressed" is displayed in a label. When
the "Close" button is pressed, the program terminates by closing the window.
• JFrame: A top-level container for your GUI application.
• JButton: Represents a button in the GUI.
• JLabel: Displays text in the GUI.
• ActionListener: A listener interface to handle button press events
SYNTAX:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Breakdown:
• JFrame: Creates the main window.
• JButton: A button component.
• JLabel: A component to show text.
• JComboBox: A drop-down list for user selection.

82
• JTextField: A field to display or accept user input.
• ActionListener: Handles user actions like button presses or item selections.
Steps for Execution:
1. Compile the program using a Java compiler (javac).
2. Run the program (java ClassName) to see the GUI window appear.
These programs demonstrate fundamental event-driven programming in Java using the Swing
API.
JFrame:
• The top-level window that holds all the components.
• In this program, JFrame serves as the main window where all the labels will be
displayed.
JLabel:
• A label component that displays a short string of text.
• Labels are used to provide information or to describe the purpose of other
components, such as text fields or buttons.
• In this example, labels will be used to display a title, instructions, and feedback
messages.

1.Design a java swing GUI application for the Login functionality as per the sample

design given below. Show a message box “Login Successful” if username and

password is “Karunya”, otherwise show a message box “Login Failed”.

Program :

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginApp {


public static void main(String[] args) { JFrame frame = new JFrame("Login
Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setLayout(new GridLayout(3, 2));

83
JLabel userLabel = new JLabel("Username:");
JTextField userText = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordText = new JPasswordField();
JButton loginButton = new JButton("Login");

frame.add(userLabel);
frame.add(userText);
frame.add(passwordLabel);
frame.add(passwordText);
frame.add(loginButton);

loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = userText.getText();
String password = new String(passwordText.getPassword());

if (username.equals("Karunya") && password.equals("Karunya")) {


JOptionPane.showMessageDialog(frame, "Login Successful");
} else {
JOptionPane.showMessageDialog(frame, "Login Failed");
}
}
});

frame.setVisible(true);
}
}

OUTPUT:

84
3.Write a Java program to create three radio buttons. When any of them is selected, an

appropriate message is displayed.

PROGRAM

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RadioButtonDemo extends JFrame implements ItemListener {

JRadioButton javaButton, aspButton, sqlButton;


ButtonGroup group;

public RadioButtonDemo() {
super("JRadioButton Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
setLayout(new FlowLayout());

javaButton = new JRadioButton("Java");


aspButton = new JRadioButton("ASP.Net");
sqlButton = new JRadioButton("SQL");

group = new ButtonGroup();


group.add(javaButton);
group.add(aspButton);
group.add(sqlButton);

javaButton.addItemListener(this);
aspButton.addItemListener(this);
sqlButton.addItemListener(this);

add(javaButton);
add(aspButton);
add(sqlButton);

setVisible(true);
}

public void itemStateChanged(ItemEvent e) {

85
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == javaButton) {
JOptionPane.showMessageDialog(this, "The Java option has been
selected");
} else if (e.getSource() == aspButton) {
JOptionPane.showMessageDialog(this, "The ASP.Net option has been
selected");
} else if (e.getSource() == sqlButton) {
JOptionPane.showMessageDialog(this, "The SQL option has been
selected");
}
}
}

public static void main(String[] args) {


new RadioButtonDemo();
}
}

OUTPUT :

RESULT :

The program for java swings has been completed and output is verified successfully.

86

You might also like