Showing posts with label swings. Show all posts
Showing posts with label swings. Show all posts

Thursday, 3 August 2017

Number reverse program using swings

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

class StringReverse1 extends JFrame implements ActionListener
{
    JButton b1,b2;
    JTextField tx,tx1;
    JLabel l1,l2,l3;
    Container c;
    StringReverse1()
    {
        c=getContentPane();
        c.setBackground(Color.black);
        c.setLayout(null);

        l1=new JLabel("Enter a number");
        l1.setBounds(50,50,100,50);
        l1.setForeground(Color.white);
        c.add(l1);

        l3=new JLabel("after Reverse");
        l3.setBounds(125,0,225,50);
        l3.setForeground(Color.white);
        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.white);
        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,rev=0,rem;
        if(str=="Check")
        {
            no=Integer.parseInt(tx.getText());
            while(no!=0)
            {
                rem=no%10;
                rev=rev*10+rem;
                no=no/10;
            }
            tx1.setText(String.valueOf(rev));
        }
        if(str=="Cancel")
        {
            System.exit(0);
        }
    }

}
class StringReverse
{
    public static void main(String args[])
    {
        StringReverse1 f=new StringReverse1();

        f.setTitle("Reverse");
        f.setSize(400,400);
        f.setVisible(true);
    }
}




OUTPUT:





Wednesday, 2 August 2017

Factorial program using swings

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

class Factorial extends JFrame {
JTextField t1, t2;

Factorial() {
JLabel l1 = new JLabel("Enter Number: ");
JLabel l2 = new JLabel("Factorial of Input Number: ");
t1 = new JTextField(20);
t2 = new JTextField(20);
JPanel p = new JPanel(new GridLayout(3, 2));
JButton b = new JButton("Find");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number = t1.getText();
int num = Integer.parseInt(number);
long fac = num;
for (int i = num; i > 1; i--) {
fac = fac * (i - 1);
}
t2.setText(Long.toString(fac));

}
});
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(b);
add(p);
setVisible(true);
pack();
}

public static void main(String[] args) {
Factorial f = new Factorial();
}
}


OUTPUT: