Add MS. Calculator or MS. Notepad Button in Your Java Project
How to Add MS. Calculator or MS. Notepad Button in Your Java Project.
And coding to run your any dos command through click a button in Java. Let's See This code Example and make your project some thing different.
I hope you Like this code, if any problem plz comment or leave a massage.
/*
* calculator.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class calculator extends JFrame implements MouseListener
{
JButton btn1,btn2;
public calculator()
{
Container c=this.getContentPane();
c.setLayout(null);
btn1=new JButton("Calculator");
btn1.setBounds(5,5,150,40);
btn2=new JButton("Notepad");
btn2.setBounds(5,55,150,40);
btn1.addMouseListener(this);
btn2.addMouseListener(this);
c.add(btn1);
c.add(btn2);
}
public void mouseClicked(MouseEvent me)
{
if(me.getSource()==btn1)
{
try
{
String[] command = new String[1];
command[0] = "calc";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
JOptionPane.showMessageDialog(null,"Thanks to using Microsoft Calculator:::Rahul Raj (Satya)\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
if(me.getSource()==btn2)
{
try
{
String[] command = new String[1];
command[0] = "notepad";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
JOptionPane.showMessageDialog(null,"Thanks to using Microsoft Calculator:::Rahul Raj (Satya)\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
}
public void mouseEntered(MouseEvent me)
{
if(me.getSource()==btn1)
{
btn1.setForeground(Color.blue);
}
if(me.getSource()==btn2)
{
btn2.setForeground(Color.blue);
}
}
public void mousePressed(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
if(me.getSource()==btn1)
{
btn1.setForeground(Color.red);
}
if(me.getSource()==btn2)
{
btn2.setForeground(Color.red);
}
}
public void mouseReleased(MouseEvent me)
{
}
public static void main(String args[])
{
calculator a=new calculator();
a.setBounds(400,400,350,300);
a.setVisible(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

0 comments :