나는 아주 간단한 문제라고 생각했던 것들을 약간 가지고있다.커스텀 JComponent가 JPanel에 추가되지 않음
사용자 정의 JComponent Ball
은 단순한 타원에 불과합니다. JFrame의 기본 콘텐츠 창으로 설정 한 JPanel에 ball을 추가하려고합니다 (frame.setContentPane (mainPanel)을 통해). 공과 함께, Paddle
클래스의 두 개의 다른 구성 요소, 내가 작성한 또 다른 사용자 정의 JComponent가 있습니다. 이상하게도, 패들은 잘 추가 될 수 있습니다.
문제의 패널은 요소를 배치하기 위해 의도적으로 절대 레이아웃을 사용합니다. 이는 이것이 Pong 게임의 단순한 레크리에이션이라고 생각 되었기 때문입니다.
볼과 양쪽 패들을 JPanel에 추가 한 후 간단한 테스트를 실행 한 결과, 구성 요소 배열이 나열된 2 개만 반환 된 것으로 나타났습니다 : 패들. 그들 모두를 추가하고 초기화하는 전체 코드는 JPanel을하고 JFrame의는 다음과 같다 :
public Pong()
{
frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel(null);
mainPanel.setBackground(Color.black);
mainPanel.setPreferredSize(new Dimension(500, 500));
player = new Paddle(20, 100);
enemy = new Paddle(20, 100);
ball = new Ball(5, Color.GREEN);
mainPanel.add(player);
mainPanel.add(enemy);
mainPanel.add(ball);
enemy.setColor(Color.RED);
frame.setContentPane(mainPanel);
frame.pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = screen.width/2 - frame.getWidth()/2;
int y = screen.height/2 - frame.getHeight()/2;
frame.setLocation(x, y);
mainPanel.addKeyListener(mc);
frame.setVisible(true);
mainPanel.repaint();
x = mainPanel.getWidth()/2 - ball.getWidth()/2;
y = mainPanel.getHeight()/2 - ball.getHeight()/2;
System.out.println("X: " + x + ", Y: " + y);
ball.setLocation(x, y);
x = 0;
y = mainPanel.getHeight()/2 - player.getHeight()/2;
System.out.println("X: " + x + ", Y: " + y);
player.setLocation(x, y);
x = mainPanel.getWidth() - enemy.getWidth();
y = mainPanel.getHeight()/2 - enemy.getHeight()/2;
System.out.println("X: " + x + ", Y: " + y);
enemy.setLocation(x, y);
System.out.println(Arrays.deepToString(mainPanel.getComponents()));
mainPanel.repaint();
frame.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
mainPanel.requestFocus();
}
});
dm.start();
mainPanel.requestFocus();
}
[다음과 같이 공의 코드는 참고 :이 클래스 Pong
의 내부 클래스이기 때문에 공은 비공개입니다 이 프로젝트에서 작업 할 때 나는이 문제가 발생했습니다 처음이 아니다
private class Ball extends JComponent
{
private int radius;
private Color color;
public Ball(int size, Color c)
{
if(size > 0)
radius = size;
else
radius = 1;
this.setPreferredSize(new Dimension(radius, radius));
color = c;
}
public Ball(int size)
{
this(size, Color.RED);
}
//Getters and Setters...
@Override
public void paintComponent(Graphics g)
{
System.out.println("Radius: " + radius);
g.setColor(color);
g.fillOval(0, 0, radius, radius);
}
}
, 그것은 항상 내가 절대 위치와 함께 사용자 정의 JComponent의를 사용하고 때 발생하는 것 같습니다 : 이는] 아무것도 확장하지 않습니다 JPanel에서 여전히 다른 프로젝트에서도이 문제가있는 것 같지 않습니다. 내가 간과하고있는 것은 매우 단순한 무언가 일지 모르지만 나는 그것이 무엇인지 생각할 수 없다. 이것에 대한 답변은 크게 감사하겠습니다.
꼭 필요한 경우 all the lines of code for this project can be found here (총 300 줄이 조금 넘음).
미리 도움을 청하십시오!
내가 얘기를 깜빡 했네요 (의견, 감사를 기반으로 편집, 내 스윙 녹슨입니다). 알 수없는 변수를 정리하면됩니다. – Aargonian
당신은'Ball.paintComponent()'안에서'super.paintComponent (g)'를 호출하는 것을 잊어 버렸습니다. – dic19
나는 노를 위해 그것도하지 않았다. 나는 그것을 고칠 것이다. 그러나 그것은 문제의 원인으로 보이지 않는다. – Aargonian