Thursday, September 26, 2013

Exercise no. 40

package lord1;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class positivenegativeoe extends JFrame implements ActionListener
{
    private Container con=getContentPane();
    JButton ok;
    JLabel number;
    JTextField fieldnumber;
    JPanel one,two,main;
  
  
    public void actionPerformed(ActionEvent e)
    {
      
        try{
        Double x=(double) 0;
      
        if(e.getSource().equals(ok))
        {
            x=Double.parseDouble(fieldnumber.getText());
          
          
          
            if(x>0)
            {
                //positive
                if(x%2==0)
                {
                    JOptionPane.showMessageDialog(main," EVEN-POSITIVE");
                    fieldnumber.setText("");
                  
                }
                else
                {
                    JOptionPane.showMessageDialog(main," ODD-POSITIVE");
                    fieldnumber.setText("");
                }
            }
            else
            {
                //negative
                if(x%2==0)
                {
                    JOptionPane.showMessageDialog(main," EVEN-NEGATIVE");
                    fieldnumber.setText("");
                }
                else
                {
                    JOptionPane.showMessageDialog(main," ODD-NEGATIVE");
                    fieldnumber.setText("");
                }
            }
          
        }
        }catch(Exception f)
        {
            JOptionPane.showMessageDialog(main,"Please try again");
            fieldnumber.setText("");
        }
    }
  
    public positivenegativeoe()
    {
        ok=new JButton("OK");
        ok.setBackground(Color.gray);
        ok.setForeground(Color.WHITE);
        number=new JLabel("Enter number :");
        fieldnumber=new JTextField(10);
      
    ok.addActionListener(this);
      
        one=new JPanel(new GridLayout(1,2));
        one.add(number);
      
        two=new JPanel(new GridLayout(1,2));
        two.add(fieldnumber);
        two.add(ok);
      
        main=new JPanel(new GridLayout(2,1));
        main.add(one);
        main.add(two);
      
      
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    con.setBackground(Color.green);
    con.add(main);
    setSize(300,150);
    setVisible(true);  
    }
  
public static void main(String[]args)
{
    positivenegativeoe out=new positivenegativeoe();
    out.setLocationRelativeTo(null);
}
}

No comments:

Post a Comment