Friday, 1 January 2016

Make a coloured button in java

To make a coloured button in java use following code

Code:

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

public class myGreenButton
{

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

   }
}

Output:


How to implement a TextField in java

To implement text field use following code

Code:

import javax.swing.*;
import java.awt.*;
public class JTextClass

{
  public static void main(String[] args)
  {
     JFrame f1;

     f1 = new JFrame("Welcome");

     JLabel l1 = new JLabel("Name");

     JTextField tf1 = new JTextField(20);

     JButton b1 = new JButton("Submit");
 
     f1.add(l1);
     f1.add(tf1);
     f1.add(b1);
     f1.setLayout(new FlowLayout(FlowLayout.LEFT));
     f1.setVisible(true);
     f1.setSize(400,100);
     f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f1.setLocation(200,200);

    }
 }

Output:


How to implement JCheckbox to make check boxes in Java.

. To make check boxes in java use following code

Code:

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

public class JCheckDemo
{

   public static void main(String[] args)
   {

     JFrame f1;

     f1 = new JFrame("Welcome");

      JLabel l1 = new JLabel("Choose different one.");

     JCheckBox cb1 = new JCheckBox("Apple");
     JCheckBox cb2 = new JCheckBox("Mango");
     JCheckBox cb3 = new JCheckBox("Chicken");

    f1.add(l1);
    f1.add(cb1);
    f1.add(cb2);
    f1.add(cb3);
     f1.setLayout(new FlowLayout(FlowLayout.LEFT));
     f1.setVisible(true);
     f1.setSize(400,100);
     f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    }
 }

Output:


How to display a JFrame with button

To display a JFrame with JButton you must use following code.

Code:

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

public class myButton
{

  public static void main(String[] args)
  {
    JFrame f1 = new JFrame("Welcome");
    JButton b1 = new JButton("OK");

    f1.add(b1);
    f1.setVisible(true);
    f1.setSize(200,200);
    f1.setLayout(new FlowLayout(FlowLayout.LEFT));

   }
}

output