Sunday, December 29, 2013

JTabbedPane Java Example



import java.awt.*;
import javax.swing.*; 
import java.awt.event.*;
public class TAP1 extends JFrame implements ActionListener
{
        JLabel jlrad,jlare,jlcir,jls,jlp,jla;
        JTextField jtrad,jtare,jtcir,jts,jtp,jta;
        JButton jbcal1,jbexit1,jbcal2,jbexit2;
        JPanel circle,square,jpmain;
        TAP1()
        {       super("Tabbed Panes");
                setSize(200,200);
                jlrad=new JLabel("radius");
                jlare=new JLabel("area");
                jlcir=new JLabel("circunference");

                jtrad=new JTextField(10);
                jtare=new JTextField(10);
                jtcir=new JTextField(10);

                jbcal1=new JButton("Calculate");
                jbcal2=new JButton("Calculate");
                jbexit1=new JButton("Exit");
                jbexit2=new JButton("Exit");       

                jls=new JLabel("Side");
                jlp=new JLabel("Parameter");
                jla=new JLabel("Area");

                jts=new JTextField(10);
                jtp=new JTextField(10);
                jta=new JTextField(10);

                circle=new JPanel();
                square =new JPanel();
                jpmain=new JPanel();
                circle.setLayout(new GridLayout(4,2));
                square.setLayout(new GridLayout(4,2));

                circle.add(jlrad);circle.add(jtrad);
                circle.add(jlare);circle.add(jtare);
                circle.add(jlcir);circle.add(jtcir);
                circle.add(jbcal1);circle.add(jbexit1);

                square.add(jls);square.add(jts);
                square.add(jlp);square.add(jtp);
                square.add(jla);square.add(jta);
                square.add(jbcal2);square.add(jbexit2);

                jbcal1.addActionListener(this);
                jbexit1.addActionListener(this);
                jbcal2.addActionListener(this);
                jbexit2.addActionListener(this);
                JTabbedPane tab=new JTabbedPane();
                tab.addTab("Circle",circle);
                tab.addTab("Square",square);
                jpmain.setLayout(new BorderLayout());
                jpmain.add(tab);
                jpmain.setSize(100,100);
                setContentPane(jpmain);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);

        }
        public void actionPerformed(ActionEvent e)
        {       double temp1,area,peri;
                JButton sou=new JButton();
                sou=(JButton)(e.getSource());
                if(sou==jbexit1 || sou==jbexit2)
                {
                        System.exit(0);
                }
                else
                        if(sou==jbcal1)
                        {
                                temp1=Double.parseDouble(jtrad.getText());
                                area=3.14*temp1*temp1;
                                jtare.setText(""+area);
                                peri=2*3.14*temp1;
                                jtcir.setText(""+peri);
                        }
                        else
                        {
                                temp1=Double.parseDouble(jts.getText());
                                area=temp1*temp1;
                                jta.setText(""+area);
                                peri=4*temp1;
                                jtp.setText(""+peri);
                        }

        }
        public static void main(String arguments[])
        {
                TAP1 aa=new TAP1();
        }
                     
}

No comments:

Post a Comment