0% found this document useful (0 votes)
6 views8 pages

Ut 4

The document contains multiple Java AWT and Swing examples demonstrating different layout managers and components such as BorderLayout, GridLayout, MenuBar, ImageIcon, JRadioButton, JTabbedPane, JScrollPane, JTree, JTable, and JProgressBar. Each example is encapsulated in a class that extends JFrame or Frame and showcases the creation and addition of various UI elements. The code snippets illustrate how to set up the GUI components and their basic functionalities.

Uploaded by

aditishirole2112
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)
6 views8 pages

Ut 4

The document contains multiple Java AWT and Swing examples demonstrating different layout managers and components such as BorderLayout, GridLayout, MenuBar, ImageIcon, JRadioButton, JTabbedPane, JScrollPane, JTree, JTable, and JProgressBar. Each example is encapsulated in a class that extends JFrame or Frame and showcases the creation and addition of various UI elements. The code snippets illustrate how to set up the GUI components and their basic functionalities.

Uploaded by

aditishirole2112
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/ 8

Border Layout Example

package AWT;
import java.awt.*;
class demo extends Frame{
public demo()
{
setTitle("Border Layout Example ");
setSize(300,300);
setLayout(new BorderLayout());
Button b1 = new Button("East");
Button b2 = new Button("West");
Button b3 = new Button("North");
Button b4 = new Button("South");
Button b5 = new Button("Center");
add(b1,BorderLayout.EAST);
add(b2,BorderLayout.WEST);
add(b3,BorderLayout.NORTH);
add(b4,BorderLayout.SOUTH);
add(b5,BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new demo();
}
}

Grid Layout Example

package AWT;
import java.awt.*;
class demo extends Frame{
public demo()
{
setTitle("Grid Layout");
setLayout(new GridLayout(2,2));//2rows , 2 colns
Button l1 = new Button("1");
Button l2 = new Button("2");
Button l3 = new Button("3");
Button l4 = new Button("4");
add(l1);
add(l2);
add(l3);
add(l4);
setSize(300,400);
setVisible(true);
}
public static void main(String[] args) {
new demo();
}
}

MenuItem, Menu ,MenuBar

package AWT;
import java.awt.*;
class demo extends Frame{
public demo()
{
setTitle("MenuBar");
MenuBar mb =new MenuBar();
setMenuBar(mb);

Menu f = new Menu("File");


mb.add(f);

MenuItem s = new MenuItem("Save");


MenuItem o = new MenuItem("Open");
MenuItem c = new MenuItem("Close");
f.add(s);
f.add(o);
f.add(c);

setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new demo();
}
}
Image Icon

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

class basic extends JFrame


{
basic()
{
setLayout(new FlowLayout());
setSize(300,200);

ImageIcon i = new
ImageIcon("C:\\Users\\Prime\\OneDrive\\Pictures\\Screenshots\\Screenshot
(1).png");
JLabel j = new JLabel("Text with icon ",i,JLabel.CENTER);
add(j);

setVisible(true);
}
public static void main(String[] args) {
new basic();
}
}

JRadioButton

import javax.swing.*;
import java.awt.*;
class demo extends JFrame
{
demo()
{
setLayout(new FlowLayout());
setSize(300,300);
JRadioButton r1 = new JRadioButton("Male");
JRadioButton r2 = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
add(r1);
add(r2);
setVisible(true);
}
public static void main(String[] args)
{
new demo();
}
}

JTabbedPane

package AWT;
import java.awt.*;
import javax.swing.*;
public class Adv_Swing extends JFrame{
public Adv_Swing()
{
setLayout(new FlowLayout());
setSize(400,400);

JTabbedPane tab = new JTabbedPane();

JPanel p1 = new JPanel();


JLabel l1 = new JLabel("Tab 1");
p1.add(l1);

tab.add("Tab 1",p1);

JPanel p2 = new JPanel();


JLabel l2 = new JLabel("Tab 2");
p2.add(l2);

tab.add("Tab 2",p2);

add(tab);
setVisible(true);
}
public static void main(String[] args) {
new Adv_Swing();
}
}

ScrollPane

package AWT;
import java.awt.*;
import javax.swing.*;
class Adv_Swing extends JFrame
{
public Adv_Swing()
{
setLayout(new FlowLayout());
setSize(300,400);
JTextArea t = new JTextArea(2,10);
JScrollPane sc = new JScrollPane(t);
//add(t);
add(sc);
setVisible(true);
}
public static void main(String[] args) {
new Adv_Swing();
}
}

JTRee
package AWT;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
class Adv_Swing extends JFrame
{
public Adv_Swing()
{
setLayout(new FlowLayout());
setSize(300,400);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("Child");
DefaultMutableTreeNode cc1 = new DefaultMutableTreeNode("Childs
Child");

root.add(c1);
c1.add(cc1);
JTree t1 = new JTree(root);
add(t1);
setVisible(true);
}
public static void main(String[] args) {
new Adv_Swing();
}
}

Jtable

package AWT;
import java.awt.*;
import javax.swing.*;
class Adv_Swing extends JFrame
{
public Adv_Swing()
{
setLayout(new FlowLayout());
setSize(300,400);
String[][] rows = {{"01","ram"},{"02","Sham"}};

String[] clm = {"Id","Name"};

JTable t1 = new JTable(rows,clm);


JScrollPane p1 = new JScrollPane(t1);
add(p1);
setVisible(true);
}
public static void main(String[] args) {
new Adv_Swing();
}
}
Prograss Bar

package AWT;
import java.awt.*;
import javax.swing.*;
class Adv_Swing extends JFrame
{
public Adv_Swing()
{
setLayout(new FlowLayout());
setSize(300,400);
JProgressBar jp = new JProgressBar(1,100);
jp.setValue(10);
jp.setStringPainted(true);
add(jp);
setVisible(true);
}
public static void main(String[] args) {
new Adv_Swing();
}
}

You might also like