来自JDK API 1.6.0:

Try this:

  1. Click the Launch button to run the Button Demo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.

    Run ButtonDemo (
    download JDK 7 or later). Or, to compile and run the example yourself,
    consult the
    example index.

    -->

  2. Click the left button. It disables the middle button (and itself, since it is no longer useful) and enables the right button.
  3. Click the right button. It enables the middle button and the left button, and disables itself.

As the ButtonDemo example shows, a Swing button can display both text and an image. In ButtonDemo, each button has its text in a different place, relative to its image. The underlined letter(下划线字母,提供了一种ALT+大写字母的快捷键方式。一种(mnemonic)助记的方式) in each button's text shows the mnemonic — the keyboard alternative — for each button. In most look and feels, the user can click a button by pressing the Alt key and the mnemonic. For example, Alt-M would click the Middle button in ButtonDemo.

When a button is disabled, the look and feel automatically generates the button's disabled appearance. However, you could provide an image to be substituted for the normal image. For example, you could provide gray versions of the images used in the left and right buttons.

How you implement event handling depends on the type of button you use and how you use it. Generally, you implement an action listener, which is notified every time the user clicks the button. For check boxes you usually use an item listener, which is notified when the check box is selected or deselected.

婷婷总结:Button是需要action listener的,而check boxes是需要item listener.监听程序是不一样的。addItemListener().

Below is the code from ButtonDemo.java that creates the buttons in the previous example and reacts to button clicks. The bold code is the code that would remain if the buttons had no images.

//In initialization code:
ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("images/left.gif"); b1 = new JButton("Disable middle button", leftButtonIcon);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable"); b2 = new JButton("Middle button", middleButtonIcon);
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setMnemonic(KeyEvent.VK_M); b3 = new JButton("Enable middle button", rightButtonIcon);
//Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.setEnabled(false);
//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b3.addActionListener(this); b1.setToolTipText("Click this button to disable "
+ "the middle button.");
b2.setToolTipText("This middle button does nothing "
+ "when you click it.");
b3.setToolTipText("Click this button to enable the "
+ "middle button.");

...
} public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
} else {
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
}
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ButtonDemo.class.getResource(path);
...//error handling omitted for clarity...
return new ImageIcon(imgURL);
}

How to Use JButton Features

Ordinary buttons — JButton objects — have just a bit more functionality than the AbstractButton class provides: You can make a JButton be the default button.

最新文章

  1. css3实现超出文本指定行数(指定文本长度)用省略号代替
  2. Java---Java的面试题(一)
  3. Octopress博客使用
  4. initWithFrame方法的理解
  5. C语言之可重入函数 && 不可重入函数
  6. java 集合框架(二)Iterable接口
  7. vue2.0项目引入element-ui
  8. HTML的各种基本标签
  9. 关于解决Mac使用docker安装SQL server for Linux 中文乱码问题
  10. Ubuntu 15.10 下Tachyon安装
  11. Gulp应用场景
  12. spring websocket自动断开连接再创建引发的问题解决方案
  13. 3G下的无压缩视频传输(基于嵌入式linux) (转载)
  14. mybatis结合redis实战二级缓存
  15. 完全卸载mysql数据库教程
  16. LeetCode OJ:Binary Tree Paths(二叉树路径)
  17. 修改apk里面的源码
  18. 【密码学】Https握手协议以及证书认证
  19. CC20:高度最小的BST
  20. 将数字转换成Excel表头格式的字母序号

热门文章

  1. 源码编译安装mysql5.6
  2. C++中关于strtok()函数的用法
  3. python_根据"词库"进行“词联想”
  4. RabbitMQ集群 Docker一键部署
  5. JSONP解决跨域方案
  6. Python函数-eval()
  7. POJ1274(二分图最大匹配)
  8. Java基础--压缩和解压缩gz包
  9. 创建github怎样管理
  10. 一张图看懂------left join;right join;inner join