我正在尝试将图标添加到工具栏,但放置它的最佳位置是什么?我的桌面或者我应该在项目文件中创建一个新文件或添加所有图片,因为它没有显示,这是我的代码:
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]);
}
最佳答案
我会使用Action
。这是 AbstractAction
构造函数
public AbstractAction(String name, Icon icon)
- 创建具有指定名称和小图标的 Action。参数:
name - Action 的名称 (Action.NAME);忽略 null 值
icon - Action 的小图标 (Action.SMALL_ICON); null 值被忽略
使用Action
的好处是它可以被具有类似目的的组件重用。比如说你想在工具栏中有一个图标按钮来打开一个文件,并且在 JMenu
中也有一个 JMenuItem
也可以打开一个文件。他们可以共享相同的操作,从而共享相同的图标、操作命令和要执行的操作。
Action action = new AbstractAction("someActionCommand", someIcon) {
@Override
public void actionPerformed(ActionEvent e) {
// do something.
}
};
toolbar.add(action);
上面会自动为您放置图标,但不会放置字符串。在 JMenuItem
中,它会同时放置字符串和图标。
然后只需将 Action
添加到工具栏即可。
查看更多信息 How to use Actions
要回答您的真实问题,正如@MadProgrammer 指出的那样,您应该将图像加载为 embedded resource , 使用
ImageIcon icon = new ImageIcon(MyClass.class.getResource("/resources/images/image.png"));
其中 /resources/images
目录在 src
中,getResource()
返回一个 URL。在构建时,您的 IDE 应该为您将文件复制到类路径中。
ProjectRoot
src
resources
images
image.png
您会发现,当使用文件系统中的文件时,在部署时将不起作用
这是一个示例,其中 JMenuItem
和 JToolBar
按钮共享相同的操作。请注意,在 JToolBar
all 中,我要做的就是添加 Action
,我不需要为其创建按钮。 JToolBar
自动使它成为按钮,没有操作命令
我使用下面文件结构中的“open.gif”并使用
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();
}
});
}
}
关于java - 如何将 ImageIcon 添加到 JToolBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21446957/