导出 Eclipse JAR 时未格式化的图像
Unformatted image when exporting Eclipse JAR
导出 - Runnable Jar 时,框架的图像会丢失其格式。看图中Screen 1是eclipse中直接执行的window,Screen 2是Jar的执行。
enter image description here
插入图片的代码。
JLabel lblImage = new JLabel();
lblImage.setBorder(BorderFactory.createLineBorder(Color.BLACK));
lblImage.setBounds(225, 25, 225, 225);
lblImage.setIcon(new ImageIcon("logo.png"));
由 windows 构建器自动生成的用于将 lblimagem 添加到框架的代码:
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(lblImage, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
.addGap(38)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblTextApresentation, GroupLayout.PREFERRED_SIZE, 424, GroupLayout.PREFERRED_SIZE)
.addComponent(lblVersions)
.addComponent(cbVersions, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(199)
.addComponent(lblTsvizzevolution, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
new ImageIcon("logo.png")
根据 javadoc,该参数称为 filename
。
关注其中的 'file' 部分:jar 文件中的条目不是文件,因此,您想要的是 不可能 使用此构造函数。你必须使用其他东西。
您正在寻找的是 getResource 系统:它要求 java 从 java 加载您的 class 文件的同一位置为您提供内容。即使那是在一个罐子里。
URL url = MyClass.class.getResource("logo.png");
new ImageIcon(url);
就是你所需要的。 getResource 的参数通常与 MyClass.class 可能所在的位置有关。如果您想相对于 jar 的 'root',请改用 /logo.png
。这在开发期间也有效,并且在您部署这些东西时也有效。不管你怎么部署。
导出 - Runnable Jar 时,框架的图像会丢失其格式。看图中Screen 1是eclipse中直接执行的window,Screen 2是Jar的执行。 enter image description here
插入图片的代码。
JLabel lblImage = new JLabel();
lblImage.setBorder(BorderFactory.createLineBorder(Color.BLACK));
lblImage.setBounds(225, 25, 225, 225);
lblImage.setIcon(new ImageIcon("logo.png"));
由 windows 构建器自动生成的用于将 lblimagem 添加到框架的代码:
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(lblImage, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
.addGap(38)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblTextApresentation, GroupLayout.PREFERRED_SIZE, 424, GroupLayout.PREFERRED_SIZE)
.addComponent(lblVersions)
.addComponent(cbVersions, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(199)
.addComponent(lblTsvizzevolution, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
new ImageIcon("logo.png")
根据 javadoc,该参数称为 filename
。
关注其中的 'file' 部分:jar 文件中的条目不是文件,因此,您想要的是 不可能 使用此构造函数。你必须使用其他东西。
您正在寻找的是 getResource 系统:它要求 java 从 java 加载您的 class 文件的同一位置为您提供内容。即使那是在一个罐子里。
URL url = MyClass.class.getResource("logo.png");
new ImageIcon(url);
就是你所需要的。 getResource 的参数通常与 MyClass.class 可能所在的位置有关。如果您想相对于 jar 的 'root',请改用 /logo.png
。这在开发期间也有效,并且在您部署这些东西时也有效。不管你怎么部署。