【问题标题】:Draw JPanel into JPanel将 JPanel 绘制到 JPanel 中
【发布时间】:2016-07-02 10:31:39
【问题描述】:

我有两个扩展 JPanel 的类:MapPanel 和 CityPanel。我正在尝试将 CityPanel 绘制到 MapPanel 中,但没有出现任何内容。我不明白为什么如果我以同样的方式添加 JButton 它将完美显示。 代码如下:

public class PanelMap extends JPanel {

    public PanelMap() {
        CityPanel city = new CityPanel();
        city.setVisible(true);
        this.add(city);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

}



public class CityPanel extends JPanel {

    private BufferedImage image;

    public CityPanel() {
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Test", 0, 0);     }

}

编辑:

我在 CityMap 中有这段代码。它显示字符串但不显示图像。

public CityPanel(String filePath, int red, int green, int blue) {
        this.image = colorImage(filePath, red, green, blue);
        this.setSize(100, 100);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 50, 50, null); 
        g.drawString("sdjkfpod", 50, 50);
    }

【问题讨论】:

  • @Override protected void paintComponent(Graphics g) { super.paintComponent(g); } 这段代码毫无意义。它所实现的只是确保该方法完全按照它在缺少覆盖的方法时会做的事情。
  • 由于CityPanel 不建议尺寸,而PanelMap 具有默认FlowLayout,因此城市面板将为0x0 像素并且不会显示。添加一个红色的LineBorder 来证明给自己看 更一般地说:为了尽快获得更好的帮助,请发布minimal reproducible exampleShort, Self Contained, Correct Example
  • 给你的内面板尺寸...
  • 嗨@AndrewThompson 你觉得我的回答有什么问题吗?
  • 嗨@Jean-BaptisteYunès 你能看看我的回答并告诉我我使用的任何不正确的信息吗?

标签: java swing jpanel


【解决方案1】:

能否请您替换 PanelMap.java 的以下构造函数:

public PanelMap() {
  CityPanel city = new CityPanel();
  city.setVisible(true);
  this.add(city);
}

通过以下构造函数:

public PanelMap() {
    String filePath = "C:\\...\\city2.png";
    CityPanel city = new CityPanel(filePath, 0, 255, 255);       
    this.setLayout(new BorderLayout());
    this.add(city, BorderLayout.CENTER);        
}

看看结果?

对您的代码进行了以下更改:

  • 语句city.setVisible(true); 被删除,因为它不是 完全需要。
  • 声明this.add(city); 确实是添加CityPanelPanelMapCityPanel 占用的空间非常小,看起来像 非常小的矩形。这就是BorderLayout 一直存在的原因 用过。

PanelMapDemo.java 之后将PanelMap 添加到JFrame 并创建一个可执行示例。

public class PanelMapDemo extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            PanelMapDemo demoFrame = new PanelMapDemo("PanelMapDemo");
            demoFrame.setVisible(true);
        }
    });
}

public PanelMapDemo(String title) {
    super(title);
    setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    add(new PanelMap());
    setSize(new java.awt.Dimension(400, 200));
    setLocationRelativeTo(null);
 }
}

在我的系统上当原始图片是:

你的MapPanel把图片改成:

希望,这会有所帮助。

【讨论】:

  • 嗨@user1315621 你看过我的回答了吗?
  • 不要忽略pack()封闭框架。
猜你喜欢
  • 1970-01-01
  • 2015-03-12
  • 2012-09-27
  • 2014-05-20
  • 2012-02-23
  • 2022-01-21
  • 2013-03-28
  • 1970-01-01
相关资源
最近更新 更多