In this article AWT Swing Programs we give the programs to AWT all components and Swing all components with properties.

AWT Swing Programs

// Addition of Two numbers

import java.awt.*;
import java.awt.event.*;

public class AddTwoNumbersAWT extends Frame implements ActionListener {
// Declare components
TextField num1, num2, result;
Label label1, label2, label3;
Button addButton;

// Constructor to set up the GUI
public AddTwoNumbersAWT() {
// Create components
label1 = new Label(“First Number:”);
label2 = new Label(“Second Number:”);
label3 = new Label(“Result:”);

num1 = new TextField(10);
num2 = new TextField(10);
result = new TextField(10);

addButton = new Button(“Add”);

// Add components to the frame
add(label1);
add(num1);

add(label2);
add(num2);

add(label3);
add(result);

add(addButton);

// Set layout and add action listener to the button
setLayout(new FlowLayout());
addButton.addActionListener(this);

// Configure the frame
setTitle(“Add Two Numbers”);
setSize(300, 150);
setVisible(true);
}

// Action listener method to handle button click event

public void actionPerformed(ActionEvent e) {
// Get numbers from text fields
int number1 = Integer.parseInt(num1.getText());
int number2 = Integer.parseInt(num2.getText());

// Perform addition
int sum = number1 + number2;

// Display result in result text field
result.setText(String.valueOf(sum));
}

public static void main(String[] args) {
new AddTwoNumbersAWT();
}
}

// AWT all Components

import java.awt.*;

import java.awt.event.*;

public class AWTComponents

{

public static void main(String []args)

{

// Create frame

final Frame f=new Frame(“AWT all components”);

// Create a lable

Label l1 = new Label(“Wel-Come!”);

l1.setBounds(20,100,70,30);

f.add(l1);   // add lable to frame

// Create a TextField (Single-line text box)

TextField textField = new TextField();

textField.setBounds(100, 100, 250, 30);

f.add(textField);

// Creating a Checkbox

final Checkbox cb1 = new Checkbox(“Java”);

cb1.setBounds(100, 150,  100, 50);

final Checkbox cb2 = new Checkbox(“Python”);

cb2.setBounds(100, 200,  100, 50);

f.add(cb1);

f.add(cb2);

// Create CheckboxGroup

CheckboxGroup group = new CheckboxGroup();

// Create Radio Buttons (Checkboxes) and join to CheckboxGroup

Checkbox radio1 = new Checkbox(“Male”, group, false);

radio1.setBounds(100, 250,  100, 50);

f.add(radio1);

Checkbox radio2 = new Checkbox(“Female”, group, false);

radio2.setBounds(100, 300,  100, 50);

f.add(radio2);

// Create a TextArea (Multi-line text box)

TextArea textArea = new TextArea();

textArea.setBounds(100, 350, 200, 100);

f.add(textArea);

// Buttons Attributes

Button b = new Button(“Click mi”);

b.setBounds(400, 350, 100, 50);

b.setSize(200, 50); // Setting the button size to 200×50

f.add(b);

f.setSize(800,500);

f.setLayout(null);

f.setVisible(true);

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

f.dispose();

}

});

}

}

// Swing all Components

import javax.swing.*;

import java.awt.*;

public class SwingAllComponentsExample {

public static void main(String[] args) {

// Create a new frame

JFrame frame = new JFrame(“Swing Components Example”);

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a panel to hold components

JPanel panel = new JPanel();

panel.setLayout(new FlowLayout());

// Add a label

JLabel label = new JLabel(“Enter your name:”);

panel.add(label);

// Add a text field

JTextField textField = new JTextField(20);

panel.add(textField);

// Add a button

JButton button = new JButton(“Submit”);

panel.add(button);

// Add a text area

JTextArea textArea = new JTextArea(5, 20);

textArea.setText(“This is a JTextArea”);

panel.add(textArea);

// Add a checkbox

JCheckBox checkBox = new JCheckBox(“I accept the terms and conditions”);

panel.add(checkBox);

// Add radio buttons

JRadioButton radioButton1 = new JRadioButton(“Male”);

JRadioButton radioButton2 = new JRadioButton(“Female”);

ButtonGroup genderGroup = new ButtonGroup();

genderGroup.add(radioButton1);

genderGroup.add(radioButton2);

panel.add(radioButton1);

panel.add(radioButton2);

// Add the panel to the frame

frame.add(panel);

// Make the frame visible

frame.setVisible(true);

}

}

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *