Swing开发JButton图标效果篇

来源:http://www.open-open.com/lib/view/open1376227965709.html
摘要:  本文通过给JButton添加鼠标事件,对JButton一个简单的美化

    JButton是AbstractButton类的扩展,当按钮被选中,点击时会出现不同的状态,Swing本身的展示效果在绘制面板的时候不尽人意,因此可以通过更改按钮的鼠标事件来美化按钮。

1.TestFrame.java,用于绘制主面板的组件,以下是源码:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicButtonUI;

public class TestFrame extends JFrame {

	private JTextField searchText = null;
	private JButton searchBtn = null;
	private JButton editBtn = null;
	private JButton saveBtn = null;

	public TestFrame() {
		setLookAndFeel();
		init();
	}

	/**
	 * 为主面板设置皮肤
	 */
	private void setLookAndFeel() {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 增加工具栏的组件,并设置样式
	 */
	private JMenuBar addMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		menuBar.setLayout(new FlowLayout(40, 4, 0));
		// 增加文件选择按钮
		menuBar.add(createBtn("File", "./src/folder_up.png"));

		// 增加编辑按钮
		editBtn = createBtn("Edit", "./src/page_edit.png");
		//editBtn.setEnabled(false);
		menuBar.add(editBtn);

		// 增加保存文件按钮
		saveBtn = createBtn("Save", "./src/folder_down.png");
		//saveBtn.setEnabled(false);
		menuBar.add(saveBtn);

		// 增加查询输入文本框
		searchText = new JTextField();
		searchText.setPreferredSize(new Dimension(140, 27));
		menuBar.add(searchText);

		// 增加查询按钮
		searchBtn = createBtn("Search", "./src/search.png");
		//searchBtn.setEnabled(false);
		menuBar.add(searchBtn);

		return menuBar;
	}

	/**
	 * 创建工具栏按钮
	 * 
	 * @author lqj
	 * @param text
	 *            按钮名称
	 * @param icon
	 *            按钮图标所在路径
	 * @return 返回添加样式和监听器后的按钮
	 */
	private JButton createBtn(String text, String icon) {
		JButton btn = new JButton(text, new ImageIcon(icon));
		btn.setUI(new BasicButtonUI());// 恢复基本视觉效果
		btn.setPreferredSize(new Dimension(80, 27));// 设置按钮大小
		btn.setContentAreaFilled(false);// 设置按钮透明
		btn.setFont(new Font("粗体", Font.PLAIN, 15));// 按钮文本样式
		btn.setMargin(new Insets(0, 0, 0, 0));// 按钮内容与边框距离
		btn.addMouseListener(new MyMouseListener(this));
		return btn;
	}

	/**
	 * @author lqj
	 * 初始化主面板和工作栏内容
	 */
	private void init() {

		JMenuBar menuBar = addMenuBar();
		// menuBar.setBackground(Color.WHITE);
		menuBar.setPreferredSize(new Dimension(1400, 30));
		this.setJMenuBar(menuBar);
		this.setSize(1320, 750);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setTitle("TestButton");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		// this.setBackground(Color.gray);
	}

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

}

 2.MyMouseListener.java,用于增加鼠标事件,源码如下:

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

public class MyMouseListener implements MouseListener {

	private TestFrame frame;

	public MyMouseListener() {
		// TODO Auto-generated constructor stub
	}

	public MyMouseListener(TestFrame f) {
		this.frame = f;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		String btnName =((JButton)e.getSource()).getText();
		if ("File".equals(btnName.trim())) {
			//鼠标点击事件
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		JButton btn = (JButton) e.getComponent();
		Border etchedBorder = new EtchedBorder(EtchedBorder.RAISED);// 设置边框凸显
		btn.setBorder(etchedBorder);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		JButton btn = (JButton) e.getComponent();
		Border etchedBorder = new EtchedBorder(EtchedBorder.LOWERED);// 设置边框凹显
		btn.setBorder(etchedBorder);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		JButton btn = (JButton) e.getComponent();
		btn.setForeground(new Color(0x33, 0x66, 0xcc));// 设置字体颜色
		btn.setBorderPainted(true);// 显示边框
		Border etchedBorder = new EtchedBorder(EtchedBorder.LOWERED);// 设置边框凹显
		btn.setBorder(etchedBorder);
		btn.setRolloverEnabled(true);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		JButton btn = (JButton) e.getComponent();
		btn.setForeground(Color.black);// 设置字体颜色
		btn.setBorderPainted(false);// 隐藏边框
	}

}

最后上传一张运行的效果图片给大家看!希望对大家有用!

Swing开发JButton图标效果篇



你可能感兴趣的:(java)