Java Tutorial/Swing/AbstractButton

Материал из Java эксперт
Перейти к: навигация, поиск

AbstractButton: parent class for JButton

AbstractButton
      |
      +--JButton, JMenuItem, JToggleButton





AbstractButton Positioning

Position PropertyAvailable SettingshorizontalAlignmentLEFT, CENTER, RIGHThorizontalTextPositionLEFT, CENTER, RIGHTverticalAlignmentTOP, CENTER, BOTTOMverticalTextPositionTOP, CENTER, BOTTOM


Adding PropertyChangeListener to AbstractButton

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
class AbstractButtonPropertyChangeListener implements PropertyChangeListener {
  public void propertyChange(PropertyChangeEvent e) {
    String propertyName = e.getPropertyName();
    System.out.println("Property Name: "+propertyName);
    if (e.getPropertyName().equals(AbstractButton.TEXT_CHANGED_PROPERTY)) {
      String newText = (String) e.getNewValue();
      String oldText = (String) e.getOldValue();
      System.out.println(oldText + " changed to " + newText);
    } else if (e.getPropertyName().equals(AbstractButton.ICON_CHANGED_PROPERTY)) {
      Icon icon = (Icon) e.getNewValue();
      if (icon instanceof ImageIcon) {
        System.out.println("New icon is an image");
      }
    }
  }
}
public class AbstractButtonPropertyChangeListenerDemo {
  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AbstractButton bn = new JButton("asdf");
    bn.addPropertyChangeListener(new AbstractButtonPropertyChangeListener());
    bn.setText("fdsa");
    frame.add(bn);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Constants for AbstractButton PropertyChangeListener

  1. BORDER_PAINTED_CHANGED_PROPERTY
  2. CONTENT_AREA_FILLED_CHANGED_PROPERTY
  3. DISABLED_ICON_CHANGED_PROPERTY
  4. DISABLED_SELECTED_ICON_CHANGED_PROPERTY
  5. FOCUS_PAINTED_CHANGED_PROPERTY
  6. HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY
  7. HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY
  8. ICON_CHANGED_PROPERTY
  9. MARGIN_CHANGED_PROPERTY
  10. MNEMONIC_CHANGED_PROPERTY
  11. MODEL_CHANGED_PROPERTY
  12. PRESSED_ICON_CHANGED_PROPERTY
  13. ROLLOVER_ENABLED_CHANGED_PROPERTY
  14. ROLLOVER_ICON_CHANGED_PROPERTY
  15. ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY
  16. SELECTED_ICON_CHANGED_PROPERTY
  17. TEXT_CHANGED_PROPERTY
  18. VERTICAL_ALIGNMENT_CHANGED_PROPERTY
  19. VERTICAL_TEXT_POSITION_CHANGED_PROPERTY


Supporting HTML text

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
public class HTMLDemoAbstractButton {
  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AbstractButton bn = new JButton();
    bn.setText ("<html>Last Name<br><font face="courier new""
        + " color=red> (mandatory) </font></html>");
    frame.add(bn);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}