INTRODUCTION OF JBUTTON

Introduction of JButton


JButton Constructor

    JButton()

    Creates a button with no set text or icon.

    JButton(Action a)

    Creates a button where properties are taken from the Action supplied.

    JButton(Icon icon)

    Creates a button with an icon.

    JButton(String text)

    Creates a button with text.

    JButton(String text, Icon icon)

    Creates a button with initial text and an icon.

Example:JButton Constructor.

 
            
                package com.swing.sdj;
                import java.awt.BorderLayout;
                import java.awt.FlowLayout;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import java.awt.event.KeyEvent;
                import javax.swing.JButton;
                import javax.swing.JFrame;
                import javax.swing.JPanel;
                import javax.swing.JTextField;
                public class JButtonDemo2
                {
                    JFrame jtfMainFrame;
                    JButton jbnButton1, jbnButton2;
                    JTextField jtfInput;
                    JPanel jplPanel;
                    public JButtonDemo2()
                    {
                        jtfMainFrame = new JFrame("Which Button Demo");
                        jtfMainFrame.setSize(50, 50);
                        jbnButton1 = new JButton("SDJ");
                        jbnButton2 = new JButton("JAVA");
                        jtfInput = new JTextField(20);
                        jplPanel = new JPanel();
                        jbnButton1.setMnemonic(KeyEvent.VK_I); //Set ShortCut Keys
                        jbnButton1.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e)
                            {
                                jtfInput.setText("SDJ INFOSOFT");
                            }
                        });
                        jbnButton2.setMnemonic(KeyEvent.VK_I);
                        jbnButton2.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e)
                            {
                                jtfInput.setText("J2SE J2EE STRUTS HIBERNATE!");
                            }
                        });
                        jplPanel.setLayout(new FlowLayout());
                        jplPanel.add(jtfInput);
                        jplPanel.add(jbnButton1);
                        jplPanel.add(jbnButton2);
                        jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
                        jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        jtfMainFrame.pack();
                        jtfMainFrame.setVisible(true);
                    }
                public static void main(String[] args)
                {
                    JButtonDemo2 application = new JButtonDemo2();
                } 
                

Output:

Naming Convention
SDJ Button Demo

    Enter the SDJ INFOSOFT text

Naming Convention
SDJ Button Demo