[Java的挥杆] JToolBar中使用Java

JToolBar中是一个长条的图标,包括相应的​​按钮菜单下通常位于, 在Word工具栏一样, 擅长用新的按钮, 开放, 保存, …

JToolBar中在java中

我们将执行类似上面的图像节目, 含 3 新的按钮, 开放, 保存在工具栏上. 当您在下面的JTextArea单击按钮将显示相应的事件.

package nguyenvanquan7826.JToolBar;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

/**
 * --------------------- @author nguyenvanquan7826 ---------------------
 * ------------------ website: cachhoc.net -------------------
 */
public class DemoJToolBar extends JFrame implements ActionListener {

	public static final String NEW = "new";
	public static final String OPEN = "open";
	public static final String SAVE = "save";

	public static String iconNew = "iconNew.png";
	public static String iconOpen = "iconOpen.png";
	public static String iconSave = "iconSave.png";

	private JTextArea ta;

	public DemoJToolBar() {
		setJMenuBar(createMenuBar());
		addContent();
		setDisplay();
	}

	/**
	 * set display for JFrame with title is "Demo JToolBar"
	 */
	private void setDisplay() {
		setTitle("Demo JToolBar");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * demo menu
	 */
	private JMenuBar createMenuBar() {
		JMenuBar mb = new JMenuBar();
		mb.add(new JMenu("File"));
		mb.add(new JMenu("Help"));
		return mb;
	}

	/**
	 * add content of JFrame, it contain a JToolBar and a JTextArea to display
	 * event on toolbar
	 */
	private void addContent() {
		setLayout(new BorderLayout());
		add(createToolBar(), BorderLayout.PAGE_START);
		add(new JScrollPane(ta = createTextArea(15, 30)), BorderLayout.CENTER);
	}

	/**
	 * create a toolbar
	 */
	private JToolBar createToolBar() {
		JToolBar tb = new JToolBar();
		tb.add(createButton(iconNew, "New", NEW, "Create a new file"));
		tb.add(createButton(iconOpen, "Open", OPEN, "Open a file"));
		tb.add(createButton(iconSave, "Save", SAVE, "Save this file"));
		return tb;
	}

	/**
	 * create a JTextArea to display events
	 */
	private JTextArea createTextArea(int row, int col) {
		JTextArea ta = new JTextArea(row, col);
		ta.setWrapStyleWord(true);
		ta.setLineWrap(true);
		// ta.setEditable(false);
		return ta;
	}

	/**
	 * create a Button, it has a icon, tex (if not found icon), actionCommand
	 * and tool tip text
	 */
	private JButton createButton(String iconLink, String text, String command,
			String toolTip) {
		JButton btn = new JButton();
		btn.setActionCommand(command);
		btn.setToolTipText(toolTip);

		ImageIcon icon = createIcon(iconLink);
		if (icon == null) {
			btn.setText(text);
		} else {
			btn.setIcon(icon);
		}
		btn.setActionCommand(command);
		btn.addActionListener(this);
		return btn;
	}

	/**
	 * create icon
	 */
	private ImageIcon createIcon(String iconLink) {
		URL imageURL = JToolBarOracle.class.getResource(iconLink);
		if (imageURL != null) {
			return new ImageIcon(imageURL);
		} else {
			System.out.println("image " + iconLink + " not found");
			return null;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		String action = "null";
		if (command.equals(NEW)) {
			action = "new file";
		} else if (command.equals(OPEN)) {
			action = "open file";
		} else if (command.equals(SAVE)) {
			action = "save file";
		}
		ta.append(action + "\n");
	}

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

此工具栏,你可以把你的鼠标放在上面吧 (更多的点段) 拖动把它在水平或垂直位置. 如果你想使用的方法来解决这个问题 toolBar.setFloatable(假);, 此外,还可以添加其他对象,如JTextField中,… 做出这样的搜索栏.

图标 , 开放, 保存 功能套餐您以下:

JToolBar中在java中

阅读更多: 类JToolBar中, 用户JToolBar中, 的Java Swing