簡體   English   中英

如何在 JPanel 的所有組件上繪制一些東西

[英]How to draw something over all components of a JPanel

如何在面板的所有組件上繪制一些東西?

在下面的代碼中,我嘗試覆蓋paintComponent方法來執行此操作,我調用super.paintComponent(g)希望這將繪制作為構造函數一部分添加的所有組件,但我的X 仍然保持在圖像下方。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Stack extends JPanel {

    private final JLabel some_text = new JLabel("Some very long text that will be covered", JLabel.LEFT);
    private final JLabel some_icon = new JLabel((Icon)null, JLabel.CENTER);
    public static final String COMPASS_URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Compass_Rose_English_North.svg/240px-Compass_Rose_English_North.svg.png";

    public Stack() throws IOException {
        super(new GridBagLayout());

        URL compassUrl = new URL(COMPASS_URL);
        BufferedImage compassImage = ImageIO.read(compassUrl);
        ImageIcon compassIcon = new ImageIcon(compassImage);
        some_icon.setIcon(compassIcon);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;

        add(some_icon, c);

        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        add(some_text, c);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setColor(Color.RED);
        g2.setStroke(new BasicStroke(15));
        g2.drawLine(0, 0, this.getWidth(), this.getHeight());
        g2.drawLine(this.getWidth(), 0, 0, this.getHeight());
        g2.dispose();
    }

    private static void createAndShowUI() throws IOException {
        JFrame frame = new JFrame("MyFrame");
        frame.getContentPane().add(new Stack());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowUI();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

有沒有辦法將我的 X 帶到前台或等待super.paintComponent(g)繪制所有內容,然后運行繪制 X 的代碼?

謝謝,羅伯托

覆蓋 JPanel 的paint(…)方法:

protected void paint(Graphics g)
{
    super.paint(g);

    // add custom painting code here
}

paint(…)方法調用paintChildren(…)方法,因此您的自定義代碼將在面板上的所有組件都繪制完成后執行。

有關詳細信息,請參閱深入了解繪畫機制

嘗試這個。 我進行了以下更改。

  • paintComponentsetIcon圖像,而不是在setIcon
  • 定位圖像使其居中。
  • setPreferredSize用於實際面板大小(設置框架大小包括可能不是您想要的邊框)。
  • paintComponent使用RenderingHints來平滑十字的邊緣。
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Stack2 extends JPanel {

    private final JLabel some_text = new JLabel(
            "Some very long text that will be covered", JLabel.LEFT);
    private final JLabel some_icon =
            new JLabel((Icon) null, JLabel.CENTER);
    public static final String COMPASS_URL =
            "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Compass_Rose_English_North.svg/240px-Compass_Rose_English_North.svg.png";
    BufferedImage compassImage;

    public Stack2() throws IOException {
        super(new GridBagLayout());

        URL compassUrl = new URL(COMPASS_URL);
        compassImage = ImageIO.read(compassUrl);
//        ImageIcon compassIcon = new ImageIcon(compassImage);
//        some_icon.setIcon(compassIcon);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;

        add(some_icon, c);

        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        add(some_text, c);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(compassImage,
                (400 - compassImage.getWidth()) / 2,
                (400 - compassImage.getHeight()) / 2, null);
        g2.setColor(Color.RED);
        g2.setStroke(new BasicStroke(15));
        g2.drawLine(0, 0, this.getWidth(), this.getHeight());
        g2.drawLine(this.getWidth(), 0, 0, this.getHeight());
        g2.dispose();
    }

    private static void createAndShowUI() throws IOException {
        JFrame frame = new JFrame("MyFrame");
        Stack2 panel = new Stack2();
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setPreferredSize(new Dimension(400, 400));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowUI();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM