Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing只绘制两个JPanel中的一个?_Java_Swing_Jframe_Jpanel - Fatal编程技术网

Java Swing只绘制两个JPanel中的一个?

Java Swing只绘制两个JPanel中的一个?,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,为了实习,我正在用Java构建一个显示API。加载一个图像是可行的,但只要我想添加另一个图像,第一个图像就会消失,只绘制第二个图像。有没有办法解决这个问题 主要类别: package com.lespaul.display; import java.awt.Graphics; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowCo

为了实习,我正在用Java构建一个显示API。加载一个图像是可行的,但只要我想添加另一个图像,第一个图像就会消失,只绘制第二个图像。有没有办法解决这个问题

主要类别:

package com.lespaul.display;

import java.awt.Graphics;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Window extends JFrame {
    private static final long serialVersionUID = 3716315131567381715L;

    public Window() {
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(200, 200);
        this.setVisible(true);
        Image pane = new Image("floor.png");
        Image pane2 = new Image("floor.png");
        this.add(pane);
        this.add(pane2);
        pane2.setPosition(50,50);
        this.revalidate();

    }

    public static void main(String[] args){
        Window window = new Window();
    }
}
这是真实的图像

package com.lespaul.display;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.io.IOException;
import java.net.URL;

import com.lespaul.position.*;

public class Image extends JPanel {
    private static final long serialVersionUID = -5815516814733234713L;
    public BufferedImage image;
    public Vector2 position = new Vector2(0, 0);

    public Image() {

    }
    public Image(String ref){
        try {
            this.addImage(ref);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void setPosition(int x, int y){
        Dimension size = this.getPreferredSize();
        Insets insets = this.getInsets();
        this.setBounds(x+insets.left,y+insets.top,size.width,size.height);
        this.position.x = x;
        this.position.y = y;
        repaint();
    }
    public void addImage(String fileName) throws IOException {
        URL url = this.getClass().getClassLoader().getResource(fileName);
        image = ImageIO.read(url);

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null)
            g.drawImage(image,(int)position.x,(int)position.y, null);
    }
}    
最佳做法是用于管理组件位置

祝你好运

使用任何布局,如

    jFrame.getContentPane().add(jPanel1,BorderLayout.NORTH);
    jFrame.getContentPane().add(jPanel2,BorderLayout.SOUTH);

不要称你的班级形象。有一个叫这个名字的Java接口,这会让你的代码很混乱

如果要在不同位置绘制多个图像,常用的解决方案是跟踪ArrayList中的图像,然后创建一个自定义组件,该组件在ArrayList中循环以在指定位置绘制图像


请查看一个示例。
DrawOnComponent
示例将告诉您如何实现这种方法。

问题是,我需要绝对地做这类事情,而不是布局……这个答案应该是注释,注释是错误的建议,应该删除。仍然不起作用。这只是把图像放在顶部的某个地方。我需要它在一个绝对的位置1)为了更好的帮助更快,张贴一个(最小的完整和可验证的例子)。2) 例如,获取图像的一种方法是热链接到中看到的图像。“我需要它处于绝对位置”绝对位置在哪里?javagui可能必须在许多平台上工作,在不同的屏幕分辨率上&使用不同的plaf。因此,它们不利于部件的精确放置。为了组织一个健壮的GUI组件,可以使用布局管理器,或者,与布局填充和边框一起使用。@andrewhompson,有时候手指只是不键入大脑在想什么:)至少大脑在想什么。我读了很多东西,不知道作者是否也是如此……)