1

わかりましたCanvas(実装できるのでこれを使用していますBufferedStrategy)。

私はCanvas自分のJFrame contentPaneに自分を追加し、それよりも自分のglassPaneHUDPanelに追加します(これは、ヘルスバーとチャカラバーJPanelを表示するために、他の2つJPanelのs(JProgressBarsとsを含む)で構成されます) 。JLabelJFrame

HUDのすべてのコンポーネントは不透明です。

ここで、 contentPaneとしてaを使用しHUDPanelsglassPaneにを追加すると、コンポーネントを保持する2つのJFramesが不透明にペイントされません。CanvasJPanel

ここに画像の説明を入力してください

しかし、contentPaneJPanelに代わりにCanvasを追加すると、すべて正常に機能します。JFrame

ここに画像の説明を入力してください

どうしてこれなの?どうすれば修正できますか?

前もって感謝します

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);
        }
    }
}
4

1 に答える 1

2
  • GlassPane軽量で、常に遅れていますAWT Canvas

  • JLayeredPaneこれは、GlassPaneJavaの非常に一般的な問題であり3D、これらのAPIはに基づいています。CAD/CAMOpen GL/ClAWT Containers

  • JLayer(軽量コンテナ)を使用しても同じ問題が発生しますが、そこisLightWe....()からピアNative OS やそのリソースをテストできます

于 2012-11-15T14:23:49.903 に答える