Java:在调整窗口大小之前图像不会显示

发布于 2024-11-16 02:30:30 字数 1239 浏览 3 评论 0原文

我正在使用 java 进行编程,并且正在研究 GUI 和图形。在我的程序中,我将图像绘制到 JPanel 上并将 JPanel 添加到主窗口。我遇到的问题是,当我运行程序时,图像不会显示,直到我手动调整窗口大小。以下是相关代码:

绘制图像的位置:

public class painting extends JPanel{
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        g.drawImage(Toolkit.getDefaultToolkit().getImage("image.png"), 0, 0, null);
    }
}

将 JPanel 添加到 JFrame 的位置(c 是 GridBagConstraints):

public class GUI extends JFrame{
    public GUI(){
        painting Pnt = new painting();
        c.gridx = 1;    c.gridy = 0;
        c.ipadx = 540;  c.ipady = 395;
        add(Pnt, c);        
    }
}

设置窗口的位置:

public class MainC{
    public static void main (String args[]){
        GUI gui = new GUI();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.pack();
        gui.setVisible(true);
        gui.setTitle("Title");
    }
}

谢谢, Bennett

编辑:我注意到它有时会正确显示图像,但如果我关闭程序并重试,它就无法工作,直到我调整它的大小。

EDIT2:这里是文件 GUI 类MainC 类

I am programming in java and I'm getting into GUIs and Graphics. In my program I paint an image onto a JPanel and add the JPanel to the main window. The Problem I'm having is when I run the program the image doesn't show until I manually resize the window. Here is the relevant code:

Where the image is drawn:

public class painting extends JPanel{
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        g.drawImage(Toolkit.getDefaultToolkit().getImage("image.png"), 0, 0, null);
    }
}

Where JPanel is added to JFrame (c is GridBagConstraints):

public class GUI extends JFrame{
    public GUI(){
        painting Pnt = new painting();
        c.gridx = 1;    c.gridy = 0;
        c.ipadx = 540;  c.ipady = 395;
        add(Pnt, c);        
    }
}

Where The Window is set up:

public class MainC{
    public static void main (String args[]){
        GUI gui = new GUI();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.pack();
        gui.setVisible(true);
        gui.setTitle("Title");
    }
}

Thanks,
Bennett

EDIT: I noiced that it sometimes displays the image correctly but then if I close the program and try again and it doesn't work until I resize it.

EDIT2: Here are the files GUI class, MainC class

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

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

发布评论

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

评论(4

情绪操控生活 2024-11-23 02:30:30

Toolkit.getImage() 异步工作。使用 ImageIO.read() 或添加 MediaTracker

Toolkit.getImage() works asynchronously. Either use ImageIO.read() or add a MediaTracker.

绝不服输 2024-11-23 02:30:30

GUI 构造函数中,您应该先调用 super() 。

同时移动:

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("Title");

在 GUI 构造函数中。

In GUI constructor you should call super() before anything else.

Also move:

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("Title");

in GUI constructor.

就像说晚安 2024-11-23 02:30:30

您的问题一定出在代码的其他地方。我将您的三个类复制并粘贴到我的 IDE 中。我唯一改变的是在 GUI.java 中我添加了 setLayout(new GridBagLayout());GridBagConstraints c = new GridBagConstraints(); 并且我更改了图像为我自己的一张。该窗口按预期工作,并立即显示图像。

我假设您还有此处未显示的其他代码,例如我添加的 c 的初始化。检查您的其他代码以确保您最初没有重绘图像。另外,如果您发布其他代码,我可以帮助您找出问题

Your problem must be somewhere else in your code. I copied and pasted your three classes into my IDE. The only thing I changed was in GUI.java I added setLayout(new GridBagLayout()); and GridBagConstraints c = new GridBagConstraints(); and I changed the location of the image to one of my own. The window works as expected with the image displayed right away.

I assume you have additional code not displayed here, such as the initialization of c that I added. Check your other code to make sure you are not redrawing over your image initially. Also, if you post the other code I could help you identify the problem

嗳卜坏 2024-11-23 02:30:30

Toolkit.getDefaultToolkit().getImage("image.png")

会给你带来图片 ?

我尝试过:

public class Gui extends JFrame{
public Gui() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    this.setBounds(100, 100, 300, 300);
    JPanel pnl = new JPanel(){
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            this.setBackground(Color.WHITE);
            try {
                g.drawImage(new Robot().createScreenCapture(new Rectangle(90, 90)), 0, 0, null);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    };
    getContentPane().add(pnl, BorderLayout.CENTER);
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Gui().setVisible(true);
        }
    });
}

它有效。

does

Toolkit.getDefaultToolkit().getImage("image.png")

brings you the picture?

I tried it with:

public class Gui extends JFrame{
public Gui() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    this.setBounds(100, 100, 300, 300);
    JPanel pnl = new JPanel(){
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            this.setBackground(Color.WHITE);
            try {
                g.drawImage(new Robot().createScreenCapture(new Rectangle(90, 90)), 0, 0, null);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    };
    getContentPane().add(pnl, BorderLayout.CENTER);
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Gui().setVisible(true);
        }
    });
}

and it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文