JFrame find odd/even
//JFrame class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyJFrame extends JFrame implements ActionListener
{
Container con;
JLabel lbl1;
JTextField txt1;
JButton btn1;
MyJFrame(String title)
{
super(title);
con = new Container();
con.setLayout(new FlowLayout());
lbl1 = new JLabel("Number");
con.add(lbl1);
txt1 = new JTextField(10);
con.add(txt1);
btn1 = new JButton("Odd/Even");
btn1.addActionListener(this);
con.add(btn1);
add(con);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
int no = Integer.parseInt(txt1.getText());
if(no%2 == 0)
JOptionPane.showMessageDialog(this, "Even Number");
else
JOptionPane.showMessageDialog(this, "Odd Number");
}
public static void main(String[] args) {
MyJFrame obj = new MyJFrame("Odd Even");
obj.setSize(200, 200);
obj.setVisible(true);
}
}
OUTPUT:
Comments
Post a Comment