我正在使用两个带有cardLayout 的JPanel,它们都被添加到contentPane 上。每个面板上都有一个 JButton,可切换到另一个面板,但在我的 gridPanel 上,其上有图形,它们似乎覆盖了 JButton,尽管当您单击其位置时它仍然有效,但它是不可见的。如何获得一个既有图形又有 JButton 的 JPanel?非常感谢您的帮助
主类:
public class ProjectileGame extends JPanel
{
private static JFrame frame = new JFrame("MyGame");
private static JPanel panelContent = new JPanel();
MenuPanel menuPanel = new MenuPanel();
GridPanel gridPanel = new GridPanel();
Ball ball = new Ball();
static CardLayout card = new CardLayout();
public ProjectileGame()
{
frame.setTitle("ProjectileGame");
frame.add(panelContent);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setSize(900, 700);
panelContent.setLayout(card);
panelContent.add(menuPanel, "1");
panelContent.add(gridPanel, "2");
card.show(panelContent, "1"); //First JPanel to be shown is that of the main menu.
frame.setVisible(true);
}
public static void menuButtonPressed()
{
card.show(panelContent, "2");
}
public static void gridButtonPressed()
{
card.show(panelContent, "1");
}
public static void main(String args[])
{
new ProjectileGame();
}
}
gridPanel 类:
public class GridPanel extends JPanel implements ActionListener
{
private static final Graphics2D g2 = null;
Ball ball = new Ball();
JButton backToTheMenu = new JButton("To the Menu");
Timer timer = new Timer(14, this);
public GridPanel()
{
setOpaque(true);
setBackground(Color.WHITE);
setPreferredSize(new Dimension(900, 710));
add(ball);
add(backToTheMenu);
backToTheMenu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
ProjectileGame.gridButtonPressed();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d1 = (Graphics2D)g;
ball.paintComponent(g2d1);
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);
}
g.setColor(Color.RED);
g.drawLine(0, 650, 900, 650);
g.drawLine(30, 0, 30, 1000);
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);
}
public void actionPerformed(ActionEvent e)
{
ball.ballPhysics();
repaint();
timer.restart();
}
}
最佳答案
不要向传递给 paintComponent()
方法的 Graphics
对象添加变换和旋转等。这个 Graphics
对象在绘制时会传递给所有其他组件。
相反,您应该创建一个临时Graphics
对象来进行绘画:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
// do custom painting
g2d.dispose();
}
现在您需要能够向面板添加多个组件。请注意,您始终可以将另一个面板添加到一个面板中,这样您就可以拥有无限的面板嵌套级别。因此,您可以轻松地为 CardLayout 创建两张“卡片”,每张卡片都添加了不同数量的组件。
基础知识是这样的:
JButton button1 = new JButton("Back to Card2")
JPanel gridPanel = new GridPanel();
JPanel card1 = new JPanel(new BorderLayout());
card1.add(button1, BorderLayout.PAGE_START);
card1.add(gridPanel, BorderLayout.CENTER);
JButton button2 = new JButton("Back to Card1");
JPanel card2 = new JPanel();
card2.add(button2);
JPanel cardPanel = new JPanel();
cardPanel.setLayout( new CardLayout(...) );
cardPanel.add(card1, ...);
cardPanel.add(card2, ...);
frame.add(cardPanel);
关于java - 为什么我的paintComponent 方法会在我的JButton 上进行绘制?如何使 JPanel 上同时具有 JButton 和图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128902/