gpt4 book ai didi

java - 为什么 Canvas 作为 contentPane 不绘制添加到 JFrame glasspane 的不透明组件

转载 作者:行者123 更新时间:2023-11-30 04:36:26 32 4
gpt4 key购买 nike

好吧,我有一个 Canvas (我使用它是因为它可以实现 BufferedStrategy)。

我将我的 Canvas 添加到我的 JFrame contentPane 并添加 HUDPanel (这是一个 JPanel 由另外 2 个 JPanel 组成 - 带有 JProgressBarJLabel - 用于显示健康状况和chakara 条)到我的 JFrameglassPane

HUD 上的所有组件都是不透明的。

现在,当我将 HUDPanel 添加到我的 JFrameglassPane 并使用 Canvas 作为其 contentPane 容纳组件的 2 个 JPanel 未绘制为不透明:

enter image description here

但是如果我将 JPanel 而不是 Canvas 添加到 JFrame contentPane 则一切正常:

enter image description here

这是为什么呢?我该如何解决它?

提前谢谢

HUDPanelTest.java

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class HUDPanelTest {

public HUDPanelTest() {
initComponents();
}

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

private void initComponents() {
JFrame frame = new JFrame() {
@Override
public Dimension getPreferredSize() {//testing only
return new Dimension(800, 600);
}
};
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

HUDPanel hudPanel = new HUDPanel();
frame.setGlassPane(hudPanel);
JPanel p = new JPanel() {
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.setColor(Color.yellow);
grphcs.fillRect(0, 0, getWidth(), getHeight());
}
};
Canvas c = new Canvas() {
@Override
public void paint(Graphics grphcs) {//for testing ONLY I actually use a BufferStrategy
super.paint(grphcs);

grphcs.setColor(Color.yellow);
grphcs.fillRect(0, 0, getWidth(), getHeight());
}
};

frame.add(c);
//frame.add(p);//this works as expected

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

}
}

HUDPanel.java

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class HUDPanel extends JPanel {

HUDPanel.HealthPanel healthPanel;
HUDPanel.ChakaraPanel chakaraPanel;

public HUDPanel() {
super(new BorderLayout(), true);
setOpaque(false);

healthPanel = new HUDPanel.HealthPanel();
chakaraPanel = new HUDPanel.ChakaraPanel();

initComponents();

setHealthPlayer1(100);
setHealthPlayer2(100);
setChakaraPlayer1(0);
setChakaraPlayer2(0);
setLabelTextHealthPlayer1("Player 1 (Health):");
setLabelTextHealthPlayer2("Player 2 (Health):");
setLabelTextChakaraPlayer1("Player 1 (Chakara):");
setLabelTextChakaraPlayer2("Player 2 (Chakara):");
}

private void initComponents() {
add(healthPanel, BorderLayout.NORTH);
add(chakaraPanel, BorderLayout.SOUTH);
}

public void setHealthPlayer1(int health) {
healthPanel.setPlayer1ProgressBarValue(health);
}

public void setHealthPlayer2(int health) {
healthPanel.setPlayer2ProgressBarValue(health);
}

public void setChakaraPlayer1(int chakara) {
chakaraPanel.setPlayer1ProgressBarValue(chakara);
}

public void setChakaraPlayer2(int chakara) {
chakaraPanel.setPlayer2ProgressBarValue(chakara);
}

public void setLabelTextHealthPlayer1(String text) {
healthPanel.setPlayer1LabelText(text);
}

public void setLabelTextHealthPlayer2(String text) {
healthPanel.setPlayer2LabelText(text);
}

public void setLabelTextChakaraPlayer1(String text) {
chakaraPanel.setPlayer1LabelText(text);
}

public void setLabelTextChakaraPlayer2(String text) {
chakaraPanel.setPlayer2LabelText(text);
}

private class HealthPanel extends JPanel {

JProgressBar player1ProgressBar = new JProgressBar();
JProgressBar player2ProgressBar = new JProgressBar();
JLabel player1Label = new JLabel();
JLabel player2Label = new JLabel();

public HealthPanel() {
super(new GridBagLayout(), true);
setOpaque(false);

initComponents();
}

private void initComponents() {
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.WEST;
gc.insets = new Insets(10, 10, 10, 10);
//or else the anchoring wont work
gc.weightx = 1;

//gc.gridx = 0;//does not seem to make a difference
//gc.gridy = 0;
add(player1Label, gc);

//gc.gridx = 1;
//gc.gridy = 0;
add(player1ProgressBar, gc);

gc.anchor = GridBagConstraints.EAST;

//gc.gridx = 2;
//gc.gridy = 0;
add(player2Label, gc);

//gc.gridx = 3;
//gc.gridy = 0;
add(player2ProgressBar, gc);
}

public void setPlayer1ProgressBarValue(int val) {
player1ProgressBar.setValue(val);
}

public void setPlayer2ProgressBarValue(int val) {
player2ProgressBar.setValue(val);
}

public void setPlayer2LabelText(String text) {
player2Label.setText(text);
}

public void setPlayer1LabelText(String text) {
player1Label.setText(text);
}
}

private class ChakaraPanel extends JPanel {

JProgressBar player1ProgressBar = new JProgressBar();
JProgressBar player2ProgressBar = new JProgressBar();
JLabel player1Label = new JLabel();
JLabel player2Label = new JLabel();

public ChakaraPanel() {
super(new GridBagLayout(), true);
setOpaque(false);

initComponents();
}

private void initComponents() {
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.WEST;
gc.insets = new Insets(10, 10, 10, 10);
//or else the anchoring wont work
gc.weightx = 1;
gc.weighty = 1;

//gc.gridx = 0;
//gc.gridy = 0;
add(player1Label, gc);

//gc.gridx = 1;
//gc.gridy = 0;
add(player1ProgressBar, gc);

gc.anchor = GridBagConstraints.EAST;

//gc.gridx = 2;
//gc.gridy = 0;
add(player2Label, gc);

//gc.gridx = 3;
//gc.gridy = 0;
add(player2ProgressBar, gc);
}

public void setPlayer1ProgressBarValue(int val) {
player1ProgressBar.setValue(val);
}

public void setPlayer2ProgressBarValue(int val) {
player2ProgressBar.setValue(val);
}

public void setPlayer2LabelText(String text) {
player2Label.setText(text);
}

public void setPlayer1LabelText(String text) {
player1Label.setText(text);
}
}
}

最佳答案

  • GlassPane是轻量级的,那么总是落后于AWT Canvas

  • 对于 JLayeredPaneGlassPane 以及 Java 和 3DCAD/CAM 来说,这是相当常见的问题>、Open GL/Cl,这些API基于AWT容器

  • 使用 JLayer(轻量级容器)也会出现同样的问题,但您可以在其中测试 isLightWe....() 来自 的对等点> native 操作系统和/或其资源

关于java - 为什么 Canvas 作为 contentPane 不绘制添加到 JFrame glasspane 的不透明组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13399072/

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