简体   繁体   English

带有Java Swing for Desktop的自定义工具栏

[英]Custom ToolBar with Java Swing for Desktop

I have created a GUI with Java Swing and wanting to create a custom toolbar according to my modules. 我已经使用Java Swing创建了一个GUI,并希望根据我的模块创建一个自定义工具栏。 Below are the images am wanting to use: 以下是想要使用的图像:

在此处输入图片说明

These images are placed in the same level as the src folder within my application. 这些图像与应用程序中的src文件夹位于同一级别。 I am aware that I can perhaps create a jar with these images so that I can easily access them from within my application but do not know how. 我知道我也许可以用这些图像创建一个jar,以便可以从应用程序中轻松访问它们,但不知道如何操作。 I have spent hours trying to make this work. 我已经花了几个小时试图使这项工作。

Below is my GUI that I have created ad wanting to beautify with these images for the toolbar else create an array of labels that will act as a navigation but either approach I couldn't get it to work. 下面是我创建的广告的GUI,该广告要使用工具栏的这些图片进行美化,还创建了一个标签数组,这些标签将用作导航,但是两种方法都无法使其正常工作。

在此处输入图片说明

The code below was my last attempt on this: 下面的代码是我对此的最后尝试:

 JToolBar toolbar1 = new JToolBar();

 ImageIcon client = new ImageIcon("clients.png");
 ImageIcon timesheet = new ImageIcon("timesheets.png");

 JButton clientTB = new JButton(client);
 JButton timesheetTB = new JButton(timesheet);

 toolbar1.add(clientTB );
 toolbar1.add(timesheetTB);

 add(toolbar1, BorderLayout.NORTH);

I even moved these images and placed them within the class that's calling them. 我什至移动了这些图像并将它们放置在称为它们的班级中。

What could I be doing wrong, please help? 我可能做错了什么,请帮忙?

You have a look at the JavaDocs for ImageIcon(String) , the String value is "a String specifying a filename or path" 您可以查看JavaDocs for ImageIcon(String) ,该String值为“指定文件名或路径的字符串”

This is a problem, because your images aren't actually files, any more, they have been embedded within your application (typically within the resulting jar file) and no longer be treated like "normal files". 这是一个问题,因为您的图像实际上不再是文件,它们已经被嵌入到您的应用程序中(通常在生成的jar文件中),并且不再被视为“普通文件”。

Instead, you need to use Class#getResource which searches the application's classpath for the named resource, something like... 相反,您需要使用Class#getResource ,它在应用程序的类路径中搜索命名资源,例如...

// This assumes that the images are in the default package 
// (or the root of the src directory)
ImageIcon client = new ImageIcon(getClass().getResource("/clients.png"));

Now, I have a personal dislike for ImageIcon , because it won't tell you when the image is loaded for some reason, like it can't be found or it's the wrong format. 现在,我个人不喜欢ImageIcon ,因为它不会告诉您由于某种原因(例如找不到图像或格式错误)何时加载图像。

Instead, I'd use ImageIO to read the image 相反,我将使用ImageIO读取图像

ImageIcon client = new ImageIcon(ImageIO.read(getClass().getResource("/clients.png")));

which will do two things, first, it will throw a IOException if the image can't be loaded for some reason and two, it won't return until the image is fully loaded, which is helpful. 这将做两件事,首先,如果由于某种原因无法加载图像,它将抛出IOException ,直到图像完全加载,它才返回。

See Reading/Loading an Image for more details 有关更多详细信息,请参见读取/加载图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM