如何将ImageIcon添加到JToolBar [英] How to add an ImageIcon to a JToolBar

查看:134
本文介绍了如何将ImageIcon添加到JToolBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在工具栏中添加一个图标但是最好放在哪个位置?我的桌面或者我应该在项目文件中创建一个新文件,或者添加所有图片,因为它没有显示,这是我的代码:

I am trying to add a icon to a toolbar but what is the best place to put it in? My desktop or should I make a new file in the project file or add all the pictures in because it is not showing and this is my code:

 JToolBar toolBar = new JToolBar();
     String[] iconFiles = {"pen-icon","",""};
     String[] buttonLabels = {"New","Open","Save"};
     icon = new ImageIcon[iconFiles.length];
     Obutton = new JButton[buttonLabels.length];

     for (int i = 0; i < buttonLabels.length; ++i) {
      icon[i] = new ImageIcon(iconFiles[i]);
      Obutton[i] = new JButton(icon[i]);
      Obutton[i].setToolTipText(buttonLabels[i]);
      if (i == 3)
        toolBar.addSeparator();
         toolBar.add(Obutton[i]);
    }


推荐答案

我会用操作。这是 AbstractAction 构造函数


  • public AbstractAction(String name,Icon icon) - 创建一个具有指定名称和小图标的Action。

  • public AbstractAction(String name, Icon icon) - Creates an Action with the specified name and small icon.

参数:

name - 操作的名称(Action.NAME); null值被忽略

icon - 动作的小图标(Action.SMALL_ICON);忽略null值

Parameters:
name - the name (Action.NAME) for the action; a value of null is ignored
icon - the small icon (Action.SMALL_ICON) for the action; a value of null is ignored

使用 Action 是可以重用于具有类似用途的组件。所以说你想在工具栏中有一个图标按钮来打开文件,并且在 JMenu JMenuItem >那也打开一个文件。他们可以共享相同的操作,从而共享相同的图标,操作命令和要执行的操作。

The benefit of using an Action is that is can be reused for components with similar purposes. So say you want to have an icon button in the toolbar to open a file, and also have a JMenuItem in a JMenu that also opens a file. They could share the same action, thus sharing the same icon, action command, and action to perform.

Action action = new AbstractAction("someActionCommand", someIcon) {
    @Override
    public void actionPerformed(ActionEvent e) {
        // do something.
    }
};

toolbar.add(action);

上面会自动为你设置图标,但不会自动为你设置字符串。在 JMenuItem 中,它会同时放置字符串和图标。

The above will automatically put the icon for you, but not the String. In a JMenuItem it would put both the String and the icon.

然后只需添加动作到工具栏。

Then just add the Action to the tool bar.

如何使用操作

为了回答真正的问题,正如@MadProgrammer所说,你应该将你的图像加载为 嵌入式资源 ,使用

To answer you real question, as @MadProgrammer noted, you should be loading your images as an embedded resource, using

 ImageIcon icon = new ImageIcon(MyClass.class.getResource("/resources/images/image.png"));

其中 / resources / images 目录是在 src 中, getResource()返回一个URL。在构建时,IDE应该将文件复制到类路径中。

where the /resources/images directory is in the src, and getResource() returns a URL. Upon build, your IDE should copy the files into the class path for you.

 ProjectRoot
           src
               resources
                       images
                             image.png

你会发现当使用文件系统中的文件,在部署时不起作用

You'll come to find that when using a file from the file system, will not work upon time of deployment

这是一个例子,其中 JMenuItem JToolBar 按钮共享相同的操作。请注意,在 JToolBar 所有我要做的就是添加 Action ,我不要不需要为它创建一个按钮。 JToolBar 自动使其成为一个按钮,没有动作命令

Here's an example, where the JMenuItem and the JToolBar button share the same action. Notice that in the JToolBar all I have to do is add the Action, I don't need to create a button for it. The JToolBar automatically makes it a button, without the action command

我用它来自以下文件结构的open.gif并使用

I use this "open.gif" from the below file structure and use

ImageIcon icon = new ImageIcon(
            ActionTest.class.getResource("/resources/image/open.gif"));

结果是

这是代码。享受!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class ActionTest {

    public ActionTest() {
        ImageIcon openIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/open.gif"));
        ImageIcon saveIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/save.gif"));
        ImageIcon newIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/new.gif"));

        Action openAction = new AbstractAction("Open", openIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Open File");
            }
        };
        Action saveAction = new AbstractAction("Save", saveIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Save File");
            }
        };
        Action newAction = new AbstractAction("New", newIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("New File");
            }
        };

        JMenuItem openMenuItem = new JMenuItem(openAction);
        JMenuItem saveMenuItem = new JMenuItem(saveAction);
        JMenuItem newMenuItem = new JMenuItem(newAction);

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(newMenuItem);
        menuBar.add(fileMenu);

        JToolBar toolBar = new JToolBar();
        toolBar.add(Box.createHorizontalGlue());
        toolBar.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
        toolBar.add(newAction);
        toolBar.add(openAction);
        toolBar.add(saveAction);

        JFrame frame = new JFrame("Toolbar and Menu Test");
        frame.setJMenuBar(menuBar);
        frame.add(toolBar, BorderLayout.PAGE_START);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ActionTest();
            }
        });
    }
}

这篇关于如何将ImageIcon添加到JToolBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆