gpt4 book ai didi

Java swing改变JPanel图像

转载 作者:搜寻专家 更新时间:2023-11-01 03:12:49 28 4
gpt4 key购买 nike

我在网上看了一圈,发现how to add an image to a JPanel .

这工作正常(当图像加载到 ImagePanel 构造函数中时),但我希望能够动态更改图像(例如:按下按钮时)。我试图实现这一点,但继续获得 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException当我点击按钮时。

有人可以帮忙吗?我是 Java 的新手。谢谢!

这是我的 JPanel扩展类:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

private BufferedImage image;

public ImagePanel() {
this.loadImage("/home/raph/Images/shortcurt_unity.png");
}

public void loadImage(String filename) {
try {
this.image = ImageIO.read(new File(filename));
this.repaint();
} catch (IOException ex) {
// handle exception...
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
// Scale it by width
int scaledWidth = (int)((image.getWidth() * getHeight()/image.getHeight()));
// If the image is not off the screen horizontally...
if (scaledWidth < getWidth()) {
// Center the left and right destination x coordinates.
int leftOffset = getWidth() / 2 - scaledWidth / 2;
int rightOffset = getWidth() / 2 + scaledWidth / 2;
g.drawImage(image, leftOffset, 0, rightOffset, getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
}
// Otherwise, the image width is too much, even scaled
// So we need to center it the other direction
else {
int scaledHeight = (image.getHeight() * getWidth()) / image.getWidth();
int topOffset = getHeight() / 2 - scaledHeight / 2;
int bottomOffset = getHeight() / 2 + scaledHeight / 2;
g.drawImage(image, 0, topOffset, getWidth(), bottomOffset, 0, 0, image.getWidth(), image.getHeight(), null);
}
}
}
}

我在我的 JFrame 中初始化它:

private static ImagePanel jPanel1;

Netbeans 自动生成了以下内容(jpanel 相关代码的摘录):

javax.swing.JPanel jPanel1 = new ImagePanel();

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1260, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 124, Short.MAX_VALUE)
);

我通过按钮调用的电话:

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.loadImage("/home/raph/Images/shortcurt_unity.png");
}

请注意,在调试时,我无法进入 loadImage ,我在到达之前得到了异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at musicchecker.MusicCheckerGUI.jButton10ActionPerformed(MusicCheckerGUI.java:276)
at musicchecker.MusicCheckerGUI.access$400(MusicCheckerGUI.java:26)
at musicchecker.MusicCheckerGUI$5.actionPerformed(MusicCheckerGUI.java:124)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

最佳答案

看起来您的面板的静态引用从未设置:

private static ImagePanel jPanel1;

在没有看到所有实际代码的情况下很难确定,但看起来 netbeans 在这里创建了对不同面板的引用:

javax.swing.JPanel jPanel1 = new ImagePanel();

尝试:

jPanel1 = new ImagePanel();

关于Java swing改变JPanel图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6063914/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com