0% found this document useful (0 votes)
42 views53 pages

Cce103 Second Topic

ArrayList is a class in Java that implements a resizable array. It allows dynamic arrays that can grow or shrink as elements are added or removed. ArrayList uses an array as the underlying data structure. LinkedList is a sequence of links that contain items, with each link containing a connection to the next link. It allows adding and removing elements from the beginning or end of the list using methods like addFirst(), addLast(), removeFirst(), removeLast(), getFirst(), getLast().

Uploaded by

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

Cce103 Second Topic

ArrayList is a class in Java that implements a resizable array. It allows dynamic arrays that can grow or shrink as elements are added or removed. ArrayList uses an array as the underlying data structure. LinkedList is a sequence of links that contain items, with each link containing a connection to the next link. It allows adding and removing elements from the beginning or end of the list using methods like addFirst(), addLast(), removeFirst(), removeLast(), getFirst(), getLast().

Uploaded by

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

ArrayList is a class in Java that the user able to resize the array list.

Constructor is a special method used in Java to initialize objects and this can be called when an object is created within a class.
Vector is a class that implements adding and removing of items in an array of objects.

ArrayList

•The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as
needed.
•Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must
know in advance how many elements an array will hold.
•Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are
removed, the array may be shrunk.
ArrayList Constructors

 The ArrayList class supports three constructors. The first constructor builds an
empty array list:

ArrayList( )

 The following constructor builds an array list that is initialized with the elements of
the collection c.

ArrayList(Collection c)

 The following constructor builds an array list that has the specified initial capacity.
The capacity is the size of the underlying array that is used to store the elements.
 The capacity grows automatically as elements are added to an array list.

ArrayList(int capacity)
ArrayList Methods:

add(int index, Object element) – used to insert specified element at the specified position index in the list

set(int index, Object element) – used to replace the element at the specified index position

get(int index) – used to display the element of the specified index position from the list.

size() – used to display the number of elements from the array list.

remove(int index) – used to remove the element from the array list.
package sam;
import java.util.ArrayList;
public class Sam {
public static void main(String[] args) {
ArrayList phones = new ArrayList();

phones.add("Samsung"); Output:
phones.add("Vivo"); Samsung
phones.add("Oppo"); Vivo
phones.add("iPhone"); Oppo
phones.add("Huawei"); [Samsung, Vivo, Oppo, iPhone, Huawei]
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
System.out.println(phones);
}
}
package sam;
import java.util.ArrayList;
public class Sam {

public static void main(String[] args) {


ArrayList phones = new ArrayList();

phones.add("Samsung");
phones.add("Vivo");
phones.add("Oppo"); Output:
phones.add("iPhone"); [Nokia, Vivo, Oppo, iPhone, Huawei]
phones.add("Huawei"); Nokia
phones.set(0,"Nokia"); Vivo
System.out.println(phones); Oppo
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
}
}
package sam;
import java.util.ArrayList;
public class Sam {

public static void main(String[] args) {


ArrayList phones = new ArrayList();

phones.add("Samsung");
Output:
phones.add("Vivo");
phones.add("Oppo");
[Nokia, Vivo, Oppo, iPhone, Huawei]
phones.add("iPhone");
Nokia
phones.add("Huawei");
Vivo
phones.set(0,"Nokia");
Oppo
System.out.println(phones);
5
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
System.out.println(phones.size());
}
}
package sam;
import java.util.ArrayList;
public class Sam {
public static void main(String[] args) {
ArrayList phones = new ArrayList();
phones.add("Samsung");
phones.add("Vivo"); Output:
phones.add("Oppo"); [Nokia, Oppo, iPhone, Huawei]
phones.add("iPhone"); Nokia
phones.add("Huawei"); Oppo
phones.set(0,"Nokia"); iPhone
phones.remove(1); 4
System.out.println(phones);
System.out.println(phones.get(0));
System.out.println(phones.get(1));
System.out.println(phones.get(2));
System.out.println(phones.size());

}
}
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
// create an empty array list with an initial capacity
ArrayList<Integer> arrlist = new ArrayList<Integer>();

// use add() method to add elements in the list Output:


arrlist.add(15); Number = 15
arrlist.add(22); Number = 22
arrlist.add(30); Number = 25
arrlist.add(40); Number = 30
Number = 40
// adding element 25 at third position
arrlist.add(2,25);

// let us print all the elements available in list


for (Integer number : arrlist) {
System.out.println("Number = " + number);
} }}
ArrayList vs LinkedList

•The ArrayList a collection which can contain many objects of the same type and implement a List interface to add items,
change items, remove items and clear the list of items.

• A linked list is a sequence of data structures, which are connected together via links.

Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second
most-used data structure after array. Following are the important terms to understand the concept of Linked List.

Link − Each link of a linked list can store a data called an element.

Next − Each link of a linked list contains a link to the next link called Next.

LinkedList − A Linked List contains the connection link to the first link called First.
package sam;
import java.util.*;
public class Sam {

public static void main(String[] args) { Syntax:


addFirst() – adding an item to the beginning of the list.
LinkedList phones = new LinkedList();
phones.add("Samsung");
phones.add("Vivo");
phones.add("Oppo"); Output:
phones.addFirst("Nokia"); [Nokia, Samsung, Vivo, Oppo]
System.out.println(phones);
}
}
package sam;
import java.util.*;
public class Sam {

public static void main(String[] args) {

LinkedList phones = new LinkedList(); Syntax:


phones.add("Samsung"); addLast() – adding an item at the end of the list.
phones.add("Vivo");
phones.add("Oppo");
phones.addFirst("Nokia");
Output:
phones.addLast("Nokia");
[Nokia, Samsung, Vivo, Oppo, Nokia]
System.out.println(phones);
}
}
package sam;
import java.util.*;
public class Sam {

public static void main(String[] args) {


Syntax:
LinkedList phones = new LinkedList(); removeFirst() – removing an item at the beginning of the list.
phones.add("Samsung");
phones.add("Vivo");
phones.add("Oppo");
phones.addFirst("Nokia"); Output:
phones.removeFirst(); [Samsung, Vivo, Oppo]
System.out.println(phones);
}
}
package sam;
import java.util.*;
public class Sam {
Sysntax:
public static void main(String[] args) { removeLast() – removing an item at the end of the list.

LinkedList phones = new LinkedList();


phones.add("Samsung");
phones.add("Vivo"); Otput:
phones.add("Oppo"); [Nokia, Samsung, Vivo]
phones.addFirst("Nokia");
phones.removeLast();
System.out.println(phones);
}
}
package sam;
import java.util.*;
public class Sam { Syntax:
getFirst() – accessing an item at the beginning of the list.
public static void main(String[] args) {

LinkedList phones = new LinkedList();


phones.add("Samsung");
phones.add("Vivo"); Output:
phones.add("Oppo"); Samsung
phones.getFirst();
System.out.println(phones);

}
}
package sam;
import java.util.*;
public class Sam {
Syntax:
public static void main(String[] args) {
getLast() – accessing an item at the end of the list.
LinkedList phones = new LinkedList();
phones.add("Samsung");
phones.add("Vivo");
phones.add("Oppo"); Output:
phones.getLast(); Oppo
System.out.println(phones);

}
}
Swing in Java is a toolkit that composed of Graphical User Interface components.
Java Swing Library is a built-in library on top of the old Java Abstract Widget Toolkit (AWT).
Graphical User Interface in Java is a user-friendly visual experience builder for Java applications.

In this Java Swing journey, your will learn the following:


1.Java Layout Manager
2. Java Border Layout
3. Java Flow Layout
4.Swing GUI Composition
Java Layout Manager
•is used to organize the GUI components within a container.
•The common layout managers are BorderLayout, and FlowLayout.

BorderLayout - refers to the places of the components, which can either be on top, bottom, left, right, and center.
FlowLayout – default layout manager of every JPanel, which lays out components in a single row commonly one after the
another.
Swing GUI composition
1.Containers
2. Components

Container
•is a class that is designed to hold the group of components.
•A container is also known as component
Swing in Java is a toolkit that composed of Graphical User Interface components.
Java Swing Library is a built-in library on top of the old Java Abstract Widget Toolkit (AWT).
Graphical User Interface in Java is a user-friendly visual experience builder for Java applications.
Java Layout Manager
•is used to organize the GUI components within a container.
•The common layout managers are BorderLayout, and FlowLayout.
BorderLayout - refers to the places of the components, which can either be on top, bottom, left, right, and center.
FlowLayout – default layout manager of every JPanel, which lays out components in a single row commonly one after
the another.
Swing GUI composition
1. Containers J
The identifies all Swing components. Swing used to be
2. Components marketed as Java Foundation Classes, before it became an
integral part of the JDK. On Dec. 5 2010
Container
•is a class that is designed to hold the group of components.
•A container is also known as component
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0); Sample Border Layout
}
package swinglayoutdemo; });
controlPanel = new JPanel();
import java.awt.*; controlPanel.setLayout(new FlowLayout());
import java.awt.event.*; mainFrame.add(headerLabel);
import javax.swing.*; mainFrame.add(controlPanel);
public class SwingLayoutDemo { mainFrame.add(statusLabel);
private JFrame mainFrame; mainFrame.setVisible(true);
private JLabel headerLabel; }
private void showBorderLayoutDemo(){
private JLabel statusLabel; headerLabel.setText("Layout in action: BorderLayout");
private JPanel controlPanel; JPanel panel = new JPanel();
private JLabel msglabel; panel.setBackground(Color.darkGray);
public SwingLayoutDemo(){ panel.setSize(300,300);
prepareGUI(); } BorderLayout layout = new BorderLayout();
public static void main(String[] args){ layout.setHgap(10);
layout.setVgap(10);
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo(); panel.setLayout(layout);
swingLayoutDemo.showBorderLayoutDemo(); panel.add(new JButton("Center"),BorderLayout.CENTER);
} panel.add(new JButton("Line Start"),BorderLayout.LINE_START);
private void prepareGUI(){ panel.add(new JButton("Line End"),BorderLayout.LINE_END);
mainFrame = new JFrame("Java SWING Examples"); panel.add(new JButton("East"),BorderLayout.EAST);
mainFrame.setSize(400,400); panel.add(new JButton("West"),BorderLayout.WEST);
panel.add(new JButton("North"),BorderLayout.NORTH);
mainFrame.setLayout(new GridLayout(3, 1)); panel.add(new JButton("South"),BorderLayout.SOUTH);

headerLabel = new JLabel("",JLabel.CENTER ); controlPanel.add(panel);


statusLabel = new JLabel("",JLabel.CENTER); mainFrame.setVisible(true);
statusLabel.setSize(350,100); }
}
Java FlowLayout
Java FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the
default layout of the applet or panel.
import java.awt.*;
import javax.swing.*; // adding the buttons to frame
public class FlowLayoutExample { frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);
JFrame frameObj; frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frameObj.add(b8);
// constructor frameObj.add(b9); frameObj.add(b10);
FlowLayoutExample() {
// parameter less constructor is used
// creating a frame object // therefore, alignment is center
frameObj = new JFrame(); // horizontal as well as the vertical gap is 5 units.
// creating the buttons frameObj.setLayout(new FlowLayout());
JButton b1 = new JButton("1");
JButton b2 = new JButton("2"); frameObj.setSize(300, 300);
JButton b3 = new JButton("3"); frameObj.setVisible(true);
JButton b4 = new JButton("4"); }
JButton b5 = new JButton("5");
JButton b6 = new JButton("6"); // main method
JButton b7 = new JButton("7"); public static void main(String argvs[])
JButton b8 = new JButton("8"); {
JButton b9 = new JButton("9"); new FlowLayoutExample();
JButton b10 = new JButton("10"); }
}
k
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/ Output:
package swingsample2;

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

public class Swingsample2{


JFrame f;
Swingsample2(){
f=new JFrame();
// setting flow layout of right alignment
JButton b1=new JButton("1");
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton b2=new JButton("2");
JButton b3=new JButton("3");
f.setSize(300,300);
JButton b4=new JButton("4");
f.setVisible(true);
JButton b5=new JButton("5");
}
public static void main(String[] args) {
// adding buttons to the frame
new Swingsample2(); } }
f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);
// import statement
import java.awt.*; // adding the buttons to frame
import javax.swing.*; frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);
frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frameObj.add(b8);
public class FlowLayoutExample1 frameObj.add(b9); frameObj.add(b10);
{
JFrame frameObj; // parameterized constructor is used
// where alignment is left Output:
// constructor // horizontal gap is 20 units and vertical gap is 25 units.
FlowLayoutExample1() frameObj.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 25));
{
// creating a frame object
frameObj = new JFrame(); frameObj.setSize(300, 300);
frameObj.setVisible(true);
// creating the buttons }
JButton b1 = new JButton("1"); // main method
JButton b2 = new JButton("2"); public static void main(String argvs[])
JButton b3 = new JButton("3"); {
JButton b4 = new JButton("4"); new FlowLayoutExample1();
JButton b5 = new JButton("5"); }
JButton b6 = new JButton("6"); }
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("10");
package sting;
import javax.swing.JOptionPane; // program uses JOptionPane

public class Sting


{
public static void main( String args[] )
{
// obtain user input from JOptionPane input dialogs
String firstNumber = JOptionPane.showInputDialog( "Enter first integer" );
String secondNumber = JOptionPane.showInputDialog( "Enter second integer" );

// convert String inputs to int values for use in a calculation


int number1 = Integer.parseInt( firstNumber );
int number2 = Integer.parseInt( secondNumber );

int sum = number1 + number2; // add numbers

// display result in a JOptionPane message dialog


JOptionPane.showMessageDialog( null, "The sum is " + sum,
"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE );
} // end method main
} // end class Addition
package OptionPaneExample;
import javax.swing.*; Output:
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
JOptionPane.showMessageDialog(f,"Hello, Welcome to
Javatpoint.");
}
public static void main(String[] args) {
new OptionPaneExample();
}
}
package OptionPaneExample;
import javax.swing.*;
public class OptionPaneExample { Output:
JFrame f;
OptionPaneExample(){
f=new JFrame();
JOptionPane.showMessageDialog(f,"Successfully
Updated.","Alert",JOptionPane.WARNING_MESSAGE);
}
public static void main(String[] args) {
new OptionPaneExample();
}
}
k

package swingsample1;
import javax.swing.*; Output:
public class Swingsample1 {
JFrame f;
Swingsample1(){
f=new JFrame();
String name=JOptionPane.showInputDialog(f,"Enter Name");

public static void main(String[] args) {


new Swingsample1();
}
}
package OptionPaneExample;
import javax.swing.*; Output:
import java.awt.event.*;
public class OptionPaneExample extends WindowAdapter{
JFrame f;
OptionPaneExample(){
f=new JFrame();
f.addWindowListener(this);
f.setSize(300, 300);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.setVisible(true);
}
public void windowClosing(WindowEvent e) {
int a=JOptionPane.showConfirmDialog(f,"Are you sure?");
if(a==JOptionPane.YES_OPTION){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
new OptionPaneExample();
}
}
OptionPaneExample
import javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){ Output
f=new JFrame(); :
JOptionPane.showMessageDialog(f,"Hello, Welcome to
Javatpoint.");
}
public static void main(String[] args) {
new OptionPaneExample();
}
}
JButton Example
Java JButton
The JButton class is used to create a labeled button that has platform independent implementation. The application result in
some action when the button is pushed. It inherits AbstractButton class.
• public class JButton extends AbstractButton implements Accessible

Output:
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
JButton Example with ActionListener

import java.awt.event.*;
import javax.swing.*; Output:
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
JLabel Example
Java JLabel
The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The
text can be changed by an application but a user cannot edit it directly. It inherits JComponent class.
• public class JLabel extends JComponent implements SwingConstants, Accessible
import javax.swing.*;
class LabelExample
{ Output:
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Second Label.");
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true); } }
JLabel Example with ActionListener
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LabelExample extends Frame implements ActionListener{
JTextField tf; JLabel l; JButton b; Otput:
LabelExample(){
tf=new JTextField();
tf.setBounds(50,50, 150,20);
l=new JLabel();
l.setBounds(50,100, 250,20);
b=new JButton("Find IP");
b.setBounds(50,150,95,30);
b.addActionListener(this);
add(b);add(tf);add(l);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try{
String host=tf.getText();
String ip=java.net.InetAddress.getByName(host).getHostAddress();
l.setText("IP of "+host+" is: "+ip);
}catch(Exception ex){System.out.println(ex);}
}
public static void main(String[] args) {
new LabelExample();
}}
JTextField Example
JTextField
The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class.
• public class JTextField extends JTextComponent implements SwingConstants
import javax.swing.*;
class TextFieldExample
{ Output:
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
t2=new JTextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true); } }
JTextField Example with ActionListener
import javax.swing.*;
import java.awt.event.*;
public class TextFieldExample implements ActionListener{
JTextField tf1,tf2,tf3;
Output:
JButton b1,b2; }
TextFieldExample(){ public void actionPerformed(ActionEvent e) {
JFrame f= new JFrame(); String s1=tf1.getText();
tf1=new JTextField(); String s2=tf2.getText();
tf1.setBounds(50,50,150,20); /*50=column spacing, 70=row spacing int a=Integer.parseInt(s1);
100=Textfield lenght, 20=box thickness */ int b=Integer.parseInt(s2);
int c=0;
tf2=new JTextField(); if(e.getSource()==b1){
tf2.setBounds(50,100,150,20); c=a+b;
tf3=new JTextField(); }else if(e.getSource()==b2){
tf3.setBounds(50,150,150,20); c=a-b;
tf3.setEditable(false); }
b1=new JButton("+"); String result=String.valueOf(c);
b1.setBounds(50,200,50,50); tf3.setText(result);
b2=new JButton("-"); }
b2.setBounds(120,200,50,50); public static void main(String[] args) {
b1.addActionListener(this); new TextFieldExample();
b2.addActionListener(this); }}
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
JCheckBox Example
The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a CheckBox
changes its state from "on" to "off" or from "off" to "on ".It inherits JToggleButton class.
import javax.swing.*;
public class CheckBoxExample
{ Output:
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}}
JCheckBox Example with ItemListener
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxExample Output:
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);
}
JCheckBox checkbox1 = new JCheckBox("C++");
});
checkbox1.setBounds(150,100, 50,50);
f.setSize(400,400);
JCheckBox checkbox2 = new JCheckBox("Java");
f.setLayout(null);
checkbox2.setBounds(150,150, 50,50);
f.setVisible(true);
f.add(checkbox1); f.add(checkbox2); f.add(label);
}
checkbox1.addItemListener(new ItemListener() {
public static void main(String args[])
public void itemStateChanged(ItemEvent e) {
{
label.setText("C++ Checkbox: "
new CheckBoxExample();
+ (e.getStateChange()==1?"checked":"unchecked"));
}
}
JRadioButton Example
The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely
used in exam systems or quiz.
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
Output:
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new RadioButtonExample(); } }
JRadioButton Example with ActionListener
import javax.swing.*; Output:
import java.awt.event.*;
class RadioButtonExample extends JFrame implements
ActionListener{
JRadioButton rb1,rb2;
JButton b;
RadioButtonExample(){
rb1=new JRadioButton("Male");
rb1.setBounds(100,50,100,30);
rb2=new JRadioButton("Female");
rb2.setBounds(100,100,100,30);
ButtonGroup bg=new ButtonGroup(); public void actionPerformed(ActionEvent e){
bg.add(rb1);bg.add(rb2); if(rb1.isSelected()){
b=new JButton("click"); JOptionPane.showMessageDialog(this,"You are Male.");
b.setBounds(100,150,80,30); }
b.addActionListener(this); if(rb2.isSelected()){
add(rb1);add(rb2);add(b); JOptionPane.showMessageDialog(this,"You are Female.");
setSize(300,300); }
setLayout(null); }
setVisible(true); public static void main(String args[]){
} new RadioButtonExample();
}}
  Activity 4 Second_topic( given elements are 1,2,3,4,5)

1.       Create a program that will perform the ArraList() methods. These methods are add(), set(), get(), size(), remove() which to
be used in the program coding.

1.1    Use get() method to display the elements of the specified index position from the list started numbers  0, 1, 2, 3, and 4.

1.2   Use set() method to replace the element on index 2 by 5 at the specified index position.

1.3   use remove() method to remove the element from the array list, index numbers 2 and 3.

1.4   Use size() method to determine the remaining size of the elements.

1.5   Then display the output.

 2.        Create a program using JoptionPane to input two numbers, perform the four operations of addition, subtraction,
multiplication, and division, and display the results in one dialog message.

3.        Create a program using JoptionPane with a condition of if or case statement in the program. The user will ask to choose
which of the four operations is to be performed. And if the user will pick one of the choices he displays then the output will show on
the screen.

4. Create a program using Jframe to input numbers to jtextfield and click the choices  correspond the Jbutton that displays on the
screen the sign of four of the four operation.
Output:sample Jframe

package swingsample4; Sting sample

import javax.swing.JFrame;

public class Swingsample4 {

public static void main(String[] args) {


JFrame FRAME= new JFrame();
FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// true means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
}

}
package swingsample4;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Swingsample4 {

public static void main(String[] args) {


JFrame FRAME= new JFrame();
JLabel LABEL= new JLabel(" BASIC GUI LABEL");
LABEL.setBounds(50,100,100,100);//fixed setting

FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// true means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
FRAME.add(LABEL);
}

}
package swingsample4;
import javax.swing.JFrame; Sample display Jframe and Jlabel
import javax.swing.JLabel;

public class Swingsample4 {

public static void main(String[] args) {


JFrame FRAME= new JFrame(); BASIC GUI LABEL

JLabel LABEL= new JLabel(" BASIC GUI LABEL");


LABEL.setBounds(50,100,100,100);//fixed setting

FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// tre means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
FRAME.add(LABEL);
FRAME.setLayout(null);//use to unlock the fixed setting
}

}
package swingsample4;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Swingsample4 {

public static void main(String[] args) {


JFrame FRAME= new JFrame();
JLabel LABEL= new JLabel(" BASIC GUI LABEL");
LABEL.setBounds(50,100,100,100);//fixed setting
Click Here
JButton Button=new JButton("Click Here");

//button setting

FRAME.add(Button);//clickcable
Button.setBounds(250,100,100,100);
// frame settings
FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// tre means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
FRAME.add(LABEL);
FRAME.setLayout(null);//use to unlock the fixed setting
}

}
package swingsample4;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Swingsample4 {

public static void main(String[] args) {


JFrame FRAME= new JFrame();
JLabel LABEL= new JLabel(" BASIC GUI LABEL");
LABEL.setBounds(50,100,100,100);//fixed setting
JButton Button=new JButton("Click Here");

//button setting

Button.setBounds(250,100,100,100);
// frame settings
FRAME.setSize(600,600);//Dimensions
FRAME.setVisible(true);// tre means frame will be visible
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FRAME.setTitle("Sting_sample");
//label setting
FRAME.add(LABEL);
FRAME.add(Button);//transfer from the button setting
FRAME.setLayout(null);//use to unlock the fixed setting
}

}
Output:
package swing_demo.java;
//button setting
import java.awt.Color; Button.setBackground(Color.orange);
import javax.swing.JButton; Button.setBounds(250,100,100,100);
import javax.swing.JFrame; FRAME.add(Button);
import javax.swing.JLabel; // frame setting
import javax.swing.JPanel; FRAME.setSize(600,600);//Dimensions
public class Swing_demoJava {
FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String[] args) { FRAME.setTitle("Sting_sample");
JFrame FRAME= new JFrame();
JPanel basicpanel= new JPanel(); //label setting
JLabel LABEL= new JLabel(" BASIC GUI LABEL"); FRAME.add(basicpanel);
//FRAME SETTINGS FRAME.add(LABEL);
LABEL.setBounds(100,100,100,100); FRAME.add(Button);
basicpanel.setBounds(250,80,200,200); FRAME.setLayout(null);
basicpanel.setBackground(Color.gray); FRAME.setVisible(true);// tre means frame will be visible
JButton Button=new JButton("Click Here"); }

You might also like