0% found this document useful (0 votes)
7 views

Swing Components Programs

The document contains multiple Java Swing GUI examples demonstrating various components such as JLabel, JButton, JTextField, JCheckBox, JRadioButton, JToggleButton, JMenu, JTable, and JTabbedPane. Each example illustrates the creation and layout of these components within a JFrame, showcasing their functionalities and how they can be integrated into a graphical user interface. Additionally, it discusses layout managers like FlowLayout and BorderLayout used to organize components within containers.

Uploaded by

shreyassupe346
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)
7 views

Swing Components Programs

The document contains multiple Java Swing GUI examples demonstrating various components such as JLabel, JButton, JTextField, JCheckBox, JRadioButton, JToggleButton, JMenu, JTable, and JTabbedPane. Each example illustrates the creation and layout of these components within a JFrame, showcasing their functionalities and how they can be integrated into a graphical user interface. Additionally, it discusses layout managers like FlowLayout and BorderLayout used to organize components within containers.

Uploaded by

shreyassupe346
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/ 14

KLE Society’s

Bachelor of Computer application


P.C.Jabin Science College, HUBBALLI

//Demo on JLabel

import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Jlabeldemo extends JFrame {

Jlabeldemo()
{
setLayout(new FlowLayout());
ImageIcon img=new ImageIcon("D:\\positive thoughts\\joyful.jpg");
JLabel jl1=new JLabel();
JLabel jl2=new JLabel("HELLO");
JLabel jl3=new JLabel(img);
JLabel jl4=new JLabel("Goog Morning",img,JLabel.LEFT);
add(jl1);add(jl2);add(jl3);add(jl4);
setSize(500,400);
setVisible(true);
}

public static void main(String[] args) {


// TODO Auto-generated method stub
new Jlabeldemo();
}

}
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

//Demo on JButton

import java.awt.Button;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class JButtondemo extends JFrame {


JButtondemo()
{
setLayout(new FlowLayout());
JButton b1=new JButton();
JButton b2=new JButton("CLICK");
ImageIcon img=new ImageIcon("D:\\positive thoughts\\joyful.jpg");
JButton b3=new JButton(img);
b1.setIcon(img);
b1.setEnabled(false);
b2.setText("POSITIVE THOUGHT");
b2.setMnemonic(NORMAL);
add(b1);add(b2);add(b3);
setSize(500,400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JButtondemo();
}
}
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

//Demo on JTextField

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Jtextfielddemo extends JFrame {

Jtextfielddemo()
{
setLayout(new FlowLayout());
JTextField t1=new JTextField();
add(t1);
JTextField t2=new JTextField("Enter Name here");
add(t2);
JTextField t3=new JTextField("Enter Age here",20);
add(t3);
JTextField t4=new JTextField(50);
add(t4);

setSize(500,400);
setVisible(true);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Jtextfielddemo();
}

}
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

//Demo on JCheckBox
import java.awt.Checkbox;
import java.awt.FlowLayout;

import javax.swing.*;

public class JCheckBoxdedmo extends JFrame {


JCheckBox c1,c2,c3,c4,c5;

JCheckBoxdedmo(){
setLayout(new FlowLayout());
c1=new JCheckBox("Singing");
ImageIcon img=new ImageIcon("D:\\positive thoughts\\joyful.jpg");
c2=new JCheckBox(img,true);
c3=new JCheckBox(img);
c4=new JCheckBox("Swimming",img);
c5=new JCheckBox("Dancing",img,true);

add(c1);add(c2);add(c3);add(c4);add(c5);
setSize(500,400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JCheckBoxdedmo();
}
}

//Jradio button example

import javax.swing.*;
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();
bg.add(rb1);bg.add(rb2);
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

b=new JButton("click");
b.setBounds(100,150,80,30);
b.addActionListener(this);
add(rb1);add(rb2);add(b);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(rb1.isSelected()){
JOptionPane.showMessageDialog(this,"You are Male.");
}
if(rb2.isSelected()){
JOptionPane.showMessageDialog(this,"You are Female.");
}
}
public static void main(String args[]){
new RadioButtonExample();
}
}

// Demo On JToggleButton
package lab;

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

public class togle1 extends JFrame implements ActionListener {


JToggleButton b1=new JToggleButton("ON");
JLabel l1=new JLabel();
togle1(){
setTitle("JToggleButton with ItemListener Example");
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

setLayout(new FlowLayout());
b1.addActionListener(this);
add(b1);
add(l1);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(b1.isSelected())
{
l1.setText("It's ON");
System.out.println("It is on");
}
else
{
l1.setText("It's Off");
System.out.println("It is off");
}
}
public static void main(String[] args) {
new togle1();
}
}

// Demo Program on JCheckBox, JRadioButton, JList,


JCombobox,

public class JcheckboxandRadioex extends JFrame implements ItemListener{


JCheckBox c1,c2,c3,c4;
JRadioButton b1,b2;
ButtonGroup cbg;
String msg;
JTextArea t1;
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

JcheckboxandRadioex()
{
setLayout(new FlowLayout());
c1=new JCheckBox("Singing",true);
c2=new JCheckBox("Music",true);
c3=new JCheckBox("Dancing");
c4=new JCheckBox("Swimming");

cbg=new ButtonGroup();
b1=new JRadioButton("1 Sem",true);

b2=new JRadioButton("2 Sem");


cbg.add(b1);
cbg.add(b2);
add(c1);add(c2);add(c3);add(c4);
add(b1);add(b2);

String week[]={"mon","tue","wed","thu","fri","sat","sun"};
JList list=new JList(week);

//list.setBounds(100,100, 75,75);
list.setSelectedIndex(3);
add(list);

t1=new JTextArea(8,6);
add(t1);

JComboBox ch= new JComboBox(week);


add(ch);
t1.setEnabled(false);
c1.addItemListener(this);
c2.addItemListener(this);
b1.addItemListener(this);
b2.addItemListener(this);

setTitle("Check box Demo Program");


setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JcheckboxandRadioex ob=new JcheckboxandRadioex();
}
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

public void itemStateChanged(ItemEvent e) {


if(c1.isSelected())
msg="Singing ";
if(c2.isSelected())
msg+=" Music";

if(b1.isSelected())
msg+="\n1st Sem ";
if(b2.isSelected())
msg+="\n2nd Sem ";
t1.setText(msg);msg="";
}
}

// Demo on JMenu

package lab;

import javax.swing.*;
import java.awt.event.*;
public class menu1 extends JFrame
{
JMenuBar mb=new JMenuBar();
JMenu fl=new JMenu("File");
JMenu f2=new JMenu("Edit");
JMenu f3=new JMenu("View");
JMenu f4=new JMenu("Help");
JMenuItem m1=new JMenuItem("New");
JMenuItem m2=new JMenuItem("Open File");
JMenuItem m3=new JMenuItem("Save");
JMenuItem m4=new JMenuItem("Save As");
JMenuItem m5=new JMenuItem("Select");
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

JMenuItem m6=new JMenuItem("Cut");


JMenuItem m7=new JMenuItem("Copy");
JMenuItem m8=new JMenuItem("Paste");
menu1(){
fl.add(m1);fl.add(m2);
fl.addSeparator();
fl.add(m3);fl.add(m4);
f2.add(m5);f2.add(m6);
f2.addSeparator();
f2.add(m7);
f2.add(m8);
mb.add(fl);
mb.add(f2);
mb.add(f3);
mb.add(f4);
setJMenuBar(mb);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new menu1();
}
}

// Demo on JTable
package lab;

import javax.swing.*;
public class shopping extends JFrame {
shopping(){
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

String result[][] ={{"I-Phone","68000","2022","America"},


{"Lap-Top","78000","2010","India"}};
String title[]={"Product-Name","Price","Model","Made in"};
JTable tb=new JTable(result,title);
JScrollPane sp=new JScrollPane(tb);
add(sp);
setTitle("Table Creation Example");
setSize(600,600);
setVisible(true);
}
public static void main(String[] args) {
new shopping();
}
}

// Demo on Tabbed Pane


import java.awt.Checkbox;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Tabbedpaneex extends JFrame {


Tabbedpaneex()
{
JTabbedPane tb=new JTabbedPane();
JCheckBox c1=new JCheckBox("Singing",true);
JCheckBox c2=new JCheckBox("Music",true);
JPanel p1=new JPanel();
p1.add(c1);
p1.add(c2);

JCheckBox c3=new JCheckBox("UI UX",true);


JCheckBox c4=new JCheckBox("Fine Arts",true);
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

JPanel p2=new JPanel();


p2.add(c3);
p2.add(c4);
tb.add("Hobby",p1);
tb.add("OEC",p2);
add(tb);
setTitle("Tabbed Pane Example Program");
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Tabbedpaneex();
}
}

Containers - Layout
• Each container has a layout manager
– Determines the size, location of contained widgets.
• Setting the current layout of a container:
void setLayout(LayoutManager lm)
• LayoutManager implementing classes:
– BorderLayout
– BoxLayout
– FlowLayout
– GridLayout
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

// Program on FlowLayout

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

public class Flowlayoutdemo extends JFrame {

JButton b1,b2,b3,b4,b5,b6;
public Flowlayoutdemo()
{
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

setLayout(new FlowLayout(FlowLayout.RIGHT,20,50));
b1=new JButton("ONE");
b2=new JButton("TWO");
b3=new JButton("THREE");
b4=new JButton("FOUR");
b5=new JButton("FIVE");
b6=new JButton("SIX");
add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);
setTitle("Flow Layout Demo");
setSize(500,400);
setVisible(true);

}
public static void main(String a[])
{
new Flowlayoutdemo();
}
}

// Program on BorderLayout
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JButton;

public class BorderLayoutdemo extends JFrame {

JButton b1,b2,b3,b4,b5,b6;
public BorderLayoutdemo ()
{
KLE Society’s
Bachelor of Computer application
P.C.Jabin Science College, HUBBALLI

setLayout(new BorderLayout());
b1=new JButton("ONE");
b2=new JButton("TWO");
b3=new JButton("THREE");
b4=new JButton("FOUR");
b5=new JButton("FIVE");

add(b1,BorderLayout.NORTH);
add(b2,BorderLayout.SOUTH);
add(b3,BorderLayout.EAST);
add(b4,BorderLayout.WEST);
add(b5,BorderLayout.CENTER);
setTitle("Border Layout Demo");
setSize(500,400);
setVisible(true);

}
public static void main(String a[])
{
new BorderLayoutdemo ();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy