JAVA笔记【20131225】

一、工具栏

工具栏是在程序中提供快速访问常用命令的按钮。

工具栏在具有边界布局或者其它支持NORTH,SOUTH,EAST,WEST约束分布的布局管理器的容器内可以被拖拽。

工具栏也可以脱离框架,包含在自己的框架内。

添加组件到工具栏:

JToolBar toolBar = new JToolBar();
toolBar.add(button);
工具栏也可以用来添加Action对象的方法

toolBar.add(blueAction);

工具栏也可以添加分隔符,把按钮组分开。
toolBar.addSeparator();

将工具栏添加到框架中

add(toolBar,BorderLayout.NORTH);

工具栏在没有停靠在界面上时,也可以有自己的标题,在构造方法中可以指定标题。

JToolBar toolBar1 = new JToolBar("xxxx");

工具栏在声明时也可以指定其放置方向,默认情况是水平放置。

JToolBar toolBar2 = new JToolBar(SwingConstants.VERTICAL);

工具栏中可以不止添加按钮,其它组件也可以。


二、工具提示

工具栏上只有图标没有含义,所以有工具提示必要。当鼠标停留在一个按钮上时,会出现文本提示,当鼠标离开按钮时,文本提示消失。

在Swing中可以通过调用setToolTipText方法将工具提示添加到任何一个JComponent组件上。

button.setToolTipText("button");

还有一种方法是使用Action对象,把工具提示与Action.SHORT_DESCRIPTION关联。

exitAction.putValue(Action.SHORT_DESCRIPTION,"exit");


例程:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class MenuToolTest01
{
	public static void main(String[] args)
	{
		MenuToolFrame checkBox = new MenuToolFrame();
		checkBox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		checkBox.setVisible(true);
	}
}

class MenuToolFrame extends JFrame
{
	public MenuToolFrame()
	{
		setTitle("MenuToolBar");
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		
		panel = new JPanel();
		add(panel);
		//菜单栏
		JMenuBar menuBar = new JMenuBar();
		//文件菜单
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		
		JMenuItem newItem = new JMenuItem("New");
		newItem.setMnemonic('N');
		fileMenu.add(newItem);
		JMenuItem openItem = new JMenuItem("Open");
		newItem.setMnemonic('O');
		fileMenu.add(openItem);
		JMenuItem saveItem = new JMenuItem("Save");
		newItem.setMnemonic('S');
		fileMenu.add(saveItem);
		//编辑菜单
		JMenu viewMenu = new JMenu("View");
		viewMenu.setMnemonic('V');
		
		Action blueAct = new ColorAction(Color.BLUE,"Blue",'B');
		Action yellowAct = new ColorAction(Color.YELLOW,"Yellow",'Y');
		Action greenAct = new ColorAction(Color.GREEN,"Green",'G');
		
		JMenuItem blueItem = new JMenuItem(blueAct);
		blueItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_DOWN_MASK));
		viewMenu.add(blueItem);
		JMenuItem yellowItem = new JMenuItem(yellowAct);
		yellowItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,InputEvent.CTRL_DOWN_MASK));
		viewMenu.add(yellowItem);
		JMenuItem greenItem = new JMenuItem(greenAct);
		greenItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_DOWN_MASK));
		viewMenu.add(greenItem);
		viewMenu.addSeparator();
		JMenuItem otherItem = new JMenuItem("Other");
		viewMenu.add(otherItem);
		
		//弹出菜单
		JPopupMenu popMenu = new JPopupMenu();
		popMenu.add(blueAct);
		popMenu.add(yellowAct);
		popMenu.add(greenAct);
		
		panel.setComponentPopupMenu(popMenu);
		
		menuBar.add(fileMenu);
		menuBar.add(viewMenu);
		setJMenuBar(menuBar);
		
		//工具栏
		Toolkit kit = Toolkit.getDefaultToolkit();
		Image img = kit.getImage("2b.jpg").getScaledInstance(15,15,5);
		ImageIcon imgIcon = new ImageIcon(img);
		
		Action blueAct1 = new ColorAction(Color.BLUE,"Blue",imgIcon);
		Action yellowAct1 = new ColorAction(Color.YELLOW,"Yellow",imgIcon);
		Action greenAct1 = new ColorAction(Color.GREEN,"Green",imgIcon);
		
		JToolBar toolBar = new JToolBar("Tools");

		toolBar.add(blueAct1);
		toolBar.add(yellowAct1);
		toolBar.add(greenAct1);

		add(toolBar,BorderLayout.NORTH);
	}

class ColorAction extends AbstractAction
{
	public ColorAction(Color c,String name,char ch)
	{
		putValue(Action.NAME,name);
		this.c=c;
		putValue(Action.MNEMONIC_KEY,new Integer(ch));
		putValue(Action.SHORT_DESCRIPTION,"set background "+name);
	}
	public ColorAction(Color c,String name,ImageIcon icon)
	{
		putValue(Action.NAME,name);
		this.c=c;
		putValue(Action.SMALL_ICON,icon);
		putValue(Action.SHORT_DESCRIPTION,"set background "+name);
	}
	public void actionPerformed(ActionEvent e)
	{
		//System.out.println("调用");
		//System.out.println(c);
		panel.setBackground(c);
	}
	private Color c;
}

	private JPanel panel;
	private static final int DEFAULT_WIDTH=600;
	private static final int DEFAULT_HEIGHT=400;
}

运行结果:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值