如何在JPanel的所有组件上绘制一些东西

  • 本文关键字:绘制 组件 JPanel java swing
  • 更新时间 :
  • 英文 :


如何在面板的所有组件上绘制一些东西?

在下面的代码中,我试图重写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的代码?

谢谢,Roberto

覆盖JPanel:的paint(…)方法

protected void paint(Graphics g)
{
super.paint(g);
// add custom painting code here
}

paint(…)方法调用paintChildren(…)方法,因此您的自定义代码将在面板上的所有组件绘制完成后执行。

有关详细信息,请参见"绘制机制"。

试试这个。我做了以下更改。

  • 绘制paintComponent中的图像,而不是setIcon中的图像
  • 定位图像使其居中
  • 实际面板大小使用setPreferredSize(设置边框大小包括边框,这可能不是您想要的(
  • 在CCD_ 11中使用CCD_
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();
}
}
});
}
}

最新更新