【发布时间】:2010-12-09 13:02:25
【问题描述】:
我有一个JFrame,它在标题栏(左角)上显示一个 Java 图标。
我想将该图标更改为我的自定义图标。我该怎么做?
【问题讨论】:
-
我敢打赌,在大多数情况下,这些人甚至还没有听说过 API。在这种情况下,最好的解决方案可能是提供带有答案的 API 链接。
我有一个JFrame,它在标题栏(左角)上显示一个 Java 图标。
我想将该图标更改为我的自定义图标。我该怎么做?
【问题讨论】:
JFrame.setIconImage(Image image) 非常标准。
【讨论】:
像这样创建一个新的ImageIcon 对象:
ImageIcon img = new ImageIcon(pathToFileOnDisk);
然后将其设置为您的JFrame 和setIconImage():
myFrame.setIconImage(img.getImage());
另外结帐setIconImages() 取而代之的是List。
【讨论】:
这是我的做法:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class MainFrame implements ActionListener{
/**
*
*/
/**
* @param args
*/
public static void main(String[] args) {
String appdata = System.getenv("APPDATA");
String iconPath = appdata + "\\JAPP_icon.png";
File icon = new File(iconPath);
if(!icon.exists()){
FileDownloaderNEW fd = new FileDownloaderNEW();
fd.download("http://icons.iconarchive.com/icons/artua/mac/512/Setting-icon.png", iconPath, false, false);
}
JFrame frm = new JFrame("Test");
ImageIcon imgicon = new ImageIcon(iconPath);
JButton bttn = new JButton("Kill");
MainFrame frame = new MainFrame();
bttn.addActionListener(frame);
frm.add(bttn);
frm.setIconImage(imgicon.getImage());
frm.setSize(100, 100);
frm.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
这里是下载器:
import java.awt.GridLayout;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class FileDownloaderNEW extends JFrame {
private static final long serialVersionUID = 1L;
public static void download(String a1, String a2, boolean showUI, boolean exit)
throws Exception
{
String site = a1;
String filename = a2;
JFrame frm = new JFrame("Download Progress");
JProgressBar current = new JProgressBar(0, 100);
JProgressBar DownloadProg = new JProgressBar(0, 100);
JLabel downloadSize = new JLabel();
current.setSize(50, 50);
current.setValue(43);
current.setStringPainted(true);
frm.add(downloadSize);
frm.add(current);
frm.add(DownloadProg);
frm.setVisible(showUI);
frm.setLayout(new GridLayout(1, 3, 5, 5));
frm.pack();
frm.setDefaultCloseOperation(3);
try
{
URL url = new URL(site);
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead = 0.0F;
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
FileOutputStream fos = new FileOutputStream(filename);
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int i = 0;
while ((i = in.read(data, 0, 1024)) >= 0)
{
totalDataRead += i;
float prog = 100.0F - totalDataRead * 100.0F / filesize;
DownloadProg.setValue((int)prog);
bout.write(data, 0, i);
float Percent = totalDataRead * 100.0F / filesize;
current.setValue((int)Percent);
double kbSize = filesize / 1000;
String unit = "kb";
double Size;
if (kbSize > 999.0D) {
Size = kbSize / 1000.0D;
unit = "mb";
} else {
Size = kbSize;
}
downloadSize.setText("Filesize: " + Double.toString(Size) + unit);
}
bout.close();
in.close();
System.out.println("Took " + System.nanoTime() / 1000000000L / 10000L + " seconds");
}
catch (Exception e)
{
JOptionPane.showConfirmDialog(
null, e.getMessage(), "Error",
-1);
} finally {
if(exit = true){
System.exit(128);
}
}
}
}
【讨论】:
imgicon.getImage() 是我需要的谢谢
这是一个对我有用的替代方案:
yourFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Filepath)));
这与接受的答案非常相似。
【讨论】:
getClass().getResource("/myimage.png")(不要忘记前导“/”!)
很遗憾,上述解决方案不适用于 Jython Fiji 插件。我不得不使用 getProperty 来动态构造相对路径。
这对我有用:
import java.lang.System.getProperty;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
frame = JFrame("Test")
icon = ImageIcon(getProperty('fiji.dir') + '/path/relative2Fiji/icon.png')
frame.setIconImage(icon.getImage());
frame.setVisible(True)
【讨论】:
只需添加以下代码:
setIconImage(new ImageIcon(PathOfFile).getImage());
【讨论】:
这在我的情况下起到了作用,super 或 this 在我的课堂上引用了JFrame
URL url = getClass().getResource("gfx/hi_20px.png");
ImageIcon imgicon = new ImageIcon(url);
super.setIconImage(imgicon.getImage());
【讨论】:
在构造函数中添加如下代码:
public Calculator() {
initComponents();
//the code to be added this.setIconImage(newImageIcon(getClass().getResource("color.png")).getImage()); }
将“color.png”更改为要插入的图片的文件名。 将此图片拖放到项目的包中(在 Source Packages 下)。
运行您的项目。
【讨论】: