[Go to site: main page, start]

0% found this document useful (0 votes)
3 views28 pages

Topic 13 Java GUI Examples With Program Code

The document contains multiple Java examples demonstrating the creation of GUI applications using Swing. Each example showcases different components such as frames, labels, buttons, text fields, and menus, illustrating how to build user interfaces for various functionalities like login forms, registration forms, and simple calculations. The code snippets are structured to be easily understandable for beginners learning Java GUI programming.

Uploaded by

sworupraj56
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)
3 views28 pages

Topic 13 Java GUI Examples With Program Code

The document contains multiple Java examples demonstrating the creation of GUI applications using Swing. Each example showcases different components such as frames, labels, buttons, text fields, and menus, illustrating how to build user interfaces for various functionalities like login forms, registration forms, and simple calculations. The code snippets are structured to be easily understandable for beginners learning Java GUI programming.

Uploaded by

sworupraj56
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

Figure:

Example 1:
package LBEF;

import [Link].*;

class Hello {

public static void main(String[] args) {

JFrame frame = new JFrame("LBEF");

[Link](500, 500);

[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);

[Link](null);
}

Output:
Example 2:
package LBEF;

import [Link].*;

class Hello {

public static void main(String[] args) {

JFrame frame = new JFrame("LBEF");

[Link](500, 500);

[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);

[Link](null);

// Labels
JLabel lblUser = new JLabel("User ID:");

[Link](50, 40, 100, 25);

[Link](lblUser);

//Button
JButton jb = new JButton("Ok");

[Link](50,70,100,25);
[Link](jb);

//

//ComboBox
JComboBox<String> cbCourse = new JComboBox<>();

[Link]("BCA");

[Link]("CSIT");
[Link]("BIM");

[Link]("BBS");

[Link]("BSc");

[Link](200,40,100,25);

[Link](cbCourse);

}
Output:
Example 3:
package GUI;

import [Link].*;

public class loginForm {

public static void main(String[] args) {

// Create Frame
JFrame frame = new JFrame("Login Form");

[Link](400, 300);
[Link](null); // IMPORTANT for setBounds

[Link](JFrame.EXIT_ON_CLOSE);

// Label: User ID
JLabel lblUser = new JLabel("User ID:");

[Link](50, 40, 100, 25);

[Link](lblUser);

// TextField: User ID
JTextField txtUser = new JTextField();

[Link](150, 40, 180, 25);

[Link](txtUser);

// Label: Password
JLabel lblPass = new JLabel("Password:");

[Link](50, 80, 100, 25);

[Link](lblPass);
// Password Field
JPasswordField txtPass = new JPasswordField();

[Link](150, 80, 180, 25);

[Link](txtPass);

// Submit Button
JButton btnSubmit = new JButton("Submit");

[Link](80, 140, 100, 30);


[Link](btnSubmit);

// Cancel Button
JButton btnCancel = new JButton("Cancel");

[Link](200, 140, 100, 30);

[Link](btnCancel);

// Show Frame
[Link](true);

Output:
Example 4:
package GUI;

import [Link].*;

import [Link].*;

class RegistrationForm extends JFrame {

// Instance variables

JLabel title, lblName, lblEmail, lblPass, lblGender, lblCourse, lblHobby;

JTextField txtName, txtEmail;

JPasswordField txtPass;

JRadioButton rbMale, rbFemale;

ButtonGroup bgGender;

JComboBox<String> cbCourse;

JCheckBox cbReading, cbSports, cbMusic;

JButton btnRegister, btnReset;

// Constructor
RegistrationForm() {

setTitle("Registration Form");

setSize(480, 480);
setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Title
title = new JLabel("Student Registration Form");

[Link](new Font("Arial", [Link], 18));

[Link](90, 20, 300, 30);

add(title);

// Name
lblName = new JLabel("Full Name:");

[Link](50, 80, 100, 25);


add(lblName);

txtName = new JTextField();

[Link](180, 80, 200, 25);

add(txtName);

// Email
lblEmail = new JLabel("Email:");

[Link](50, 120, 100, 25);

add(lblEmail);

txtEmail = new JTextField();

[Link](180, 120, 200, 25);

add(txtEmail);

// Password
lblPass = new JLabel("Password:");

[Link](50, 160, 100, 25);

add(lblPass);
txtPass = new JPasswordField();

[Link](180, 160, 200, 25);

add(txtPass);

// Gender
lblGender = new JLabel("Gender:");

[Link](50, 200, 100, 25);


add(lblGender);

rbMale = new JRadioButton("Male");

[Link](180, 200, 70, 25);

rbFemale = new JRadioButton("Female");

[Link](260, 200, 80, 25);

bgGender = new ButtonGroup();

[Link](rbMale);

[Link](rbFemale);

add(rbMale);

add(rbFemale);

// Course
lblCourse = new JLabel("Course:");

[Link](50, 240, 100, 25);

add(lblCourse);
String[] courses = {"BCA", "CSIT", "BIM", "BBS", "BSc"};
cbCourse = new JComboBox<>(courses);

[Link](180, 240, 200, 25);

add(cbCourse);

// Hobbies (CheckBox)
lblHobby = new JLabel("Hobbies:");

[Link](50, 280, 100, 25);

add(lblHobby);

cbReading = new JCheckBox("Reading");

[Link](180, 280, 90, 25);

add(cbReading);

cbSports = new JCheckBox("Sports");

[Link](280, 280, 80, 25);

add(cbSports);

cbMusic = new JCheckBox("Music");

[Link](180, 310, 80, 25);

add(cbMusic);

// Buttons
btnRegister = new JButton("Register");

[Link](140, 360, 100, 30);

add(btnRegister);
btnReset = new JButton("Reset");

[Link](260, 360, 100, 30);

add(btnReset);

setVisible(true);

// Main Method
public static void main(String[] args) {

new RegistrationForm();

Output:
Example 5:
package GUI;

import [Link].*;

import [Link];

import [Link].*;

class ExampleForm extends JFrame {

// Components

JList<String> listSubjects;

JTable tableStudents;

JSlider sliderAge;

JLabel lblList, lblTable, lblSlider;

// Constructor

ExampleForm() {

setTitle("Swing Components Example Form");


setSize(700, 500);
setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// ===== JList =====


lblList = new JLabel("Subjects:");

[Link](30, 30, 100, 25);

add(lblList);
String[] subjects = {"Math", "Science", "Computer", "English", "Economics"};
listSubjects = new JList<>(subjects);

[Link](30, 60, 150, 100);

add(listSubjects);

// ===== JTable =====


lblTable = new JLabel("Student Records:");

[Link](220, 30, 150, 25);

add(lblTable);

String[] columnNames = {"ID", "Name", "Course"};

Object[][] data = {

{"1", "Ram", "BCA"},

{"2", "Sita", "CSIT"},

{"3", "Hari", "BIM"}

};

DefaultTableModel model =
new DefaultTableModel(data, columnNames);

tableStudents = new JTable(model);

JScrollPane tableScroll = new JScrollPane(tableStudents);

[Link](220, 60, 420, 120);

add(tableScroll);
// ===== JSlider =====
lblSlider = new JLabel("Age:");

[Link](30, 200, 100, 25);

add(lblSlider);

sliderAge = new JSlider(18, 60, 25);

[Link](80, 200, 300, 50);

[Link](10);
[Link](2);
[Link](true);

[Link](true);

add(sliderAge);

setVisible(true);

// Main Method
public static void main(String[] args) {

new ExampleForm();

Output:
Example 6:
package GUI;

import [Link].*;

import [Link].*;

class MenuExample extends JFrame {

// Constructor
MenuExample() {

setTitle("JMenu and Submenu Example");

setSize(400, 300);
setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// ===== JMenuBar =====


JMenuBar menuBar = new JMenuBar();

// ===== Main Menu: File =====


JMenu fileMenu = new JMenu("File");

// Menu Items
JMenuItem newItem = new JMenuItem("New");

JMenuItem openItem = new JMenuItem("Open");


JMenuItem exitItem = new JMenuItem("Exit");

// Add action for Exit

[Link](e -> [Link](0));


// Add items to File menu

[Link](newItem);

[Link](openItem);

// ===== Submenu: Import =====


JMenu importMenu = new JMenu("Import");

JMenuItem importPDF = new JMenuItem("Import PDF");


JMenuItem importCSV = new JMenuItem("Import CSV");

// Add submenu items

[Link](importPDF);

[Link](importCSV);

// Add submenu to File menu

[Link](importMenu);

// Add Exit item

[Link](); // separator line


[Link](exitItem);

// ===== Main Menu: Help =====


JMenu helpMenu = new JMenu("Help");
JMenuItem aboutItem = new JMenuItem("About");

[Link](aboutItem);

// ===== Add Menus to MenuBar =====


[Link](fileMenu);

[Link](helpMenu);

// Set MenuBar to JFrame

setJMenuBar(menuBar);

setVisible(true);

// Main method
public static void main(String[] args) {

new MenuExample();

Output:
Example 7: Find Sum of Two Number:

package GUI;

import [Link].*;

import [Link].*;

class SumTwoNumbersGUI extends JFrame implements ActionListener {

// Components
JLabel lblNum1, lblNum2, lblResult;

JTextField txtNum1, txtNum2;

JButton btnSum;

// Constructor

SumTwoNumbersGUI() {

// Frame properties

setTitle("Sum of Two Numbers");

setSize(350, 250);
setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Label for first number


lblNum1 = new JLabel("First Number:");

[Link](30, 30, 100, 25);


add(lblNum1);

// TextField for first number


txtNum1 = new JTextField();
[Link](150, 30, 150, 25);

add(txtNum1);

// Label for second number


lblNum2 = new JLabel("Second Number:");

[Link](30, 70, 100, 25);

add(lblNum2);

// TextField for second number


txtNum2 = new JTextField();

[Link](150, 70, 150, 25);

add(txtNum2);

// Button to calculate sum


btnSum = new JButton("Calculate Sum");

[Link](90, 110, 150, 30);


[Link](this); // Add action listener

add(btnSum);

// Label to show result


lblResult = new JLabel("Sum = ");

[Link](30, 160, 200, 25);

add(lblResult);

setVisible(true);

}
// Action performed when button clicked
public void actionPerformed(ActionEvent e) {

// Get numbers from textboxes


int num1 = [Link]([Link]());

int num2 = [Link]([Link]());

// Calculate sum
int sum = num1 + num2;

// Display sum

[Link]("Sum = " + sum);

// Main method
public static void main(String[] args) {

new SumTwoNumbersGUI(); // Call constructor

}
Output:
Example 8: Sum of Two Number using Option Pane Dialogue

package GUI;

import [Link].*;

import [Link].*;

class SumTwoNumbersGUIOptionPane extends JFrame implements ActionListener {

// Components
JLabel lblNum1, lblNum2;

JTextField txtNum1, txtNum2;

JButton btnSum;

// Constructor

SumTwoNumbersGUIOptionPane() {

// Frame properties

setTitle("Sum of Two Numbers");

setSize(350, 200);
setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Label for first number


lblNum1 = new JLabel("First Number:");

[Link](30, 30, 100, 25);


add(lblNum1);

// TextField for first number


txtNum1 = new JTextField();
[Link](150, 30, 150, 25);

add(txtNum1);

// Label for second number


lblNum2 = new JLabel("Second Number:");

[Link](30, 70, 120, 25);

add(lblNum2);

// TextField for second number


txtNum2 = new JTextField();

[Link](150, 70, 150, 25);

add(txtNum2);

// Button to calculate sum


btnSum = new JButton("Calculate Sum");

[Link](90, 110, 150, 30);


[Link](this); // Add action listener

add(btnSum);

setVisible(true);

// Action performed when button clicked


public void actionPerformed(ActionEvent e) {

try {

// Get numbers from textboxes


int num1 = [Link]([Link]());
int num2 = [Link]([Link]());

// Calculate sum
int sum = num1 + num2;

// Display sum using JOptionPane


[Link](this, "The sum is: " + sum,

"Sum Result", JOptionPane.INFORMATION_MESSAGE);


} catch (NumberFormatException ex) {

[Link](this, "Please enter valid integers",

"Input Error", JOptionPane.ERROR_MESSAGE);

// Main method
public static void main(String[] args) {

new SumTwoNumbersGUIOptionPane();

Output:

You might also like