import javax.swing.*;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.*;
/**
* Title:JButton操作
* Description:演示按钮的事件响应和按钮的实效
* @author fan
*
*/
public class MyButtonDemo extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 定义三个按钮
*/
private JButton b1, b2, b3;
/**
* 定义ImageIcon对象用于图标对象
*/
private ImageIcon rightImage, middleImage, leftImage;
/**
* 定义一个面板
*/
private JPanel contentPane =new JPanel();
/**
* 方法说明:构造方法
*/
public MyButtonDemo(){
leftImage = createImageIcon("base2011/images/left.gif");
middleImage = createImageIcon("base2011/images/middle.gif");
rightImage = createImageIcon("base2011/images/right.gif");
//构建窗体
this.lunchFrame("ButtonDemo");
}
/**
* 构建窗体
*/
private void lunchFrame(String title){
b1 = new JButton("失效中间按钮", leftImage);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("Disable");
b1.setToolTipText("点击此按钮时中间按钮失效");
b1.addActionListener(this);
b2 = new JButton("中间按钮", middleImage);
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.LEADING);
b2.setMnemonic(KeyEvent.VK_M);
b2.setToolTipText("点击此按钮无任何事件发生");
b3 = new JButton("激活中间按钮", rightImage);
b3.setHorizontalTextPosition(AbstractButton.LEADING);
b3.setVerticalTextPosition(AbstractButton.CENTER);
b3.setMnemonic(KeyEvent.VK_E);
b3.setToolTipText("点击此按钮使中间按钮激活");
b3.setActionCommand("Enable");
b3.setEnabled(false);
b3.addActionListener(this);
contentPane.add(b1);
contentPane.add(b2);
contentPane.add(b3);
contentPane.setOpaque(false);//绘制边界内所有元素
this.setContentPane(contentPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle(title);
this.pack();
//将窗体定义到屏幕中间位置
this.setCenterLocation();
JFrame.setDefaultLookAndFeelDecorated(false);
}
/**
* 方法说明:创建图标对象
* @param path 图片的存放路径
* @return ImageIcon
*/
protected static ImageIcon createImageIcon(String path){
ImageIcon image = null;
try{
java.net.URL imgURL = MyButtonDemo.class.getClassLoader().getResource(path);
if(imgURL != null){
image = new ImageIcon(imgURL);
}else{
System.err.println("could not find file: "+path);
}
}catch(Exception e){
e.printStackTrace();
}
if(image != null){
return image;
}else{
return null;
}
}
/**
* 方法说明:将窗体定位到屏幕中间
*/
public void setCenterLocation(){
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = this.getSize().width;
int h = this.getSize().height;
System.out.println("宽度: "+w+" ,高度:"+h);
int x = (dim.width - w)/2;
int y = (dim.height - h)/2;
this.setLocation(x, y);
}
/**
* 方法说明:事件处理
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if("Disable".equals(e.getActionCommand())){
b1.setEnabled(false);
b2.setEnabled(false);
b3.setEnabled(true);
}else{
b1.setEnabled(true);
b2.setEnabled(true);
b3.setEnabled(false);
}
}
/**
* 方法说明:主方法
* @param args
*/
public static void main(String[] args){
new MyButtonDemo();
}
}