Friday, 14 June 2013
program in java - check number even or odd with swing
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class myframe1 extends JFrame implements ActionListener
{
JButton b1,b2;
JTextField tx,tx1;
JLabel l1,l2,l3;
Container c;
myframe1()
{
c=getContentPane();
c.setBackground(Color.pink);
c.setLayout(null);
l1=new JLabel("Enter any number");
l1.setBounds(50,50,100,50);
l1.setForeground(Color.red);
c.add(l1);
l3=new JLabel("Application for Even Odd");
l3.setBounds(125,0,225,50);
l3.setForeground(Color.blue);
c.add(l3);
tx=new JTextField();
tx.setBounds(200,50,150,30);
c.add(tx);
l2=new JLabel("Answer");
l2.setBounds(50,150,50,50);
l2.setForeground(Color.red);
c.add(l2);
tx1=new JTextField();
tx1.setBounds(200,150,150,30);
c.add(tx1);
b1=new JButton("Check");
b1.addActionListener(this);
b1.setBounds(100,200,90,50);
c.add(b1);
b2=new JButton("Cancel");
b2.addActionListener(this);
b2.setBounds(250,200,90,50);
c.add(b2);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
int no;
if(str=="Check")
{
no=Integer.parseInt(tx.getText());
if(no%2==0)
{
tx1.setText(no+" is Even");
}
else
{
tx1.setText(no+" is Odd");
}
}
if(str=="Cancel")
{
System.exit(0);
}
}
}
class check
{
public static void main(String args[])
{
myframe1 f=new myframe1();
f.setTitle("Even & Odd");
f.setSize(400,400);
f.setVisible(true);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment