이 JPanel에서 JButton "BackToTheMenu"는 패널 상단에 있으며 클릭 할 수있는 곳을 알고 있지만 볼 수는 없습니다. 그것을 클릭하면 다음 패널로 완벽하게 이동합니다. 어떻게 나타나야합니까!? 도움말 정말 고맙습니다!내 JButton이 전혀 표시되지 않지만 계속 작동하는 이유는 무엇입니까? (내가 그것을 클릭 할 수있는 곳을 알고 있습니다.)
public class GridPanel extends JPanel implements ActionListener
{
Ball ball = new Ball();
Timer timer = new Timer(14, this);
JButton backToTheMenu = new JButton("To the Menu");
public GridPanel()
{
setBackground(Color.WHITE);
setPreferredSize(new Dimension(900, 710));
add(ball);
backToTheMenu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
ProjectileGame.gridButtonPressed();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(0, 649, 30, 33);
g.setColor(Color.BLUE);
for (int i = 0; i < 35; i++)
{
g.setColor(Color.GREEN);
g.drawLine(0, (20+(30*i)), 900, (20+(30*i)));
g.drawLine((30+(30*i)), 0, (30+(30*i)), 1000);
}
ball.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(0, 650, 900, 650);
g.drawLine(30, 0, 30, 1000);
Graphics2D g2d1 = (Graphics2D)g;
g.setColor(Color.BLACK);
g2d1.drawString("X Displacement (metres)", 400, 667);
AffineTransform at = new AffineTransform();
at.setToRotation(Math.PI/-1.97);
g2d1.setTransform(at);
g2d1.drawString("Y Displacement (metres)", -380, 8);
setOpaque(false);
for (Component child : getComponents())
{
child.repaint();
}
}
public void actionPerformed(ActionEvent e)
{
ball.ballPhysics();
repaint();
timer.restart();
}
}
1), 게시 A [MCVE] 또는 [짧은, 콘도, 올바른 예 (http://www.sscce.org /). 2)'backToTheMenu'는 절대로 패널에 추가되지 않습니다. –
'public void paintComponent (Graphics g) { super.paintComponent (g); ..이 스 니펫의 마지막 줄은 달성한다 .. for (Component child : getComponents()) { child.repaint(); }'..이 스 니펫. –
'ProjectileGame.gridButtonPressed()'가 다른 곳에서 호출되지 않았습니까? 또한'actionPerformed()'에 몇몇 디버그 라인을 추가 할 수 있습니까? 단지 그 동작이 버튼에서 수행되는지 확인하기 위해서입니까? – Arvind