我有一个JFrame
,在标题栏(左上角)显示Java图标。 我想将该图标更改为我的自定义图标。我应该怎么做?如何更改JFrame图标
回答
创建一个新的ImageIcon
对象是这样的:
ImageIcon img = new ImageIcon(pathToFileOnDisk);
然后将其设置为你的JFrame
与setIconImage()
:
myFrame.setIconImage(img.getImage());
也会检出setIconImages()
,这需要List
代替。
JFrame.setIconImage(Image image)
非常标准。
这些解决方案不起作用。 – 2013-05-11 20:26:38
这是我如何做到这一点:
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()'是什么,我需要感谢 – shareef 2016-05-14 07:55:19
这里是为我工作的一种选择:
yourFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Filepath)));
它非常类似于到被接受的答案。
,回答了如何使用图像,如果它是一个资源的唯一一个。 :D – 2016-08-16 13:50:09
不幸的是,上面的解决方案并不适用于Jython斐济插件。我必须使用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(新的ImageIcon(PathOfFile).getImage());
这奏效了我的情况super
或this
referes到JFrame
在我的课
URL url = getClass().getResource("gfx/hi_20px.png");
ImageIcon imgicon = new ImageIcon(url);
super.setIconImage(imgicon.getImage());
你并不需要在你的情况下,超级本或实施。你可以放弃它。 – creativecreatorormaybenot 2016-07-02 19:18:57
可能你是对的这取决于你的产业设计 – shareef 2016-07-02 19:25:44
如果扩展类是不是真的有必要。 – creativecreatorormaybenot 2016-07-02 19:40:38
在构造函数中添加以下代码,像这样:
public Calculator() {
initComponents();
//the code to be added this.setIconImage(newImageIcon(getClass().getResource("color.png")).getImage()); }
更改 “color.png”添加到要插入的图片的文件名。 将此图片拖放到项目的包装(源包装下)。
运行您的项目。
- 1. 如何更改JFrame中的java图标
- 2. Swing:更改jframe的图标
- 3. 如何更改JFrame标题的字体?
- 4. 如何从JPanel swing更改JFrame标签
- 5. NullPointerException当试图更改JFrame的标题
- 6. 更改Java JFrame的图块
- 7. 如何放置JFrame图标
- 8. 如何更改pygame图标?
- 9. 如何更改Firefox图标?
- 10. 如何更改鼠标光标图标?
- 11. 从其他类更改JFrame的标题
- 12. 如何更改JFrame内部的JPanel?
- 13. 如何更改JFrame的形状?
- 14. 如何更改JFrame的背景颜色
- 15. 如何更改JFrame中JLabel的位置?
- 16. 如何更改jframe的背景色
- 17. 如何更改NetBeans中的jFrame名称
- 18. 如何使JRadioButtons更改属性(JFrame)?
- 19. 如何动态更改JFrame背景?
- 20. 如何更改标记图标?
- 21. 如何更改标题图标?
- 22. 如何更改标题栏图标?
- 23. 如何更改cpanel图标的标签?
- 24. 如何更改openlayers标记图标?
- 25. 如何更改鼠标上的图标?
- 26. 试图从线程更改JFrame的标题?
- 27. 更改jframe中图标的大小和形状
- 28. 如何更改图标图片
- 29. 如何更改UItabbar图标图像?
- 30. 如何使用JButton在JFrame中更改背景图像?
我敢打赌,在大多数情况下,这些人甚至还没有听说过API。像这样的情况下,最好的解决方案可能是提供一个链接到API与答案。 – Carlos 2009-10-24 23:54:51