JFrame 아이콘 변경 방법
이 질문에 이미 답변이 있습니다.
- 아이콘을 JFrame 11 답변 으로 설정하는 방법
나는이 JFrame
그 표시 제목 표시 줄 (왼쪽 아래)에서 Java 아이콘을. 해당 아이콘을 내 사용자 지정 아이콘으로 변경하고 싶습니다. 어떻게해야합니까?
다음 ImageIcon
과 같은 새 개체를 만듭니다 .
ImageIcon img = new ImageIcon(pathToFileOnDisk);
그런 다음으로 설정 JFrame
을 가진 setIconImage()
:
myFrame.setIconImage(img.getImage());
대신 setIconImages()
하는 체크 아웃 List
.
나를 위해 일한 대안은 다음과 같습니다.
yourFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Filepath)));
받아 들여진 답변과 매우 유사합니다.
JFrame.setIconImage(Image image)
꽤 표준입니다.
방법은 다음과 같습니다.
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);
}
}
}
}
불행히도 위의 솔루션은 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());
This did the trick in my case super
or this
referes to JFrame
in my class
URL url = getClass().getResource("gfx/hi_20px.png");
ImageIcon imgicon = new ImageIcon(url);
super.setIconImage(imgicon.getImage());
Add the following code within the constructor like so:
public Calculator() {
initComponents();
//the code to be added this.setIconImage(newImageIcon(getClass().getResource("color.png")).getImage()); }
Change "color.png" to the file name of the picture you want to insert. Drag and drop this picture onto the package (under Source Packages) of your project.
Run your project.
참고URL : https://stackoverflow.com/questions/1614772/how-to-change-jframe-icon
'program tip' 카테고리의 다른 글
하나 이상의 일치하는 빈이 발견되면 Spring은 어떻게 이름으로 자동 연결됩니까? (0) | 2020.08.19 |
---|---|
SonarQube는 Checkstyle, PMD, FindBugs를 대체합니까? (0) | 2020.08.19 |
XML 파일을 XmlDocument로 읽기 (0) | 2020.08.19 |
PHP에서 Python 스크립트 실행 (0) | 2020.08.19 |
JQuery에는 왜 모든 곳에 달러 기호가 있습니까? (0) | 2020.08.19 |