JButton的使用

JButton的使用:

JButton的类层次结构图:
java.lang.Object 
    --java.awt.Component
     --javax.swing.JComponent
      --javax.swing.AbstractButton
      --javax.swing.JButton

JButton是继承AbstractButton类而来,而AbstractButton本身是一个抽象类,里面定义了许多组件设置的方法与组件事件驱动 方法(Event handle),如addActionListener()、setText等,详情请看相关手册,所提供的方法不下50种,可说是非常重要的一个 类。事实上,AbstractButton类不公被JButton所继承,它同时还被JMenuItem、JToggleButton、JCheckBox、JRadioButton等类所 继承,提供给这些类强大且方便的功能,而且在使用上更能掌握这些组件的特性。我们此节先来了解JButton与JToggleButton的特 性,其余类在后面各节介绍。

JButton共有4个构造函数:
  • JButton():建立一个按钮。
  • JButton(Icon icon):建立一个有图像的按钮。
  • JButton(String icon):建立一个有文字的按钮。
  • JButton(String text,Icon icon):建立一个有图像与文字的按钮。

由JButton的构造函数可以看出:JButton与JLabel的使用相当类似,只是JButton少了排列方式的参数罢了。要是我们想设置 JButton内文字或图像的水平排列方式,我们可以利用AbstractButton抽象类所提供的setHorizontalAlignment()方法来达成。 JButton在使用上与JLabel相当类似,只是类的设计方式有所不同,JLabel类自行提供组件排列方式的方法,而JButton是继承 AbstractButton抽象类的方法来排列按钮内的内容。为什么JButton不自行提供排列方式等方法呢?主要是因为JButton与JMenuItem 、JToggleButton、JCheckBox、JRadioButton组件有许多共同的物性,例如它们都会有“按”的操作、都可以插入Icon与文字、 都可设置快捷键、都可呈现Enable或Disable状态等等,因此将AbstractButton类独立出来,实现这些共通的方法,再由其他类来继 承,将可增加程序结构对象化与模块化的特性,也让程序更容易维护.

JButton类所提供的方法非常少,大部份都会用到AbstractButton抽象类所提供的方法。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JButtonDemo1 {
	public static void main(String[] args) {
		JFrame f = new JFrame("JButtonDemo1");
		Container contentPane = f.getContentPane();
		/*
		 * 你也可以用下面这个方式代替:JButton b=new JButton();b.setIcon(new
		 * ImageIcon(".\\icons\\hand.jpg"));b.setText("按我");
		 */
		JButton b = new JButton("按我", new ImageIcon(".\\icons\\hand.jpg"));
		/*
		 * 如果没有设置文字的位置,系统默认值会将文字置于图形的右边中间位置。
		 */
		b.setHorizontalTextPosition(JButton.CENTER);
		b.setVerticalTextPosition(JButton.BOTTOM);
		contentPane.add(b);
		f.pack();
		f.show();
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
}

6-4-1:在JButton上使用Rollover图像变化:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RolloverDemo {
	public static void main(String[] args) {
		JFrame f = new JFrame("RolloverDemo");
		Container contentPane = f.getContentPane();
		Icon rollover = new ImageIcon(".\\icons\\address1.jpg");
		Icon general = new ImageIcon(".\\icons\\address2.jpg");
		Icon press = new ImageIcon(".\\icons\\address3.jpg");
		JButton b = new JButton();
		b.setRolloverEnabled(true);// 将Rollver功能打开。
		b.setIcon(general);
		b.setRolloverIcon(rollover);
		b.setPressedIcon(press);
		contentPane.add(b);
		f.pack();
		f.show();
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
}

6-4-2:在JButton上设置快捷键:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MnemonicButton implements ActionListener {
	public MnemonicButton() {
		JFrame f = new JFrame("MnemonicButton");
		Container contentPane = f.getContentPane();
		JButton b = new JButton("[o]打开新窗口");
		b.setMnemonic('o');
		/*
		 * 将b加入事件处理模式,当我们按下按钮时,会产生一个事件(ActionEvent),此事件会被ActionListener
		 * 所接收。而ActionListener是一个interface,里面只有actionPerformed()一个方法,因此我们必须实现
		 * actionPerformed()方法,处理我们所要的结果。
		 */
		b.addActionListener(this);
		contentPane.add(b);
		f.pack();
		f.show();
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public void actionPerformed(ActionEvent e) {
		JFrame newf = new JFrame("新窗口");
		JLabel label = new JLabel("这是新窗口");
		label.setHorizontalAlignment(JLabel.CENTER);
		newf.getContentPane().add(label);
		newf.setSize(100, 100);
		newf.show();
	}

	public static void main(String[] args) {
		new MnemonicButton();
	}
}

6-4-3:设置默认按钮:

在java中要设置默认按钮可以使用JRootPane类所提供的setDefaultButton()方法,下面的例子我们将事件处理模式换成另 一种写法,也就是前面所提到的Inner Class匿名类写法,因此在类DefaultButton的右边就不用再写入implements ActionListener 了。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DefaultButton {
	public DefaultButton() {
		JFrame f = new JFrame("DefaultButton");
		Container contentPane = f.getContentPane();
		contentPane.setLayout(new GridLayout(1, 2));
		JButton b1 = new JButton("Open Text window");
		JButton b2 = new JButton("Open Image window");
		b1.setMnemonic('T');
		b2.setMnemonic('I');
		f.getRootPane().setDefaultButton(b1);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFrame newf = new JFrame("新窗口");
				JLabel label = new JLabel("这是文字窗口");
				label.setHorizontalAlignment(JLabel.CENTER);
				newf.getContentPane().add(label);
				newf.setSize(200, 200);
				newf.show();
			}
		});
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFrame newf = new JFrame("新窗口");
				JLabel label = new JLabel(new ImageIcon(
						".\\icons\\address1.jpg"));
				label.setHorizontalAlignment(JLabel.CENTER);
				newf.getContentPane().add(label);
				newf.setSize(200, 200);
				newf.show();
			}
		});
		contentPane.add(b1);
		contentPane.add(b2);
		f.pack();
		f.show();
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new DefaultButton();
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值