Java by API/javax.swing/JButton — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:21, 31 мая 2010
Содержание
- 1 Button.background
- 2 Button.font
- 3 Button.foreground
- 4 extends JButton
- 5 JButton: addActionListener(ActionListener act)
- 6 JButton: addAncestorListener(AncestorListener listener)
- 7 JButton: addChangeListener(ChangeListener l)
- 8 JButton: addItemListener(ItemListener l)
- 9 JButton: addPropertyChangeListener(PropertyChangeListener lis)
- 10 JButton: contains(int x, int y)
- 11 JButton: createToolTip()
- 12 JButton: getAccessibleContext()
- 13 JButton: getActionMap()
- 14 JButton: getBackground()
- 15 JButton: getInputMap()
- 16 JButton: removeActionListener(ActionListener l)
- 17 JButton: setActionCommand(String text)
- 18 JButton: setAlignmentX(float alignmentX)
- 19 JButton: setAlignmentY(float alignmentY)
- 20 JButton: setBackground(Color c)
- 21 JButton: setBorder(Border border)
- 22 JButton: setBorderPainted(boolean b)
- 23 JButton: setDefaultCapable(boolean defaultCapable)
- 24 JButton: setDisabledIcon(Icon disabledIcon)
- 25 JButton: setDisabledSelectedIcon(Icon disabledSelectedIcon)
- 26 JButton: setHorizontalAlignment(int alignment)
- 27 JButton: setIconTextGap(int iconTextGap)
- 28 JButton: setMnemonic(KeyEvent.VK_A)
- 29 JButton: setPressedIcon(Icon pressedIcon)
- 30 JButton: setRolloverIcon(Icon rolloverIcon)
- 31 JButton: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
- 32 JButton: setSelectedIcon(Icon selectedIcon)
- 33 JButton: setToolTipText(String str)
- 34 JButton: setVerticalAlignment(int alignment)
- 35 new JButton(Action act)
- 36 new JButton(Icon ic)
- 37 new JButton(String str) (HTML String)
- 38 new JButton(String text)
- 39 new JButton(String text, Icon icon)
Button.background
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("UIDefaults Example");
frame.setDefaultCloseOperation(3);
Container c = frame.getContentPane();
UIManager.put("Button.background", Color.red);
UIManager.put("Button.foreground", Color.white);
Font f = new Font("Serif", Font.ITALIC, 24);
UIManager.put("Button.font", f);
JButton north = new JButton("North");
JButton south = new JButton("South");
JButton east = new JButton("East");
JButton west = new JButton("West");
JButton center = new JButton("Center");
c.add(north, BorderLayout.NORTH);
c.add(south, BorderLayout.SOUTH);
c.add(east, BorderLayout.EAST);
c.add(west, BorderLayout.WEST);
c.add(center, BorderLayout.CENTER);
frame.pack();
frame.show();
}
}
Button.font
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("UIDefaults Example");
frame.setDefaultCloseOperation(3);
Container c = frame.getContentPane();
UIManager.put("Button.background", Color.red);
UIManager.put("Button.foreground", Color.white);
Font f = new Font("Serif", Font.ITALIC, 24);
UIManager.put("Button.font", f);
JButton north = new JButton("North");
JButton south = new JButton("South");
JButton east = new JButton("East");
JButton west = new JButton("West");
JButton center = new JButton("Center");
c.add(north, BorderLayout.NORTH);
c.add(south, BorderLayout.SOUTH);
c.add(east, BorderLayout.EAST);
c.add(west, BorderLayout.WEST);
c.add(center, BorderLayout.CENTER);
frame.pack();
frame.show();
}
}
Button.foreground
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("UIDefaults Example");
frame.setDefaultCloseOperation(3);
Container c = frame.getContentPane();
UIManager.put("Button.background", Color.red);
UIManager.put("Button.foreground", Color.white);
Font f = new Font("Serif", Font.ITALIC, 24);
UIManager.put("Button.font", f);
JButton north = new JButton("North");
JButton south = new JButton("South");
JButton east = new JButton("East");
JButton west = new JButton("West");
JButton center = new JButton("Center");
c.add(north, BorderLayout.NORTH);
c.add(south, BorderLayout.SOUTH);
c.add(east, BorderLayout.EAST);
c.add(west, BorderLayout.WEST);
c.add(center, BorderLayout.CENTER);
frame.pack();
frame.show();
}
}
extends JButton
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: addActionListener(ActionListener act)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public MainClass() {
JButton btn1 = new JButton("Button1");
btn1.addActionListener(new ButtonListener());
add(btn1);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class ButtonListener implements ActionListener {
ButtonListener() {
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Button1")) {
System.out.println("Button1 has been clicked");
}
}
}
JButton: addAncestorListener(AncestorListener listener)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class Main {
public static void main(String args[]) {
final JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
JButton b = new JButton("Hide for 5");
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
frame.setVisible(false);
TimerTask task = new TimerTask() {
public void run() {
frame.setVisible(true);
}
};
Timer timer = new Timer();
timer.schedule(task, 5000);
}
};
b.addActionListener(action);
AncestorListener ancestor = new AncestorListener() {
public void ancestorAdded(AncestorEvent e) {
System.out.println("Added");
dumpInfo(e);
}
public void ancestorMoved(AncestorEvent e) {
System.out.println("Moved");
dumpInfo(e);
}
public void ancestorRemoved(AncestorEvent e) {
System.out.println("Removed");
dumpInfo(e);
}
private void dumpInfo(AncestorEvent e) {
System.out.println("\tAncestor: " + name(e.getAncestor()));
System.out.println("\tAncestorParent: "
+ name(e.getAncestorParent()));
System.out.println("\tComponent: " + name(e.getComponent()));
}
private String name(Container c) {
return (c == null) ? null : c.getName();
}
};
b.addAncestorListener(ancestor);
contentPane.add(b, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JButton: addChangeListener(ChangeListener l)
/*
*/
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] args) {
JButton jb = new JButton("Press Me");
jb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
System.out.println("ItemEvent!");
}
});
jb.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ev) {
System.out.println("ChangeEvent!");
}
});
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(jb);
f.pack();
f.setVisible(true);
}
}
JButton: addItemListener(ItemListener l)
/*
*/
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] args) {
JButton jb = new JButton("Press Me");
jb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
System.out.println("ItemEvent!");
}
});
jb.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ev) {
System.out.println("ChangeEvent!");
}
});
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(jb);
f.pack();
f.setVisible(true);
}
}
JButton: addPropertyChangeListener(PropertyChangeListener lis)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Button Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button1 = new JButton("Select Me");
final JButton button2 = new JButton("No Select Me");
final Random random = new Random();
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
JButton button = (JButton) actionEvent.getSource();
int red = random.nextInt(255);
int green = random.nextInt(255);
int blue = random.nextInt(255);
button.setBackground(new Color(red, green, blue));
}
};
PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
String property = propertyChangeEvent.getPropertyName();
if ("background".equals(property)) {
button2.setBackground((Color) propertyChangeEvent.getNewValue());
}
}
};
button1.addActionListener(actionListener);
button1.addPropertyChangeListener(propertyChangeListener);
button2.addActionListener(actionListener);
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
JButton: contains(int x, int y)
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolTip;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Tooltip");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setToolTipText("<HtMl>Tooltip<br>Message");
frame.add(panel, BorderLayout.CENTER);
JButton button = new JButton("Hello, World") {
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
public boolean contains(int x, int y) {
if (x < 100) {
setToolTipText("x < 100");
} else {
setToolTipText("else");
}
return super.contains(x, y);
}
};
button.setToolTipText("Hello, World");
frame.add(button, BorderLayout.NORTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JButton: createToolTip()
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolTip;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Tooltip");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setToolTipText("<HtMl>Tooltip<br>Message");
frame.add(panel, BorderLayout.CENTER);
JButton button = new JButton("Hello, World") {
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
public boolean contains(int x, int y) {
if (x < 100) {
setToolTipText("x < 100");
} else {
setToolTipText("else");
}
return super.contains(x, y);
}
};
button.setToolTipText("Hello, World");
frame.add(button, BorderLayout.NORTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JButton: getAccessibleContext()
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Main {
public static void main(String[] argv) throws Exception {
JButton button = new JButton(new ImageIcon("image.gif"));
button.setToolTipText("Button Name");
button.getAccessibleContext().setAccessibleName("Button Name");
}
}
JButton: getActionMap()
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class MainClass {
public static void main(String args[]) {
String ACTION_KEY = "The Action";
JFrame frame = new JFrame("KeyStroke Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonA = new JButton("FOCUSED (control alt 7)");
JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)");
JButton buttonD = new JButton("WINDOW (" ")");
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
}
};
KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = buttonA.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = buttonA.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
inputMap = buttonB.getInputMap();
inputMap.put(enter, ACTION_KEY);
buttonB.setActionMap(actionMap);
KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4,
InputEvent.SHIFT_MASK);
inputMap = buttonC
.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(shiftF4, ACTION_KEY);
buttonC.setActionMap(actionMap);
KeyStroke space = KeyStroke.getKeyStroke(" ");
inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(space, ACTION_KEY);
buttonD.setActionMap(actionMap);
frame.setLayout(new GridLayout(2, 2));
frame.add(buttonA);
frame.add(buttonB);
frame.add(buttonC);
frame.add(buttonD);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
JButton: getBackground()
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JColorChooser Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton("Pick to Change Background");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Color initialBackground = button.getBackground();
Color background = JColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
if (background != null) {
button.setBackground(background);
}
}
};
button.addActionListener(actionListener);
f.add(button, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
JButton: getInputMap()
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class MainClass {
public static void main(String args[]) {
String ACTION_KEY = "The Action";
JFrame frame = new JFrame("KeyStroke Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonA = new JButton("FOCUSED (control alt 7)");
JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)");
JButton buttonD = new JButton("WINDOW (" ")");
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
}
};
KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = buttonA.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = buttonA.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
inputMap = buttonB.getInputMap();
inputMap.put(enter, ACTION_KEY);
buttonB.setActionMap(actionMap);
KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4,
InputEvent.SHIFT_MASK);
inputMap = buttonC
.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(shiftF4, ACTION_KEY);
buttonC.setActionMap(actionMap);
KeyStroke space = KeyStroke.getKeyStroke(" ");
inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(space, ACTION_KEY);
buttonD.setActionMap(actionMap);
frame.setLayout(new GridLayout(2, 2));
frame.add(buttonA);
frame.add(buttonB);
frame.add(buttonC);
frame.add(buttonD);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
JButton: removeActionListener(ActionListener l)
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002.
* All rights reserved. Software written by Ian F. Darwin and others.
* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS""
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee
* cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s,
* pioneering role in inventing and promulgating (and standardizing) the Java
* language and environment is gratefully acknowledged.
*
* The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
* inventing predecessor languages C and C++ is also gratefully acknowledged.
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Demo to show a way of having "More Choices/Less Choices" in a pop-up window.
* The secret is to call pack() again each time you add/subtract the bottom
* panel.
*
* @author Ian F. Darwin, http://www.darwinsys.ru/
* @version $Id: MoreChoices.java,v 1.3 2004/02/09 03:33:50 ian Exp $
*/
public class Main extends JFrame {
Container cp;
boolean unsavedChanges = false;
JButton moreOrLessButton;
JPanel moreOrLessPanel;
ActionListener more;
ActionListener less;
/** "main program" method - construct and show */
public static void main(String[] av) {
// create a MoreChoices object, tell it to show up
JFrame jf = new Main();
jf.setLocation(100, 100); // get away from screen corner,
// since on some OSes a main window at 0,0 may be
// partly obscured (e.g. notebook with PowerPanel
jf.setVisible(true);
}
/** Construct the object including its GUI */
public Main() {
super("More Choices");
// cp = getContentPane();
cp = this;
cp.setLayout(new BorderLayout());
ButtonsPanel bp = new ButtonsPanel();
cp.add(BorderLayout.NORTH, bp);
// Construct the more/less switcher
less = new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.remove(moreOrLessPanel);
pack();
moreOrLessButton.setText("More Choices");
moreOrLessButton.removeActionListener(less);
moreOrLessButton.addActionListener(more);
}
};
more = new ActionListener() {
public void actionPerformed(ActionEvent e) {
cp.add(BorderLayout.SOUTH, moreOrLessPanel);
pack();
moreOrLessButton.setText("Fewer Choices");
moreOrLessButton.removeActionListener(more);
moreOrLessButton.addActionListener(less);
}
};
bp.add(moreOrLessButton = new JButton("More Choices"));
// Initial state is to add more choices
moreOrLessButton.addActionListener(more);
moreOrLessPanel = new ChoicesPanel();
// Finally a frame closer
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
});
pack();
}
/**
* The panel that appears when you press More Choices. This is a toy; in a
* real application, this would likely be a separate full "public class".
*/
class ChoicesPanel extends JPanel {
ChoicesPanel() {
setBorder(BorderFactory.createEtchedBorder());
add(new JCheckBox("Happiness"));
add(new JCheckBox("Satisfaction"));
add(new JCheckBox("Contentment"));
}
}
/**
* The Panel that contains the More/Less button. It is just here to override
* getPreferredSize so that we can avoid "jitter" (i.e., the width changing);
* i.e., we must ensure that the main panel and the ChoicePanel have the same
* width
*/
class ButtonsPanel extends JPanel {
public Dimension getPreferredSize() {
// System.out.println("In ButtonsPanel.getPreferredSize()");
// For height, use our normal height
int dHeight = moreOrLessButton.getPreferredSize().height + 5 + 5;
// For witdh, use the included Panel"s width
int dWidth = moreOrLessPanel.getPreferredSize().width;
// Combine them; that"s the result we need.
return new Dimension(dWidth, dHeight);
}
}
}
JButton: setActionCommand(String text)
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ActionListener {
public MainClass() {
JButton jb = new JButton(new MyIcon());
jb.setActionCommand("France");
jb.addActionListener(this);
add(jb);
}
// Show text when user presses ENTER.
public void actionPerformed(ActionEvent ae) {
System.out.println(ae);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.drawString("jexp.ru", 0, 20);
}
}
JButton: setAlignmentX(float alignmentX)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Overlay Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
JButton button = new JButton("Small");
button.setMaximumSize(new Dimension(25, 25));
button.setBackground(Color.white);
button.setAlignmentX(0.0f);
button.setAlignmentY(0.0f);
panel.add(button);
button = new JButton("Medium");
button.setMaximumSize(new Dimension(50, 50));
button.setBackground(Color.gray);
button.setAlignmentX(0.0f);
button.setAlignmentY(0.0f);
panel.add(button);
button = new JButton("Large");
button.setMaximumSize(new Dimension(100, 100));
button.setBackground(Color.black);
button.setAlignmentX(0.0f);
button.setAlignmentY(0.0f);
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
JButton: setAlignmentY(float alignmentY)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Overlay Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
JButton button = new JButton("Small");
button.setMaximumSize(new Dimension(25, 25));
button.setBackground(Color.white);
button.setAlignmentX(0.0f);
button.setAlignmentY(0.0f);
panel.add(button);
button = new JButton("Medium");
button.setMaximumSize(new Dimension(50, 50));
button.setBackground(Color.gray);
button.setAlignmentX(0.0f);
button.setAlignmentY(0.0f);
panel.add(button);
button = new JButton("Large");
button.setMaximumSize(new Dimension(100, 100));
button.setBackground(Color.black);
button.setAlignmentX(0.0f);
button.setAlignmentY(0.0f);
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
JButton: setBackground(Color c)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JColorChooser Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton("Pick to Change Background");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Color initialBackground = button.getBackground();
Color background = JColorChooser.showDialog(null,
"JColorChooser Sample", initialBackground);
if (background != null) {
button.setBackground(background);
}
}
};
button.addActionListener(actionListener);
f.add(button, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
JButton: setBorder(Border border)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("Line Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border thickBorder = new LineBorder(Color.WHITE, 12);
JButton thickButton = new JButton("12 Pixel");
thickButton.setBorder(thickBorder);
Container contentPane = frame.getContentPane();
contentPane.add(thickButton, BorderLayout.NORTH);
frame.pack();
frame.setSize(300, frame.getHeight());
frame.setVisible(true);
}
}
JButton: setBorderPainted(boolean b)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setDefaultCapable(boolean defaultCapable)
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
public class MainClass extends JFrame {
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb = new JButton("Ok", new ImageIcon("bullet.gif"));
jb.setHorizontalAlignment(SwingConstants.LEFT);
jb.setMnemonic("O");
getContentPane().add(jb, BorderLayout.CENTER);
jb = new JButton("<html><i>Cancel</i></html>");
jb.setVerticalAlignment(SwingConstants.BOTTOM);
jb.setDefaultCapable(true);
getContentPane().add(jb, BorderLayout.EAST);
getRootPane().setDefaultButton(jb);
setSize(200, 100);
setVisible(true);
}
public static void main(String[] args) {
new MainClass("Button Demo");
}
}
JButton: setDisabledIcon(Icon disabledIcon)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setDisabledSelectedIcon(Icon disabledSelectedIcon)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setHorizontalAlignment(int alignment)
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
public class MainClass extends JFrame {
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb = new JButton("Ok", new ImageIcon("bullet.gif"));
jb.setHorizontalAlignment(SwingConstants.LEFT);
jb.setMnemonic("O");
getContentPane().add(jb, BorderLayout.CENTER);
jb = new JButton("<html><i>Cancel</i></html>");
jb.setVerticalAlignment(SwingConstants.BOTTOM);
jb.setDefaultCapable(true);
getContentPane().add(jb, BorderLayout.EAST);
getRootPane().setDefaultButton(jb);
setSize(200, 100);
setVisible(true);
}
public static void main(String[] args) {
new MainClass("Button Demo");
}
}
JButton: setIconTextGap(int iconTextGap)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setMnemonic(KeyEvent.VK_A)
import java.awt.BorderLayout;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Mnemonic/Accelerator Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Action printAction = new PrintHelloAction();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Print");
KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P,
InputEvent.CTRL_MASK);
menuItem.setAccelerator(ctrlP);
menuItem.addActionListener(printAction);
menu.add(menuItem);
JButton fileButton = new JButton("About");
fileButton.setMnemonic(KeyEvent.VK_A);
fileButton.addActionListener(printAction);
frame.setJMenuBar(menuBar);
frame.add(fileButton, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
class PrintHelloAction extends AbstractAction {
private static final Icon printIcon = new MyIcon();
PrintHelloAction() {
super("Print", printIcon);
putValue(Action.SHORT_DESCRIPTION, "Hello, World");
}
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Hello, World");
}
}
class MyIcon implements Icon {
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.drawString("jexp.ru", 0, 20);
}
}
JButton: setPressedIcon(Icon pressedIcon)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setRolloverIcon(Icon rolloverIcon)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setSelectedIcon(Icon selectedIcon)
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageTest {
public static void main(String[] args) {
JPanel panel = new JPanel();
final ImageButton button = new ImageButton("button.png");
button.setPressedIcon(new ImageIcon("down.png"));
button.setRolloverIcon(new ImageIcon("over.png"));
button.setSelectedIcon(new ImageIcon("sel.png"));
button.setRolloverSelectedIcon(new ImageIcon("sel-over.png"));
button.setDisabledIcon(new ImageIcon("disabled.png"));
button.setDisabledSelectedIcon(new ImageIcon("disabled-selected.png"));
button.setLocation(60, 74);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setSelected(!button.isSelected());
System.out.println("selecting");
}
});
// button.setSelected(true);
// button.setDisabled(false);
panel.add(button);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class ImageButton extends JButton {
public ImageButton(String img) {
this(new ImageIcon(img));
}
public ImageButton(ImageIcon icon) {
setIcon(icon);
setMargin(new Insets(0, 0, 0, 0));
setIconTextGap(0);
setBorderPainted(false);
setBorder(null);
setText(null);
setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
}
}
JButton: setToolTipText(String str)
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolTip;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Tooltip");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setToolTipText("<HtMl>Tooltip<br>Message");
frame.add(panel, BorderLayout.CENTER);
JButton button = new JButton("Hello, World") {
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
public boolean contains(int x, int y) {
if (x < 100) {
setToolTipText("x < 100");
} else {
setToolTipText("else");
}
return super.contains(x, y);
}
};
button.setToolTipText("Hello, World");
frame.add(button, BorderLayout.NORTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JButton: setVerticalAlignment(int alignment)
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
public class MainClass extends JFrame {
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb = new JButton("Ok", new ImageIcon("bullet.gif"));
jb.setHorizontalAlignment(SwingConstants.LEFT);
jb.setMnemonic("O");
getContentPane().add(jb, BorderLayout.CENTER);
jb = new JButton("<html><i>Cancel</i></html>");
jb.setVerticalAlignment(SwingConstants.BOTTOM);
jb.setDefaultCapable(true);
getContentPane().add(jb, BorderLayout.EAST);
getRootPane().setDefaultButton(jb);
setSize(200, 100);
setVisible(true);
}
public static void main(String[] args) {
new MainClass("Button Demo");
}
}
new JButton(Action act)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Action Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Action printAction = new PrintHelloAction();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
menu.add(new JMenuItem(printAction));
JToolBar toolbar = new JToolBar();
toolbar.add(new JButton(printAction));
JButton enableButton = new JButton("Enable");
ActionListener enableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(true);
}
};
enableButton.addActionListener(enableActionListener);
JButton disableButton = new JButton("Disable");
ActionListener disableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(false);
}
};
disableButton.addActionListener(disableActionListener);
JButton relabelButton = new JButton("Relabel");
ActionListener relabelActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.putValue(Action.NAME, "Hello, World");
}
};
relabelButton.addActionListener(relabelActionListener);
JPanel buttonPanel = new JPanel();
buttonPanel.add(enableButton);
buttonPanel.add(disableButton);
buttonPanel.add(relabelButton);
frame.setJMenuBar(menuBar);
frame.add(toolbar, BorderLayout.SOUTH);
frame.add(buttonPanel, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class PrintHelloAction extends AbstractAction {
PrintHelloAction() {
super("Print");
putValue(Action.SHORT_DESCRIPTION, "Hello, World");
}
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Hello, World");
}
}
new JButton(Icon ic)
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ActionListener {
public MainClass() {
JButton jb = new JButton(new MyIcon());
jb.setActionCommand("France");
jb.addActionListener(this);
add(jb);
}
// Show text when user presses ENTER.
public void actionPerformed(ActionEvent ae) {
System.out.println(ae);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.drawString("jexp.ru", 0, 20);
}
}
new JButton(String str) (HTML String)
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
public class MainClass extends JFrame {
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb = new JButton("Ok", new ImageIcon("bullet.gif"));
jb.setHorizontalAlignment(SwingConstants.LEFT);
jb.setMnemonic("O");
getContentPane().add(jb, BorderLayout.CENTER);
jb = new JButton("<html><i>Cancel</i></html>");
jb.setVerticalAlignment(SwingConstants.BOTTOM);
jb.setDefaultCapable(true);
getContentPane().add(jb, BorderLayout.EAST);
getRootPane().setDefaultButton(jb);
setSize(200, 100);
setVisible(true);
}
public static void main(String[] args) {
new MainClass("Button Demo");
}
}
new JButton(String text)
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public MainClass() {
JButton btn1 = new JButton("Button1");
JButton btn2 = new JButton("Button2");
JButton btn3 = new JButton("Button3");
JButton btn4 = new JButton("Button4");
JButton btn5 = new JButton("Button5");
JButton btn6 = new JButton("Button6");
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(btn5);
add(btn6);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
new JButton(String text, Icon icon)
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("DefaultButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon warnIcon = new ImageIcon("Warn.gif");
JButton button3 = new JButton("Warning", warnIcon);
frame.add(button3);
frame.setSize(300, 200);
frame.setVisible(true);
}
}