我的问题是如何将图像显示到JPanel中?这里提出类似问题的其他主题并不清楚,我怎么能这样做。
我的项目文件夹中有一个目录,其中包含我想要在JPanel中显示的图像文件Project Folder/GUI/img
,特别是gray.png
和green.png
。
我尝试使用我在其他帖子中找到的ImageIcon
和JLabel
使用以下代码:
ImageIcon image = new ImageIcon("GUI/img/gray.png");
JLabel label = new JLabel(image);
//JPanel panel is already initialized by the IDE
panel.add(label)
但不起作用...... JPanel
保持空白而不显示任何图像。我怎么能这样做?
除此之外,我希望JPanel
内的图片在执行某些操作时更改(gray.png
green.png
),例如按下按钮。我可以通过相同的方法将其归档,以便在JPanel
右边显示图片吗?
提前致谢!
编辑:以下是获取此测试代码的示例。初始化由IDE自动完成。
import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Sample extends javax.swing.JFrame {
public Sample() {
initComponents();
}
//Initialization
private void initComponents() {
PanelImage = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
javax.swing.GroupLayout PanelImageLayout = new javax.swing.GroupLayout(PanelImage);
PanelImage.setLayout(PanelImageLayout);
PanelImageLayout.setHorizontalGroup(
PanelImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
PanelImageLayout.setVerticalGroup(
PanelImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(PanelImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(239, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(PanelImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(155, Short.MAX_VALUE))
);
pack();
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
DisplayImage(PanelImage, "/GUI/img/gray.png");
} catch (Exception ex) {
Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
}
}
//For display the url image in a JPanel
private void DisplayImage(JPanel jp, String url) throws IOException, Exception {
try {
Image image=ImageIO.read(this.getClass().getResource(url));
ImageIcon imageicon=new ImageIcon(image);
JLabel label=new JLabel(imageicon);
jp.add(label);
} catch (IOException ex) {
throw new IOException();
} catch (Exception ex) {
throw new Exception();
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sample().setVisible(true);
}
});
}
// Variables declaration
private javax.swing.JPanel PanelImage;
}
private void DisplayImage(JPanel jp, String url)
是String url
JPanel jp
显示图片所需的工作
答案 0 :(得分:2)
在Google中查找代码片段我最终得到了一个解决方案......并且正在应用与之前评论相同的模式。 给我解决方案的代码是:
label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/gray.png")))
然后,我制作了我想要实现的方法:
private static void DisplayImage(JPanel jp, String url) {
JLabel jl=new JLabel();
jl.setIcon(new javax.swing.ImageIcon(getClass().getResource(url)));
jp.add(jl);
}
也许这不是完美和最正确的解决方案,但对我来说是完美的,这就是我想要的。
感谢大家的答案和建议!
答案 1 :(得分:1)
当我在创建扫雷时,我遇到了类似的问题。我最终找到了一个解决方案并通过首先加载一个Image然后从该Image创建一个ImageIcon来使其工作。
Image myImage = ImageIO.read(getClass().getResource("image_path.jpg"));
myImage = myImage.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
ImageIcon myImageIcon = new ImageIcon(myImage);
答案 2 :(得分:1)
你必须使用ImageIcon(this.getClass()。getResource(" /Gui/img/gray.png"));
这是其工作的例子。
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ExImage extends JPanel
{
public ExImage()
{
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/gray.png"));
JLabel label = new JLabel(imageIcon);
add(label);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.add(new ExImage());
frame.setVisible(true);
frame.setPreferredSize(new Dimension(200, 300));
}
}