📄 button2.java
字号:
/*
* @(#)ButtonDemo.java 1.4 99/11/08
*
* Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.filechooser.*;
import javax.accessibility.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import java.applet.*;
import java.net.*;
/**
* JButton, JRadioButton, JToggleButton, JCheckBox Demos
*
* @version 1.4 11/08/99
* @author Jeff Dinkins
*/
public class Button2 extends DemoModule implements ChangeListener {
JTabbedPane tab;
JPanel buttonPanel = new JPanel();
JPanel checkboxPanel = new JPanel();
JPanel radioButtonPanel = new JPanel();
JPanel toggleButtonPanel = new JPanel();
Vector buttons = new Vector();
Vector checkboxes = new Vector();
Vector radiobuttons = new Vector();
Vector togglebuttons = new Vector();
Vector currentControls = buttons;
JButton button;
JCheckBox check;
JRadioButton radio;
JToggleButton toggle;
EmptyBorder border5 = new EmptyBorder(5,5,5,5);
EmptyBorder border10 = new EmptyBorder(10,10,10,10);
ItemListener buttonDisplayListener = null;
ItemListener buttonPadListener = null;
Insets insets0 = new Insets(0,0,0,0);
Insets insets10 = new Insets(10,10,10,10);
/**
* main method allows us to run as a standalone demo.
*/
public static void main(String[] args) {
ButtonDemo demo = new ButtonDemo(null);
demo.mainImpl();
}
/**
* ButtonDemo Constructor
*/
public ButtonDemo(SwingSet2 swingset) {
// Set the title for this demo, and an icon used to represent this
// demo inside the SwingSet2 app.
super(swingset, "ButtonDemo", "toolbar/JButton.gif");
tab = new JTabbedPane();
tab.getModel().addChangeListener(this);
JPanel demo = getDemoPanel();
demo.setLayout(new BoxLayout(demo, BoxLayout.Y_AXIS));
demo.add(tab);
addButtons();
addRadioButtons();
addCheckBoxes();
// addToggleButtons();
}
public void addButtons() {
tab.addTab(getString("ButtonDemo.buttons"), buttonPanel);
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.setBorder(border5);
JPanel p1 = createVerticalPanel(true);
p1.setAlignmentY(TOP_ALIGNMENT);
buttonPanel.add(p1);
// Text Buttons
JPanel p2 = createHorizontalPanel(false);
p1.add(p2);
p2.setBorder(new CompoundBorder(new TitledBorder(null, getString("ButtonDemo.textbuttons"),
TitledBorder.LEFT, TitledBorder.TOP), border5));
buttons.add(p2.add(new JButton(getString("ButtonDemo.button1"))));
p2.add(Box.createRigidArea(HGAP10));
buttons.add(p2.add(new JButton(getString("ButtonDemo.button2"))));
p2.add(Box.createRigidArea(HGAP10));
buttons.add(p2.add(new JButton(getString("ButtonDemo.button3"))));
// Image Buttons
p1.add(Box.createRigidArea(VGAP30));
JPanel p3 = createHorizontalPanel(false);
p1.add(p3);
p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imagebuttons"),
TitledBorder.LEFT, TitledBorder.TOP));
// home image button
String description = getString("ButtonDemo.phone");
button = new JButton(createImageIcon("buttons/b1.gif", description));
button.setPressedIcon(createImageIcon("buttons/b1p.gif", description));
button.setRolloverIcon(createImageIcon("buttons/b1r.gif", description));
button.setDisabledIcon(createImageIcon("buttons/b1d.gif", description));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setMargin(new Insets(0,0,0,0));
p3.add(button);
buttons.add(button);
p3.add(Box.createRigidArea(HGAP10));
// write image button
description = getString("ButtonDemo.write");
button = new JButton(createImageIcon("buttons/b2.gif", description));
button.setPressedIcon(createImageIcon("buttons/b2p.gif", description));
button.setRolloverIcon(createImageIcon("buttons/b2r.gif", description));
button.setDisabledIcon(createImageIcon("buttons/b2d.gif", description));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setMargin(new Insets(0,0,0,0));
p3.add(button);
buttons.add(button);
p3.add(Box.createRigidArea(HGAP10));
// write image button
description = getString("ButtonDemo.peace");
button = new JButton(createImageIcon("buttons/b3.gif", description));
button.setPressedIcon(createImageIcon("buttons/b3p.gif", description));
button.setRolloverIcon(createImageIcon("buttons/b3r.gif", description));
button.setDisabledIcon(createImageIcon("buttons/b3d.gif", description));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setMargin(new Insets(0,0,0,0));
p3.add(button);
buttons.add(button);
p1.add(Box.createVerticalGlue());
buttonPanel.add(Box.createRigidArea(HGAP10));
buttonPanel.add(createControls(buttons));
}
public void addRadioButtons() {
tab.addTab(getString("ButtonDemo.radiobuttons"), radioButtonPanel);
radioButtonPanel.setLayout(new BoxLayout(radioButtonPanel, BoxLayout.X_AXIS));
radioButtonPanel.setBorder(border5);
JPanel p1 = createVerticalPanel(true);
p1.setAlignmentY(TOP_ALIGNMENT);
radioButtonPanel.add(p1);
// Text Radio Buttons
JPanel p2 = createHorizontalPanel(false);
p1.add(p2);
p2.setBorder(new CompoundBorder(
new TitledBorder(
null, getString("ButtonDemo.textradiobuttons"),
TitledBorder.LEFT, TitledBorder.TOP), border5)
);
radiobuttons.add(p2.add(new JRadioButton(getString("ButtonDemo.radio1"))));
p2.add(Box.createRigidArea(HGAP10));
radiobuttons.add(p2.add(new JRadioButton(getString("ButtonDemo.radio2"))));
p2.add(Box.createRigidArea(HGAP10));
radiobuttons.add(p2.add(new JRadioButton(getString("ButtonDemo.radio3"))));
ButtonGroup group = new ButtonGroup();
// Image Radio Buttons
p1.add(Box.createRigidArea(VGAP30));
JPanel p3 = createHorizontalPanel(false);
p1.add(p3);
p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imageradiobuttons"),
TitledBorder.LEFT, TitledBorder.TOP));
// image radio button 1
String description = getString("ButtonDemo.customradio");
String text = getString("ButtonDemo.radio1");
radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
radio.setFocusPainted(false);
radio.setBorderPainted(false);
radio.setContentAreaFilled(false);
radio.setMargin(new Insets(0,0,0,0));
group.add(radio);
p3.add(radio);
radiobuttons.add(radio);
p3.add(Box.createRigidArea(HGAP20));
// image radio button 2
text = getString("ButtonDemo.radio2");
radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
radio.setFocusPainted(false);
radio.setBorderPainted(false);
radio.setContentAreaFilled(false);
radio.setMargin(new Insets(0,0,0,0));
group.add(radio);
p3.add(radio);
radiobuttons.add(radio);
p3.add(Box.createRigidArea(HGAP20));
// image radio button 3
text = getString("ButtonDemo.radio3");
radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
radio.setFocusPainted(false);
radio.setBorderPainted(false);
radio.setContentAreaFilled(false);
radio.setMargin(new Insets(0,0,0,0));
group.add(radio);
radiobuttons.add(radio);
p3.add(radio);
// verticaly glue fills out the rest of the box
p1.add(Box.createVerticalGlue());
radioButtonPanel.add(Box.createRigidArea(HGAP10));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -