Tuesday, April 12, 2016

How to use RadioButtons? ( in java GUI)

//In this program, we will make three radio buttons namely Bold, Italic and Normal and make the text in the textField change with the button check.





import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class SimpleAnimation extends JFrame{
private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;
private JTextField text;
private Font boldFont;
private Font italicFont;
private Font normalFont;
private ButtonGroup group;
//create a constructor 
public SimpleAnimation(){
super("Magic");               //This class is a subclass of JFrame
setLayout(new FlowLayout());
//cretes text field and writes something
text = new JTextField("Are you learning something?.");
add(text);
//create buttons and add them to the frame
button1 = new JRadioButton("Bold");
button2 = new JRadioButton("Italic");
button3 = new JRadioButton("Normal");
add(button1);
add(button2);
add(button3);
//define font
boldFont = new Font("Serif",Font.BOLD,14);
italicFont = new Font("Serif",Font.ITALIC,14);
normalFont = new Font("Serif",Font.PLAIN,14);
//radio button group
group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
/*add itemListener (Pay attention to ItemListener here)
  * We have created a constructor of the inner class that takes
  *Font object as parameter.Why are we doing so?         */

button1.addItemListener(new ButtonActivity(boldFont));
button2.addItemListener(new ButtonActivity(italicFont));
button3.addItemListener(new ButtonActivity(normalFont));
}
//Inner class that implements ItemListener
private class ButtonActivity implements ItemListener{
private Font font;
public ButtonActivity(Font f){
font = f;
}
public void itemStateChanged(ItemEvent e){
text.setFont(font);
}
}

}





//Diver method or main method



import javax.swing.JFrame;
import javax.swing.JPanel;

public class SimpleAnimationTest {
public static void main(String[]args){
SimpleAnimation frame = new SimpleAnimation();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 200);
frame.setVisible(true);
}

}




Output:







Simple GUI to count up/down button click

PROBLEM:  Make a counter button in java


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;

import javax.swing.*;

public class UpDownPanel implements ActionListener {
private JFrame frame;
private JButton button1;
private JButton button2;
private JLabel label;
private JPanel panel;
private JPanel panel1;
private int count =50;
public void GuiAnimation(){
frame = new JFrame();
button1 = new JButton("Up");
button2 = new JButton("Down");
panel = new JPanel();
panel1 = new JPanel();
panel.add(button1);
panel.add(button2);
panel.setBackground(Color.darkGray);
label = new JLabel("Click:"+count);
panel1.add(label);
panel1.setBackground(Color.white);
button1.addActionListener(this);
button2.addActionListener(this);
frame.getContentPane().add(panel,BorderLayout.SOUTH);
frame.getContentPane().add(panel1, BorderLayout.NORTH);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==button1){
label.setText("Click:"+ ++count);
}else{
label.setText("Click:"+ --count);
}
}

}


//Main class or a driver class

public class UpDown {
public static void main(String[]args){
UpDownPanel upDown = new UpDownPanel();
upDown.GuiAnimation();
}

}




Output:









How to generate odd numbers ( in java )





Print all odd numbers from 1 to 100.



public class OddNumbers{
    private int num;
public static void main(String[] args){
OddNumbers myNumber = new OddNumbers();
myNumber.odd();
}
public void odd(){
  for(num = 1;num <=100;num++){
  if(num%2!=0)
    System.out.printf("%3d",num);
}
  }
}

output:
1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99


Exercise: Print all even numbers between 1 and 100.

Array Example in Java


How to print all lowerCase letters using array in java?


public class ArrayListExample {
public static void main(String[]args){
char [] a = new char[26];
for(int i =0;i<a.length;i++){
a[i] = (char)(i+'a');
System.out.printf("%2s",a[i]);
}
}

}

Output :
a b c d e f g h i j k l m n o p q r s t u v w x y z

Question: Now print all UpperCase letters using the same procedure.


Binary Search Example in java

import java.util.Scanner;

public class BinarySearch {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
//initialize an array of type int that stores the value of prime no.
int [] prime = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int max = prime.length;
int min = 0;
int count=0;
System.out.println("Enter the target no (0,100) make sure the no is prime.");
int target = input.nextInt();
input.close();
int guess = (max+min)/2;
while(target!=prime[guess]){
  if(prime[guess]>target){
  max = guess;
  guess = (max+min)/2;
 
  }
  else{
  min=guess
  guess = (max+min)/2;
 
}
  count++;
}
if(target==prime[guess])System.out.println("The prime no is :"+prime[guess]+" and the position is :"+guess);
System.out.println("no of guesses :"+count);
}


}