Adv Java - [Swing: JButton Class]

♠ Posted by Unknown in at 01:19

Swing Button Classes


Swing buttons provide features that are not found in the Button class defined by the AWT. For example, you can associate an icon with a Swing button. Swing buttons are subclasses of the AbstractButton class, which extends JComponent. AbstractButton contains many methods that allow you to control the behavior of buttons, check boxes, and radio buttons.
For example, you can define different icons that are displayed for the component when it is disabled, pressed, or selected. Another icon can be used as a rollover icon, which is displayed when the mouse is positioned over the component. The following are the methods that control this behavior:

     void setDisabledIcon(Icon di)
     void setPressedIcon(Icon pi)
     void setSelectedIcon(Icon si)
     void setRolloverIcon(Icon ri)

The text associated with a button can be read and written via the following methods:

     String getText()
     void setText(String s)

Concrete subclasses of AbstractButton generate action events when they are pressed. Listeners register and unregister for these events via the methods shown here:

     void addActionListener(ActionListener al)
     void removeActionListener(ActionListener al)

AbstractButton is a super class for push buttons, check boxes, and radio buttons.


JButton :-



The JButton class provides the functionality of a push button. JButton allows an icon, a string, or both to be associated with push button. Some of its constructors are show here:


     JButton(Icon i)
     JButton(String s)
     JButton(String s, Icon i)

/*
 * <APPLET CODE = "JButtonExample.CLASS" WIDTH = 500 HEIGHT = 500></APPLET>
 */
 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
public class JButtonExample extends JApplet implements ActionListener
{
          JTextField jtf;
          public void init()
          {
                   Container contentPane = getContentPane();
                   contentPane.setLayout(new FlowLayout());
                   JButton jb;
                  
                   ImageIcon jlogo = new ImageIcon("C:\\PracticeJava\\javalogo.gif");
                   jb = new JButton(jlogo);
                   jb.setActionCommand("Java Logo");
                   jb.addActionListener(this);
Image: Swing JButton Example                   contentPane.add(jb);
                  
                   ImageIcon rshade = new ImageIcon("C:\\PracticeJava\\redshd.gif");
                   jb = new JButton(rshade);
                   jb.setActionCommand("Red Shade");
                   jb.addActionListener(this);
                   contentPane.add(jb);

                   ImageIcon gshade = new ImageIcon("C:\\PracticeJava\\greenshd.gif");
                   jb = new JButton(gshade);
                   jb.setActionCommand("Green Shade");
                   jb.addActionListener(this);
                   contentPane.add(jb);

                   jtf = new JTextField(15);
                   contentPane.add(jtf);
          }
         
          public void actionPerformed(ActionEvent ae)
          {
                   jtf.setText(ae.getActionCommand());
          }
}


0 comments:

Post a Comment