Saturday, 13 February 2016

How to use JMenuBar class

Program


import javax.swing.*;


public class JMenuBarClass
{
  public static void main(String[] args)
  {
     JFrame f = new JFrame("welcome");


     JMenuBar mb = new JMenuBar();

     JMenu m1 = new JMenu("File");
     JMenuItem mitem1 = new JMenuItem("New");

     m1.add(mitem1);

     JMenu m2 = new JMenu("Edit");
     JMenuItem mitem2 = new JMenuItem("cut");

     m2.add(mitem2);

     mb.add(m1);
     mb.add(m2);

     f.setJMenuBar(mb);
     f.setSize(200,200);
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.setVisible(true);

    }
 }
   
   
   
Output


How to make ImageButton in java

Program


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

public class ImgButton
{
   public static void main(String[] args)
   {

     JFrame f = new JFrame("welcome");
     ImageIcon img = new ImageIcon("roses.jpg");

     JButton b1 = new JButton(img);

    f.add(b1);
    f.setSize(1000,200);
    f.setLayout(new FlowLayout(FlowLayout.CENTER));
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }
}
 

Output


Saturday, 6 February 2016

How to make JSlider in java

To make JSlider

Program

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

public class JSlider_demo
{
   public static void main(String[] args)
   {
 
       JFrame f = new JFrame("welcome");

  JSlider s = new JSlider(JSlider.HORIZONTAL,2,6,3);

  s.setPaintLabels(true);
  s.setPaintTicks(true);
  s.setPaintTrack(true);

  f.add(s);
  f.setLayout(new FlowLayout(FlowLayout.LEFT));

  f.setSize(400,400);
  f.setVisible(true);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

output


Make an image under java JFrame

Program

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

public class Image_demo
{
   public static void main(String[] args)
    {
  JFrame f = new JFrame("welcome");
   ImageIcon img = new ImageIcon("Desert.jpg");

JLabel lab = new JLabel("",img,JLabel.CENTER);
  f.add(lab);

  f.setLayout(new FlowLayout(FlowLayout.CENTER));
  f.pack();
  f.setVisible(true);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setResizable(false);

}
}

output


Tuesday, 2 February 2016

Play a media - media on other player

Program

import java.awt.*;
import java.io.*;

public class Mymedia
{
  public static void main(String[] args)
  {

   try{
    Desktop d = Desktop.getDesktop();

    d.open(new File("E://god//hanuman.mp3"));

    }
    catch(Exception e){}

   }

}


Output

(The file will be played in your default media player)

Make a Label in swing that is JLabel

Program

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

public class IconFrame
{
  public static void main(String[] args)
  {

     JFrame f1;

     f1 = new JFrame("Welcome");

     JLabel l1;

     l1 = new JLabel("I love my college");
     l1.setForeground(Color.GREEN);
     f1.add(l1);
     f1.setLayout(new FlowLayout());
     f1.setVisible(true);
     f1.setSize(200,200);
     f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   

   }
}

Output


Make an enabled and a disabled button

How to make an enabled and a disabled button

Program

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

public class Enable_disable_Button
{

  public static void main(String[] args)
  {
    JFrame f1 = new JFrame("Welcome");
    JButton b1 = new JButton("OK enabled");
    b1.setBackground(Color.GREEN);
    b1.setEnabled(true);
    f1.add(b1);
    JButton b2 = new JButton("OK disabled");
    b2.setBackground(Color.GREEN);
    b2.setEnabled(false);
    f1.add(b2);
    f1.setVisible(true);
    f1.setSize(200,200);
    f1.setLayout(new FlowLayout(FlowLayout.LEFT));
    //f1.setProperty(JFrame.EXIT_ON_CLOSE);
   f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }
}

Output