Monday, June 24, 2024

Shutdown Or Restart PC Java Swing

// Shutdown Or Restart PC Swing

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.*;


public class SR extends JFrame implements ActionListener

{

JFrame f;

JButton b1,b2;


public SR()

{

f=new JFrame();


b1=new JButton("Shutdown");

b1.addActionListener(this);


b2=new JButton("Restart");

b2.addActionListener(this);

f.add(b1);

f.add(b2);


f.setLayout(new FlowLayout());

f.setSize(300,300);

f.setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

try

{

Runtime r=Runtime.getRuntime();


if(ae.getSource()==b1)

{

r.exec("shutdown -s -t 5");

}

else if(ae.getSource()==b2)

{

r.exec("shutdown -r -t 5");

}

}

catch(Exception e)

{

}

}

public static void main(String args[])

{

new SR();

}

}


Output :-





Visible Or Not Visible Oval Java Swing

// Visible Or Not Visible Oval Swing

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class draw extends JFrame implements ActionListener

{

JFrame f;

JButton b1,b2;


public draw()

{

f=new JFrame();


b1=new JButton("Visible");

b1.addActionListener(this);


b2=new JButton("Not Visible");

b2.addActionListener(this);


f.add(b1);

f.add(b2);


f.setLayout(new FlowLayout());

f.setDefaultCloseOperation(EXIT_ON_CLOSE);

f.setSize(500,500);

f.setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b1)

{

Graphics g=f.getGraphics();

g.setColor(Color.RED);

g.fillOval(100,100,200,200);

}

if(ae.getSource()==b2)

{

Graphics g=f.getGraphics();

g.setColor(Color.WHITE);

g.fillRect(0,0,1500,1500);

}

}

public static void main(String args[])

{

new draw();

}


Output :-


                            If You Click Visible Button Then Oval Will Apper And 
                          If You Click Not Visible Button Then Oval Will Disapper.   
                

 


Friday, June 14, 2024

Bouncing Ball in Java Swing

// Bouncing Ball or Circle Swing

import java.awt.*;

import javax.swing.*;

import java.util.*;


public class BC extends JFrame

{

JFrame f;

JPanel p;

public BC()

{

f=new JFrame();

p=new JPanel()

{

int x=150;

int y=50;

int r=50;

int dx=11;

int dy=7;

 

public void paint(Graphics g)

{

Float r2,g1,b;

Random r1=new Random();

r2=r1.nextFloat();

g1=r1.nextFloat();

b=r1.nextFloat();

Color c=new Color(r2,g1,b);

g.setColor(c);

super.paintComponent(g);

g.fillOval(x-r,y-r,r*2,r*2);


repaint();

Rectangle bounds=getBounds();

if((x-r+dx<0) || (x+r+dx > bounds.width))

dx=-dx;


if((y-r+dy<0) || (y+r+dy > bounds.height))

dy=-dy;


x+=dx;

y+=dy;

try

{

Thread.sleep(10);

}

catch(InterruptedException e)

{

}

}

};

f.add(p);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

f.setSize(300,300);

f.setVisible(true);

}

public static void main(String args[])

{

new BC();

}

}

Wednesday, June 12, 2024

Moving String with Random Color in Java Swing

// Moving String in Swing

import java.awt.*;

import javax.swing.*;

import java.util.*;


public class demo3 extends JFrame

{

JFrame f;

JPanel p;

public demo3()

{

f=new JFrame();


p=new JPanel()

{

int x=60;

int y=60;

int flag=1;


public void paint(Graphics g1)

{

Random r1=new Random();

Float r=r1.nextFloat();

Float g=r1.nextFloat();

Float b=r1.nextFloat();

Color c=new Color(r,g,b);

g1.setColor(c);

Font f1=new Font("Lucida Sans",Font.BOLD,30);

g1.setFont(f1);

super.paintComponent(g1);

g1.drawString("Sidhdhapura Amit's Blog",x,y);

x=x+10*flag;

if(x>800)

{

flag=-1;

}


if(x<100)

{

flag=1;

}


try

{

Thread.sleep(100);

}

catch(Exception e)

{

}

repaint();

}

};

f.add(p);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

f.setSize(800,500);

f.setVisible(true);

}

public static void main(String args[])

{

new demo3();

}

}

Data Input Into File Java Swing

//Input Data into File Swing

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;


public class Form2 extends JFrame implements ActionListener 

{

FileWriter fw;

JFrame f;

JLabel l1,l2,l3,l4,l5,l6;

JTextField t1,t2;

JPasswordField p1;

JComboBox cb1;

JTextArea ta;

JScrollPane sp1;

JButton b1,b2;


public Form2()

{

f=new JFrame("Register Form");

l6=new JLabel("REGISTER FORM");

l6.setBounds(200,5,250,100);

l6.setFont(new Font("Lucida Sans",Font.BOLD,30));

l1=new JLabel("Name");

l1.setBounds(150,100,150,30);

l1.setFont(new Font("Lucida Sans",Font.BOLD,13));


l2=new JLabel("Password");

l2.setBounds(150,160,200,30);

l2.setFont(new Font("Lucida Sans",Font.BOLD,13));

l3=new JLabel("Address");

l3.setBounds(150,220,250,30);

l3.setFont(new Font("Lucida Sans",Font.BOLD,13));


l4=new JLabel("City");

l4.setBounds(150,345,350,30);

l4.setFont(new Font("Lucida Sans",Font.BOLD,13));


l5=new JLabel("Phone");

l5.setBounds(150,410,400,30);

l5.setFont(new Font("Lucida Sans",Font.BOLD,13));


t1=new JTextField();

t1.setBounds(250,100,250,35);

t1.setFont(new Font("Lucida Sans",Font.BOLD,13));


p1=new JPasswordField();

p1.setBounds(250,160,250,35);

p1.setFont(new Font("Lucida Sans",Font.BOLD,13));

ta=new JTextArea();

sp1=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

           JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

sp1.setBounds(250,220,250,100);

ta.setFont(new Font("Lucida Sans",Font.BOLD,13));

String []ct={"","Rajkot","Gondal","Virpur","Jetpur","Junagadh"};

cb1=new JComboBox(ct);

cb1.setBounds(250,345,250,35);

cb1.setFont(new Font("Lucida Sans",Font.BOLD,13));

t2=new JTextField();

t2.setBounds(250,410,250,35);

t2.setFont(new Font("Lucida Sans",Font.BOLD,13));


b1=new JButton("Submit");

b1.setBounds(250,480,100,50);

b1.addActionListener(this);

b1.setFont(new Font("Lucida Sans",Font.BOLD,13));


b2=new JButton("Cancle");

b2.setBounds(400,480,100,50);

b2.addActionListener(this);

b2.setFont(new Font("Lucida Sans",Font.BOLD,13));


f.add(l6);

f.add(l1);

f.add(t1);


f.add(l2);

f.add(p1);


f.add(l3);

f.add(sp1);


f.add(l4);

f.add(cb1);


f.add(l5);

f.add(t2);


f.add(b1);

f.add(b2);

f.setLayout(null);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

f.setSize(700,600);

f.setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

try

{

fw=new FileWriter("BScIT.txt");

String s=t1.getText();

String s1=p1.getText();

String s2=ta.getText();

String s3=cb1.getSelectedItem().toString();

String s4=t2.getText();


fw.write(s);

fw.write(s1);

fw.write(s2);

fw.write(s3);

fw.write(s4);

fw.close();

}

catch(Exception e)

{

System.out.println(e);

}

if(ae.getSource()==b1)

{

JOptionPane.showMessageDialog(null,"Input Data Successfully!");

}

if(ae.getSource()==b2)

{

System.exit(0);

}

}

public static void main(String args[])

{

new Form2();

}

}


Output :-


                        
                         If All Fields Are Filled Then Click Submit Button Then
                                      Get Input Data Successfully Message.

                            
                             Click Ok Button Then BScIT.txt Notepad File Will Appear




Tuesday, June 11, 2024

Simple Registration Form Java Swing

//Simple Registration Form Swing

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class Form extends JFrame implements ActionListener 

{

JFrame f;

JLabel l1,l2,l3,l4,l5,l6;

JTextField t1,t2;

JPasswordField p1;

JComboBox cb1;

JTextArea ta;

JScrollPane sp1;

JButton b1,b2;


public Form()

{

f=new JFrame("Register Form");

l6=new JLabel("REGISTER FORM");

l6.setBounds(200,5,250,100);

l6.setFont(new Font("Lucida Sans",Font.BOLD,30));

l1=new JLabel("Name");

l1.setBounds(150,100,150,30);

l1.setFont(new Font("Lucida Sans",Font.BOLD,13));


l2=new JLabel("Password");

l2.setBounds(150,160,200,30);

l2.setFont(new Font("Lucida Sans",Font.BOLD,13));

l3=new JLabel("Address");

l3.setBounds(150,220,250,30);

l3.setFont(new Font("Lucida Sans",Font.BOLD,13));


l4=new JLabel("City");

l4.setBounds(150,345,350,30);

l4.setFont(new Font("Lucida Sans",Font.BOLD,13));


l5=new JLabel("Phone");

l5.setBounds(150,410,400,30);

l5.setFont(new Font("Lucida Sans",Font.BOLD,13));


t1=new JTextField();

t1.setBounds(250,100,250,35);

t1.setFont(new Font("Lucida Sans",Font.BOLD,13));


p1=new JPasswordField();

p1.setBounds(250,160,250,35);

p1.setFont(new Font("Lucida Sans",Font.BOLD,13));


ta=new JTextArea();

sp1=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

sp1.setBounds(250,220,250,100);

ta.setFont(new Font("Lucida Sans",Font.BOLD,13));

String []ct={"Rajkot","Gondal","Virpur","Jetpur","Junagadh"};

cb1=new JComboBox(ct);

cb1.setBounds(250,345,250,35);

cb1.setFont(new Font("Lucida Sans",Font.BOLD,13));

t2=new JTextField();

t2.setBounds(250,410,250,35);

t2.setFont(new Font("Lucida Sans",Font.BOLD,13));


b1=new JButton("Submit");

b1.setBounds(250,480,100,50);

b1.addActionListener(this);

b1.setFont(new Font("Lucida Sans",Font.BOLD,13));


b2=new JButton("Cancle");

b2.setBounds(400,480,100,50);

b2.addActionListener(this);

b2.setFont(new Font("Lucida Sans",Font.BOLD,13));


f.add(l6);

f.add(l1);

f.add(t1);

f.add(l2);

f.add(p1);

f.add(l3);

f.add(sp1);

f.add(l4);

f.add(cb1);

f.add(l5);

f.add(t2);

f.add(b1);

f.add(b2);

f.setLayout(null);

f.setSize(500,500);

f.setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b1)

{

JOptionPane.showMessageDialog(null,"Submitted Successfully!!");

}

if(ae.getSource()==b2)

{

System.exit(0);

}

}

public static void main(String args[])

{

new Form();

}

}


Output :-




Monday, June 10, 2024

Moving Vehicles in Java Swing

 // Moving Vehicles Swing

import java.awt.*;

import javax.swing.*;


public class demo extends JFrame

{

JFrame f;

JPanel p;

        

        public demo()

{

f=new JFrame();

p=new JPanel()

{

int x=0;

int y=100;


int w=250;

int h=140;


int w1=60;

int h1=60;

int w2=60;

int h2=60;

int x1=40;

int y1=200;


int x2=140;

int y2=200;


        int flag=10;

int flag1=10;

int flag2=10; 

int max_x=1300;

int max_y=500;

public void paint(Graphics g)

{

super.paint(g);

g.setColor(Color.ORANGE);

g.fillRect(x,y,w,h);


g.setColor(Color.BLACK);

g.fillOval(x1,y1,w1,h1);  

g.fillOval(x2,y2,w2,h2); 

if(x+w > max_x || x1+w1 > max_x || x2+w2 > max_x || x<0 || x1<0 || x2<0)

{

flag=-flag;

flag1=-flag1;

flag2=-flag2;

}

x=x+flag;

x1=x1+flag1;

x2=x2+flag2;


try

{

Thread.sleep(100);

}

catch(Exception e)

{

}

repaint();

}

};


f.add(p);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

f.setVisible(true);

f.setSize(1300,500);

}

public static void main(String args[])

{

new demo();

}

}