gpt4 book ai didi

java - 更改Java图标和工具栏图标

转载 作者:行者123 更新时间:2023-12-02 06:42:23 26 4
gpt4 key购买 nike

我正在尝试使用此更改 Java 图标和标题栏图标

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.util.Collections;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

@SuppressWarnings("all")
public class GameFrame extends JFrame
{
JFrame frame = new JFrame();
String title = Config.clientName + " Revision: " + Config.Revision + " Made by: " + Config.developerName;
String betaTitle = Config.clientName + " Beta "+ "Revision: " + Config.Revision + " Made by: " + Config.developerName;
String alphaTitle = Config.clientName + " Alpha "+ "Revision: " + Config.Revision + " Made by: " + Config.developerName;

public GameFrame(GameShell rsapplet, int width, int height, boolean undecorative, boolean resizable) {
rsApplet = rsapplet;
//Config.ConfigLoad();
if (Config.beta == 3){
setTitle(alphaTitle);
} else {
setTitle(Config.beta == 1 ? title:betaTitle);
}

setIconImage(getToolkit().getImage(getClass().getResource(Signlink.findcachedir()+"/Sprites/Icons/icon.jpeg")));

// //String imgURL = signlink.spritesLocation() + "this.jpg";
// try {
// setIconImage(new ImageIcon(imgURL).getImage());
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
setUndecorated(undecorative);
setResizable(resizable);
setVisible(true);
Insets insets = this.getInsets();
setSize(width + insets.left + insets.right, height + insets.top + insets.bottom);//28
setLocation((screenWidth - width) / 2, (screenHeight - height) / 2);
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
requestFocus();
toFront();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.BLACK);
}

public int getFrameWidth() {
Insets insets = this.getInsets();
return getWidth() - (insets.left + insets.right);
}

public int getFrameHeight() {
Insets insets = this.getInsets();
return getHeight() - (insets.top + insets.bottom);
}

public GameFrame(GameShell rsapplet, int width, int height) {
this(rsapplet, width, height, false,false);
}

public Graphics getGraphics() {
Graphics g = super.getGraphics();
Insets insets = this.getInsets();
g.translate(insets.left ,insets.top);
return g;
}

public void update(Graphics g)
{
rsApplet.update(g);
}

public void paint(Graphics g)
{
rsApplet.paint(g);
}

private final GameShell rsApplet;
public Toolkit toolkit = Toolkit.getDefaultToolkit();
public Dimension screenSize = toolkit.getScreenSize();
public int screenWidth = (int)screenSize.getWidth();
public int screenHeight = (int)screenSize.getHeight();
}

但是当我运行客户端时出现此错误,我做错了什么?

[CLIENT]: Client frame initialized... Uncaught error fetching image: java.lang.NullPointerException at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115) at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

最佳答案

而不是使用...

setIconImage(getToolkit().getImage(getClass().getResource(Signlink.findcachedir()+"/Sprites/Icons/icon.jpeg")));

尝试使用...

setIconImage(getToolkit().getImage(Signlink.findcachedir()+"/Sprites/Icons/icon.jpeg"));

这会将String视为文件引用。类加载器不太可能解析您提供的路径。

您最好尝试使用 ImageIO 读取图像,因为出现问题时它会抛出 IOException

参见Reading/Loading an Image了解更多详情

根据评论更新

BufferedImage image = null; 
try {
image = ImageIO.read(new File(Signlink.findcachedir()+"/Sprites/Icons/icon.jpeg"));
} catch (IOException e) {
e.printStackTrace();
}
frame.setIconImage(image);
frame.setVisible(true);

关于java - 更改Java图标和工具栏图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19018218/

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