一个ButtonDemo的实现过程。
来自JDK API 1.6.0:
Try this:
- 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.-->
- Click the left button. It disables the middle button (and itself, since it is no longer useful) and enables the right button.
- 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.
一个ButtonDemo的实现过程。的更多相关文章
- linux内核分析作业6:分析Linux内核创建一个新进程的过程
task_struct结构: struct task_struct { volatile long state;进程状态 void *stack; 堆栈 pid_t pid; 进程标识符 u ...
- 网站开发进阶(四)Tomcat Server处理一个http请求的过程
Tomcat Server处理一个http请求的过程 假设来自客户的请求为: http://localhost:8080/wsota/wsota_index.jsp 1) 请求被发送到本机端口8080 ...
- Tomcat系列(6)——Tomcat处理一个HTTP请求的过程
Tomcat的架构图 图三:Tomcat Server处理一个HTTP请求的过程 处理HTTP请求过程 假设来自客户的请求为:http://localhost:8080/test/index.js ...
- Tomcat Server处理一个http请求的过程
Tomcat Server处理一个http请求的过程 假设来自客户的请求为: http://localhost:8080/wsota/wsota_index.jsp 1) 请求被发送到本机端口8080 ...
- .NET Core微服务之路:利用DotNetty实现一个简单的通信过程
上一篇我们已经全面的介绍过<基于gRPC服务发现与服务治理的方案>,我们先复习一下RPC的调用过程(笔者会在这一节的几篇文章中反复的强调这个过程调用方案),看下图
- Spring MVC 原理探秘 - 一个请求的旅行过程
1.简介 在前面的文章中,我较为详细的分析了 Spring IOC 和 AOP 部分的源码,并写成了文章.为了让我的 Spring 源码分析系列文章更为丰富一些,所以从本篇文章开始,我将来向大家介绍一 ...
- 第六周分析Linux内核创建一个新进程的过程
潘恒 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 task_struct结构: ...
- 实验 六:分析linux内核创建一个新进程的过程
实验六:分析Linux内核创建一个新进程的过程 作者:王朝宪 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029 ...
- 20135202闫佳歆--week6 分析Linux内核创建一个新进程的过程——实验及总结
week 6 实验:分析Linux内核创建一个新进程的过程 1.使用gdb跟踪创建新进程的过程 准备工作: rm menu -rf git clone https://github.com/mengn ...
随机推荐
- git公私钥的拷贝
1.场景 由于我换了一台电脑,为了能访问远程仓库,我就把原来电脑上的私钥和config文件拷贝过来 2.出现问题及解决方案 git clone server:xxx_service_express 报 ...
- Arc066_F Contest with Drinks Hard
传送门 题目大意 有一个长为$N$的序列$A$,你要构造一个长为$N$的$01$序列使得$01$序列全部由$1$组成的子串个数$-$两个序列的对应位置两两乘积之和最大,每次独立的询问给定$pos,x$ ...
- grpc asp.net core 集成时一些配置的说明
一 什么是grpc google出了一款分布式通讯框架:grpc.我想这也不是新的东西了,在13年的一个项目中,用在了数据层和业务端之间的通讯上,当时并没有觉得怎么样,因为wcf很轻松的也可以可以实 ...
- java向excel写数据
package pymongo1; import java.io.File;import java.io.IOException;import java.io.OutputStream; import ...
- Phong光照模型的Shader实现
计算反射向量 Phong用到的是反射向量,计算反射向量的公式是 R = 2*N(dot(N, L)) - L 这个公式是根据向量的投影公式以及平行四边形法则推导出来的 详细步骤请看这篇文章,讲的非常好 ...
- Python 算法之冒泡排序
冒泡排序 冒泡排序算法的原理如下:(从后往前) 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是 ...
- 极路由U-boot解锁刷root固件教程,root后可刷华硕、如意云等多种固件,附赠全套刷软
9008正式版固件将会封堵此漏洞,想root的同学尽快了.安装新工具箱里的root保留,可升级官方最新固件并保留root. 此方法并非本人原创,只是将root的过程和经验做个总结,比较适合菜鸟做参考, ...
- struts2获得需要的文件或者访问路径
在struts2中,上传文件的时候遇到一个很好用但是失效的方法,找到如下替代.并且测试了一下request可以得到的相关路径. 得到request对象: HttpServletRequest requ ...
- redis学习二 排序
文章转载自:http://www.cnblogs.com/redcreen/archive/2011/02/15/1955226.html redis支持对list,set和sorted set元素的 ...
- HDLM命令dlnkmgr详解之五_set
set命令用来设置HDLM的操作环境及参数. set操作设置的大部分是dlnkmgr view -sys命令中显示的参数值 命令格式 dlnkmgr set { -lb on [ -lbtype { ...