Java Tutorial/Swing/AbstractButton
Содержание
AbstractButton: parent class for JButton
<source lang="java">
AbstractButton
| +--JButton, JMenuItem, JToggleButton</source>
AbstractButton Positioning
Position PropertyAvailable SettingshorizontalAlignmentLEFT, CENTER, RIGHThorizontalTextPositionLEFT, CENTER, RIGHTverticalAlignmentTOP, CENTER, BOTTOMverticalTextPositionTOP, CENTER, BOTTOM
Adding PropertyChangeListener to AbstractButton
<source lang="java">
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); }
}</source>
Constants for AbstractButton PropertyChangeListener
- BORDER_PAINTED_CHANGED_PROPERTY
- CONTENT_AREA_FILLED_CHANGED_PROPERTY
- DISABLED_ICON_CHANGED_PROPERTY
- DISABLED_SELECTED_ICON_CHANGED_PROPERTY
- FOCUS_PAINTED_CHANGED_PROPERTY
- HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY
- HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY
- ICON_CHANGED_PROPERTY
- MARGIN_CHANGED_PROPERTY
- MNEMONIC_CHANGED_PROPERTY
- MODEL_CHANGED_PROPERTY
- PRESSED_ICON_CHANGED_PROPERTY
- ROLLOVER_ENABLED_CHANGED_PROPERTY
- ROLLOVER_ICON_CHANGED_PROPERTY
- ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY
- SELECTED_ICON_CHANGED_PROPERTY
- TEXT_CHANGED_PROPERTY
- VERTICAL_ALIGNMENT_CHANGED_PROPERTY
- VERTICAL_TEXT_POSITION_CHANGED_PROPERTY
Supporting HTML text
<source lang="java">
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
(mandatory) </html>"); frame.add(bn); frame.setSize(300, 200); frame.setVisible(true); }
}</source>