Java 如何将图像图标添加到JToolBar
我想在工具栏上添加一个图标,但最好放在什么地方?我的桌面,或者我应该在项目文件中创建一个新文件,还是添加所有图片,因为它不显示,这是我的代码:Java 如何将图像图标添加到JToolBar,java,swing,imageicon,jtoolbar,Java,Swing,Imageicon,Jtoolbar,我想在工具栏上添加一个图标,但最好放在什么地方?我的桌面,或者我应该在项目文件中创建一个新文件,还是添加所有图片,因为它不显示,这是我的代码: JToolBar toolBar = new JToolBar(); String[] iconFiles = {"pen-icon","",""}; String[] buttonLabels = {"New","Open","Save"}; icon = new ImageIcon[iconFiles.length];
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]);
}
JToolBar toolBar=newjtoolbar();
字符串[]iconFiles={“笔图标”、“”、“”};
字符串[]按钮标签={“新建”、“打开”、“保存”};
icon=新图像icon[iconfile.length];
Obutton=newjbutton[buttonLabels.length];
对于(int i=0;i
这种类型的资源通常最好包含在应用程序上下文中(例如jar文件)。这减少了有人篡改它的机会,因为解包、修改和重新打包一个jar文件,然后简单地替换一个文件要花费更多的时间。它还减少了您需要分发的内容,因为它变得自给自足
这些被称为嵌入式资源
在这个上下文中放置它们的位置取决于,许多人使用“resources”文件夹来存储这些类型的文件,但有时,您可能需要一些与类的上下文相关的内容。这取决于你
这会在加载这些资源时产生问题,因为您不能再使用类似文件的方式引用它们
通常,您可以使用Class#getResource(String)
,它返回URL
或Class#getResourceAsStream(String)
,它返回InputStream
。这为您提供了加载这些嵌入式资源所需的一切
ImageIcon(String)
希望该值是一个文件引用,这意味着它不适用于嵌入式资源,但是ImageIcon
提供了一个构造函数,该构造函数将URL
作为引用,这意味着您需要使用
icon[i] = new ImageIcon(getClass().getResource(iconFiles[i]));
加载您的图像
根据您的示例,映像需要与类相关(即在与包结构相同的目录结构中)。如何实现这一点将取决于您的开发环境
请记住,您还可以指定getResource
的相对路径,甚至在某些上下文中指定绝对路径。绝对路径basic在搜索资源时将类路径的元素作为前缀添加到指定路径。我将使用操作。下面是AbstractAction
构造函数
公共抽象操作(字符串名称、图标)
-创建具有指定名称和小图标的操作
参数:
名称-操作的名称(Action.name);忽略空值
图标-操作的小图标(Action.small_图标);忽略null值
使用操作
的好处是,可以将其重新用于具有类似用途的组件。因此,假设您希望在工具栏中有一个图标按钮来打开文件,并且在JMenu
中也有一个JMenuItem
来打开文件。它们可以共享相同的操作,从而共享相同的图标、操作命令和要执行的操作
Action action = new AbstractAction("someActionCommand", someIcon) {
@Override
public void actionPerformed(ActionEvent e) {
// do something.
}
};
toolbar.add(action);
上面会自动为您放置图标,但不会放置字符串。在JMenuItem
中,它将同时放置字符串和图标
然后只需将操作添加到工具栏
更多信息请访问
要回答您真正的问题,正如@MadProgrammer所指出的,您应该使用
其中/resources/images
目录位于src
中,并且getResource()
返回URL。生成后,IDE应该将文件复制到类路径中
ProjectRoot
src
resources
images
image.png
您会发现,当使用文件系统中的文件时,在部署时将不起作用
下面是一个示例,其中JMenuItem
和JToolBar
按钮共享相同的操作。请注意,在JToolBar
中,我只需添加操作
,无需为其创建按钮。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();
}
});
}
}
+1对于行动,好主意,虽然不能完全回答缺少资源的问题……但我就是这样理解这个问题的;)@我真的需要开始读这些问题了。尤其是当他们太矮的时候。我已经习惯了略读这些长长的描述性问题。在那里,我想我是唯一一个这样做的人;)“我已经习惯于略读这些长长的描述性问题了。”哦,我比这更糟。有时我会根据题目回答问题。在回答这些问题之前,也许我应该四处看看是否有重复的那么@MadProgrammer,我算不算“描述浏览组”中的“成员”了?@AndrewThompson欢迎加入。当标题与问题不匹配时总是很好的:D
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();
}
});
}
}