0% found this document useful (0 votes)
71 views25 pages

Swing (Jbutton, Jlabel ) Awt (Button, Label ) : Java Gui

The document describes Java GUI components and how to create graphical user interfaces (GUIs) using Java. It discusses common GUI components like frames, panels, buttons, text fields, radio buttons, checkboxes and layout managers. It provides code examples to create a login form using these components, adding them to frames and panels with different layouts. It also includes an example of how to handle button click events using action listeners.

Uploaded by

Oussema Bahri
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)
71 views25 pages

Swing (Jbutton, Jlabel ) Awt (Button, Label ) : Java Gui

The document describes Java GUI components and how to create graphical user interfaces (GUIs) using Java. It discusses common GUI components like frames, panels, buttons, text fields, radio buttons, checkboxes and layout managers. It provides code examples to create a login form using these components, adding them to frames and panels with different layouts. It also includes an example of how to handle button click events using action listeners.

Uploaded by

Oussema Bahri
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/ 25

Java GUI

Component container helper

Button Color
Frame
Radio Font
Textfield …. panel event…

Swing (JButton, JLabel…)


AWT(Button, Label …)
JFrame in main :
public static void main(String[] args) {

JFrame b= new JFrame();

b.setTitle("mustapha");

b.setSize(300, 200);

b.setVisible(true);

b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b.setResizable(false);

JFrame in Constructor :
public class j2 extends JFrame {

public j2(){

this.SetTitle("frame2");

this.setSize(new Dimension(500,300));

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocation(100, 100);

JFrame in classe :
public void show(){

this.SetTitle("sss");
this.SetSize("500,250");

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocation(100, 100);

JPanel:
public class jgui extends JFrame {

JPanel n= new JPanel();

public jgui() {

kj();

public void kj(){

setTitle("frame2");

setSize(new Dimension(500,300));

setResizable(false);

setVisible(true);

add(n);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

n.setBackground(Color.RED);

}
Add button:
setLayout(new FlowLayout());

add(b1);

add(b2);

add(f1);

panel

frame

button

Textfield

Layout:

FlowLayout GridLayout BorderLayout AbsoluteLayout


FlowLayout:
setLayout(new FlowLayout(FlowLayout.CENTER,10,30));
setLayout(new FlowLayout(FlowLayout.Right,30,50));

GridLayout:
setLayout(new GridLayout(2,6,10,30));

BorderLayout:
setLayout(new BorderLayout(10,20));

add(b1,BorderLayout.EAST);
add(b2,BorderLayout.NORTH);
add(b3,BorderLayout.SOUTH);
add(b4,BorderLayout.WEST);

AbsoluteLayout(x,y,w,h):
setLayout(null);

b1.setBounds(100,10,200,200);
add(b1);
Y

X
W H
Add button in a frame:
P1.add(b1);

In a panel , we shouldn’t use BorderLayout.

New project:
File Edit Help

Full name :

Password :

Login New account

Male female other

Single in a relationship

Material : CG CA SA DB
Menu

JMenu JMenuBar

CODE :
package gradleproject1;

import java.awt.*;

import javax.swing.*;

public class login extends JFrame{

JPanel p1,p2,p3;

JLabel l1,l2,l3,l4,l5;

JTextField t1;

JPasswordField t2;

JButton b1,b2,n1,n2,n3,n4,n5;

JRadioButton r1,r2,r3,r4,r5;

ButtonGroup G1,G2;

JCheckBox ch1,ch2,ch3,ch4;

JMenu m1,m2,m3;

JMenuBar mb1;

public login() {

this.setTitle("form");

this.setVisible(true);
this.setSize(500, 700);

this.setLocation(200, 50);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setResizable(false);

this.setLayout(null);

ButtonGroup G1= new ButtonGroup();

ButtonGroup G2= new ButtonGroup();

b1= new JButton("login");

b2= new JButton("new account");

n1= new JButton("menu1");

n2= new JButton("menu2");

n3= new JButton("menu3");

n4= new JButton("menu4");

n5= new JButton("menu5");

JCheckBox ch1= new JCheckBox("CG");

JCheckBox ch2= new JCheckBox("CA");

JCheckBox ch3= new JCheckBox("SA");

JCheckBox ch4= new JCheckBox("DB");

JMenu m1= new JMenu("File");

JMenu m2= new JMenu("Edit");

JMenu m3= new JMenu("Help");

JMenuBar mb1= new JMenuBar();

t1= new JTextField();

t2= new JPasswordField();

l1= new JLabel("Full name:");

l2=new JLabel("Password:");

l3= new JLabel("sex:");

l4=new JLabel("status:");

l5= new JLabel("material:");


r1= new JRadioButton ("Male");

r2= new JRadioButton ("Female");

r3= new JRadioButton ("Other");

r4= new JRadioButton ("Single");

r5= new JRadioButton ("In a relationship");

l1.setBounds(50, 50, 100, 25);

t1.setBounds(200, 50, 150, 25);

l2.setBounds(50, 100, 100, 25);

t2.setBounds(200, 100, 150, 25);

b1.setBounds(120, 160, 75, 25);

b2.setBounds(250, 160, 75, 25);

r1.setBounds(110, 250, 100, 25);

r2.setBounds(250, 250, 100, 25);

r3.setBounds(390, 250, 100, 25);

r4.setBounds(110, 300, 100, 25);

r5.setBounds(250, 300, 100, 25);

l3.setBounds(10, 250, 100, 25);

l4.setBounds(10, 300, 100, 25);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(b1);

this.add(b2);

this.add(l3);

G1.add(r1);

G1.add(r2);

G1.add(r3);

this.add(l4);
G2.add(r4);

G2.add(r5);

this.add(r1);

this.add(r2);

this.add(r3);

this.add(r4);

this.add(r5);

l5.setBounds(10, 350, 100, 25);

this.add(l5);

ch1.setBounds(130, 350, 50, 25);

ch2.setBounds(210, 350, 50, 25);

ch3.setBounds(280, 350, 50, 25);

ch4.setBounds(350, 350, 50, 25);

n5.setBackground(Color.red);

n4.setBackground(Color.BLUE);

n3.setBackground(Color.BLUE);

n2.setBackground(Color.GREEN);

n1.setBackground(Color.GREEN);

n1.setForeground(Color.DARK_GRAY);

n2.setForeground(Color.DARK_GRAY);

n3.setForeground(Color.YELLOW);

n4.setForeground(Color.YELLOW);

n5.setForeground(Color.white);

this.add(ch1);

this.add(ch2);

this.add(ch3);

this.add(ch4);

m1.add(n5);

m2.add(n1);
m2.add(n2);

m3.add(n3);

m3.add(n4);

mb1.add(m1);

mb1.add(m2);

mb1.add(m3);

mb1.setBounds(0, 0, 500, 25);

this.add(mb1); }

public class Main {

public static void main(String[] args) {

login a= new login();

EVENTS:

ActionListener ActionEvent
ssss
Code:

package event;

import java.awt.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.* ;

public class ev extends JFrame implements ItemListener {


vn v = new vn();
int x=100;
int y=7;
JButton b1,b2,b3,b4,b5,b6;
JLabel l1,l2,l3;
JTextField t1,t3;
JPasswordField t2;
JPanel p1,p2,p3;
JRadioButton r1,r2,r3,r4,r5;
ButtonGroup g1;
JCheckBox c1,c2,c3;
public ev() {
this.setSize(500,600);
this.setTitle("Event");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1= new JPanel();
p2= new JPanel();
p3= new JPanel();
b1=new JButton("Save");
b2=new JButton("Show");
b3=new JButton("Right");
b4=new JButton("Left");
b5=new JButton("min");
b6=new JButton("max");
l1=new JLabel("Full name");
l2=new JLabel("Password");
l3=new JLabel();
c1=new JCheckBox("Bold");
c2=new JCheckBox("Italic");
c3=new JCheckBox("Plain");
t1= new JTextField();
t2= new JPasswordField();
t3= new JTextField();
r1= new JRadioButton("Red");
r2= new JRadioButton("Blue");
r3= new JRadioButton("yellow");
r4= new JRadioButton("Green");
r5= new JRadioButton("Grey");
g1= new ButtonGroup();
p1.setBackground(Color.CYAN);
p2.setBackground(Color.BLUE);
p3.setBackground(Color.RED);
this.setLayout(new GridLayout(3,1));
this.add(p1);
this.add(p2);
this.add(p3);
p1.setLayout(null);
p2.setLayout(null);
p3.setLayout(null);
l1.setBounds(20, 10, 100, 25);
l2.setBounds(20, 80, 100, 25);
l3.setBounds(x, 10, 300, 50);
l3.setForeground(Color.yellow);
l3.setFont(new Font("atilic",Font.CENTER_BASELINE,y));
t1.setBounds(150, 10, 200, 25);
t2.setBounds(150, 80, 200, 25);
b1.setBounds(120, 150, 100, 25);
b2.setBounds(270, 150, 100, 25);
p1.add(l1);
p1.add(l2);
p1.add(t1);
p1.add(t2);
p1.add(b1);
p1.add(b2);

t3.setBounds(100, 10, 300, 25);


r1.setBounds(10, 100, 80, 25);
r2.setBounds(100, 100, 80, 25);
r3.setBounds(190, 100, 80, 25);
r4.setBounds(280, 100, 80, 25);
r5.setBounds(370, 100, 80, 25);
g1.add(r1);
g1.add(r2);
g1.add(r3);
g1.add(r4);
g1.add(r5);
p2.add(t3);
p2.add(r1);
p2.add(r2);
p2.add(r3);
p2.add(r4);
p2.add(r5);
c1.setBounds(50, 100, 100, 25);
c2.setBounds(200, 100, 100, 25);
c3.setBounds(350, 100, 100, 25);
b3.setBounds(20, 150, 100, 25);
b4.setBounds(140, 150, 100, 25);
b5.setBounds(260, 150, 100, 25);
b6.setBounds(380, 150, 100, 25);
p3.add(l3);
p3.add(c1);
p3.add(c2);
p3.add(c3);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
b1.addActionListener(v);
b2.addActionListener(v);
b3.addActionListener(v);
b4.addActionListener(v);
b5.addActionListener(v);
b6.addActionListener(v);
r1.addActionListener(v);
r2.addActionListener(v);
r3.addActionListener(v);
r4.addActionListener(v);
r5.addActionListener(v);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);

}
@Override
public void itemStateChanged(ItemEvent e) {
if (c1.isSelected()&& c2.isSelected()&& c3.isSelected()){
l3.setFont(new Font("atilic",Font.BOLD+Font.ITALIC+Font.PLAIN,y));
}
else if(c1.isSelected()) {
l3.setFont(new Font("atilic",Font.BOLD,y));
}
else if (c2.isSelected()){
l3.setFont(new Font("atilic",Font.ITALIC,y));
}
else if (c3.isSelected()){
l3.setFont(new Font("atilic",Font.PLAIN,y));
}
else if (c2.isSelected()&& c1.isSelected()){
l3.setFont(new Font("atilic",Font.ITALIC+Font.BOLD,y));
}
else if (c3.isSelected()&& c1.isSelected()){
l3.setFont(new Font("atilic",Font.PLAIN+Font.BOLD,y));
}
else if (c2.isSelected()&& c3.isSelected()){
l3.setFont(new Font("atilic",Font.ITALIC+Font.PLAIN,y));
}
}

public class vn implements ActionListener {


public void actionPerformed(ActionEvent e) {
String s1,s2;
if (e.getSource()==b1){
s1=t1.getText();
s2=t2.getText();
System.out.println(s1+" "+s2);
t1.setText(" ");
t2.setText(" ");

}
if (e.getSource()==b2){
s1=t1.getText();
s2=t2.getText();
t3.setText(s1+" " +s2);
l3.setText("welcome"+" "+s1);
t1.setText(" ");
t2.setText(" ");

}
if (e.getSource()==r1){
p2.setBackground(Color.red);
t3.setForeground(Color.red);
l3.setForeground(Color.PINK);
}
if (e.getSource()==r2){
p2.setBackground(Color.BLUE);
t3.setForeground(Color.BLUE);
l3.setForeground(Color.BLUE);
}
if (e.getSource()==r3){
p2.setBackground(Color.YELLOW);
t3.setForeground(Color.YELLOW);
l3.setForeground(Color.yellow);
}
if (e.getSource()==r4){
p2.setBackground(Color.GREEN);
t3.setForeground(Color.GREEN);
l3.setForeground(Color.GREEN);
}
if (e.getSource()==r5){
p2.setBackground(Color.GRAY);
t3.setForeground(Color.GRAY);
l3.setForeground(Color.GRAY);
}
if (e.getSource()==b3){
x+=10;
l3.setBounds(x, 10, 300, 50);
}
if (e.getSource()==b4){
x-=10;
l3.setBounds(x, 10, 300, 50);
}
if (e.getSource()==b5){
y-=2;
l3.setFont(new Font("atilic",Font.CENTER_BASELINE,y));
}
if (e.getSource()==b6){
y+=2;
l3.setFont(new Font("atilic",Font.CENTER_BASELINE,y));
}
}

}
}

package event;

public class Main {

public static void main(String[] args) {


ev m = new ev();

}
Mouse Event:

package mouse;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;
public class me extends JFrame {

JPanel p1,p2;

JLabel l1,l2;

nm n= new nm();

public me(){

this.setSize(300,500);

this.setTitle("M_Event");

this.setVisible(true);

this.setResizable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLayout(new GridLayout(2,1));

l1=new JLabel(" Welcome 1");

l2=new JLabel(" Welcome 2");

p1= new JPanel();

p2= new JPanel();

p2.setBackground(Color.BLUE);

p1.setBackground(Color.RED);

this.add(p1);

this.add(p2);

p1.setLayout(null);

p2.setLayout(null);

l1.setBounds(100, 50, 100, 25);

l2.setBounds(100, 50, 100, 25);

l2.setForeground(Color.MAGENTA);

l1.setForeground(Color.WHITE);

p1.add(l1);

p2.add(l2);

p1.addMouseListener(n);

p2.addMouseListener(n);
p2.addMouseMotionListener(n);

private class nm implements MouseListener,MouseMotionListener,MouseWheelListener{

@Override

public void mouseClicked(MouseEvent e) {

p2.setBackground(Color.CYAN);

l2.setText("Mouse Clicked");

@Override

public void mousePressed(MouseEvent e) {

@Override

public void mouseReleased(MouseEvent e) {

@Override

public void mouseEntered(MouseEvent e) {

l1.setText("Mouse Entered");

p1.setBackground(Color.GREEN);

@Override

public void mouseExited(MouseEvent e) {

l1.setText("Mouse Exited");
p1.setBackground(Color.RED);

@Override

public void mouseDragged(MouseEvent e) {

@Override

public void mouseMoved(MouseEvent e) {

l2.setText(e.getX()+" " + e.getY());

@Override

public void mouseWheelMoved(MouseWheelEvent e) {

public class Main {

public static void main(String[] args) {

me m= new me();

You might also like