在 Netbeans swing gui 构建器中设置 jFrame 的图标

发布于 2024-11-16 12:19:25 字数 629 浏览 2 评论 0原文

我一直在尝试在 Netbean 的 GUI 构建器中设置一个窗口,但没有成功。我尝试从我的主类访问 JFrame,如下所示:

public void run(){

    JFrame frame = new JFrame("Title of Frame");
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage("org/icon.png"));

}

它使用 icon.png 在我的主窗口之外创建了一个新框架。我想知道是否有某种方法可以访问包含其余 UI 元素的 JFrame,并设置该图标。

我也尝试过 new SearchAppUI().setIconImage(null); 这没有做任何值得注意的事情。

直接设置图标:

JFrame.setIconImage("org/icon.png"); 

给我错误,非静态方法 setIconImage(java.awt.Image) 无法从静态上下文中引用。

有什么方法可以从 Netbean 设置主 JFrame 的图标swing desinger 预览,还是来自我的 run() 方法?

I've been trying to set up a window in Netbean's GUI builder, without success. I've tried accessing the JFrame, from my main class as:

public void run(){

    JFrame frame = new JFrame("Title of Frame");
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage("org/icon.png"));

}

Which creates a new frame apart from my main window with my icon.png. I'd like to know if there is some way to gain access to the JFrame that contains the rest of my UI elements, and set that icon.

I've also tried
new SearchAppUI().setIconImage(null);
which doesn't do anything of note.

Setting the icon directly:

JFrame.setIconImage("org/icon.png"); 

gives me the error, non-static method setIconImage(java.awt.Image) cannot be referenced from a static context.

Is there any way to set the main JFrame's icon from either Netbean's swing desinger preview, or from my run() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似最初 2024-11-23 12:19:25

OP 有点过时,但仅供记录,请尝试以下操作:

frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("org/icon.png")));

The OP is a bit dated but just for the record, try this:

frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("org/icon.png")));
那小子欠揍 2024-11-23 12:19:25

NVM,我在以下位置找到了解决方案:http://www.youtube.com/watch ?v=o_35iro4b7M

描述如何设置jFrame的图标和标题。基本上,它需要
库,至少对于原型设计来说是这样。

import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Image;
import javax.imageio.ImageIO;

我现在非常想坚持使用 Netbean 的 guibuilder 的

NVM, I found a solution on: http://www.youtube.com/watch?v=o_35iro4b7M

Describing how to set the icon and title of a jFrame. Basically, it requires
the libraries

import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Image;
import javax.imageio.ImageIO;

I pretty much wanted to stick with using Netbean's guibuilder for now, at least for prototyping.

帅冕 2024-11-23 12:19:25

首先。值得阅读如何制作框架

您可以使用以下示例。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FrameWithIcon extends JFrame {

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
                    FrameWithIcon myFrame = new FrameWithIcon();
                    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    myFrame.setTitle("Frame with Icon");
                    myFrame.setLayout(new BorderLayout());
                    myFrame.setIconImage(
                        loadImageIcon("org/icon.png").getImage());

                    Dimension size = new Dimension(250, 100);
                    JPanel panel = new JPanel();
                    panel.setPreferredSize(size);

                    myFrame.add(panel, BorderLayout.LINE_START);
                    myFrame.setVisible(true);
                    myFrame.pack();
                }
            });
        } catch (InterruptedException ex) {
        } catch (InvocationTargetException ex) {
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    private static ImageIcon loadImageIcon(String path) {
        URL imgURL = FrameWithIcon.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

First of all. It's worth to read How to Make Frames.

You can use the following example.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FrameWithIcon extends JFrame {

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
                    FrameWithIcon myFrame = new FrameWithIcon();
                    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    myFrame.setTitle("Frame with Icon");
                    myFrame.setLayout(new BorderLayout());
                    myFrame.setIconImage(
                        loadImageIcon("org/icon.png").getImage());

                    Dimension size = new Dimension(250, 100);
                    JPanel panel = new JPanel();
                    panel.setPreferredSize(size);

                    myFrame.add(panel, BorderLayout.LINE_START);
                    myFrame.setVisible(true);
                    myFrame.pack();
                }
            });
        } catch (InterruptedException ex) {
        } catch (InvocationTargetException ex) {
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    private static ImageIcon loadImageIcon(String path) {
        URL imgURL = FrameWithIcon.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文