わかりましたCanvas
(実装できるのでこれを使用していますBufferedStrategy
)。
私はCanvas
自分のJFrame
contentPaneに自分を追加し、それよりも自分のglassPaneHUDPanel
に追加します(これは、ヘルスバーとチャカラバーJPanel
を表示するために、他の2つJPanel
のs(JProgressBar
sとsを含む)で構成されます) 。JLabel
JFrame
HUDのすべてのコンポーネントは不透明です。
ここで、 contentPaneとしてaを使用しHUDPanel
てsglassPaneにを追加すると、コンポーネントを保持する2つのJFrame
sが不透明にペイントされません。Canvas
JPanel
しかし、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);
}
}
}