gpt4 book ai didi

java - Swing JLayeredPane 未在调用 PaintAll 时绘制所有元素

转载 作者:行者123 更新时间:2023-12-02 04:05:38 26 4
gpt4 key购买 nike

我有 JLayeredPane,其中包含 0 级的 Canvas(在 Paint 方法中填充自身黄色)和 JPanel > 在第 1 级(在构造函数中将其背景设置为红色)。

paintAllToImage 方法中单击按钮时,我创建 BufferedImage 并使用 component.paintAll(image.getGraphics) 在此图像上绘制 JLayerePane ());
问题是,该图像仅绘制了 Canvas(它完全填充为黄色)。请参阅附图。

enter image description here

(按钮上面的部分是实际绘制的,按钮下面的部分是图像,从 JLayeredPane 创建)

完整代码如下:

public class LayeredPaneEx extends JPanel {

private JLayeredPane layeredPane;
public LayeredPaneEx() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
layeredPane.setLayout(null);

Canvas panel = new CustomCanvas();
panel.setSize(300, 400);
CustomPanel customPanel = new CustomPanel();
layeredPane.add(panel, new Integer(0));
layeredPane.add(customPanel, new Integer(1));

add(layeredPane);

JButton paintBtn = new JButton("Paint All");
paintBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ImageIcon icon = new ImageIcon(paintAllToImage(layeredPane));
JLabel imageLabel = new JLabel(icon);
add(imageLabel);
}
});
add(paintBtn);

JLabel paintLabel = new JLabel();
paintLabel.setPreferredSize(new Dimension(300, 300));
}

private class CustomCanvas extends Canvas {
@Override
public void paint(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(0, 0, getWidth(), getHeight());
}
}

private class CustomPanel extends JPanel {
CustomPanel() {
setSize(100, 100);
setBackground(Color.RED);
}
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("LayeredPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane = new LayeredPaneEx();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

public static BufferedImage paintAllToImage(Component component) {
BufferedImage image;
image = new BufferedImage(
component.getWidth(),
component.getHeight(),
BufferedImage.TYPE_INT_RGB
);
component.paintAll(image.getGraphics());
return image;
}
}

最佳答案

编辑:新答案

通过调整此Stack Overflow answer ,似乎可以将轻量级 CustomPanel 放入重量级 Panel 中,并将它们放在另一个重量级 Panel 之上。这是屏幕截图:

Screenshot showing lightweight on top of heavyweight

这是程序:

import java.awt.*;
import javax.swing.*;

/**
* Adapted from https://stackoverflow.com/a/1428298/1694043.
*/
public class GuiTest {
public static void main(String[] arguments) {
new GuiTest();
}

public GuiTest() {
JFrame frame = new JFrame("Heavyweight versus lightweight");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addPanelsToFrame(frame);

SwingUtilities.invokeLater(() -> frame.setVisible(true));
}

private void addPanelsToFrame(JFrame frame) {
CustomCanvas customCanvas = new CustomCanvas(300, 400, Color.YELLOW);
Panel awtPanel1 = new Panel(new BorderLayout());
awtPanel1.setSize(300, 400);
awtPanel1.add(customCanvas, BorderLayout.CENTER);
frame.getLayeredPane().add(awtPanel1, JLayeredPane.DEFAULT_LAYER);

CustomPanel customPanel = new CustomPanel(100, 100, Color.RED);
Panel awtPanel2 = new Panel(new BorderLayout());
awtPanel2.setSize(100, 100);
awtPanel2.add(customPanel, BorderLayout.CENTER);
frame.getLayeredPane().add(awtPanel2, JLayeredPane.PALETTE_LAYER);
}


private class CustomCanvas extends Canvas {
private Color backgroundColor;

public CustomCanvas(int width, int height, Color backgroundColor) {
setSize(width, height);
this.backgroundColor = backgroundColor;
}

@Override
public void paint(Graphics g) {
g.setColor(backgroundColor);
g.fillRect(0, 0, getWidth(), getHeight());
}
}


private class CustomPanel extends JPanel {
public CustomPanel(int width, int height, Color backgroundColor) {
setSize(width, height);
setBackground(backgroundColor);
}
}
}
<小时/>

旧答案

使用 MadProgrammer 的建议为了避免使用 Canvas 类,您可以使用 CustomPanel 类的两个实例。该类扩展了基于 Swing 的轻量级 JPanel,而不是基于重量级 AWT 的 Canvas。请参阅https://stackoverflow.com/a/13769255/1694043有关轻量级与重量级 Java GUI 组件的更多信息。

这是屏幕截图:

Screenshot of modified program

修改后的代码如下:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

public class LayeredPaneEx extends JPanel {
private JLayeredPane layeredPane;

public LayeredPaneEx() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
layeredPane.setLayout(null);

//Canvas panel = new CustomCanvas();
//panel.setSize(300, 400);
//CustomPanel customPanel = new CustomPanel();
//layeredPane.add(panel, new Integer(0));
//layeredPane.add(customPanel, new Integer(1));
layeredPane.add(new CustomPanel(300, 400, Color.YELLOW), new Integer(0));
layeredPane.add(new CustomPanel(100, 100, Color.RED), new Integer(1));

add(layeredPane);

JButton paintBtn = new JButton("Paint All");
paintBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ImageIcon icon = new ImageIcon(paintAllToImage(layeredPane));
JLabel imageLabel = new JLabel(icon);
add(imageLabel);
}
});
add(paintBtn);

JLabel paintLabel = new JLabel();
paintLabel.setPreferredSize(new Dimension(300, 300));
}

// private class CustomCanvas extends Canvas {
// @Override
// public void paint(Graphics g) {
// g.setColor(Color.YELLOW);
// g.fillRect(0, 0, getWidth(), getHeight());
// }
// }

private class CustomPanel extends JPanel {
public CustomPanel(int width, int height, Color backgroundColor) {
setSize(width, height);
setBackground(backgroundColor);
}
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("LayeredPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane = new LayeredPaneEx();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

public BufferedImage paintAllToImage(Component component) {
BufferedImage image = new BufferedImage(
component.getWidth(),
component.getHeight(),
BufferedImage.TYPE_INT_RGB
);
component.paintAll(image.getGraphics());

return image;
}
}

关于java - Swing JLayeredPane 未在调用 PaintAll 时绘制所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34306688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com