简体   繁体   中英

Why does my JButton not show up at all but still work? (I know where it is so I can click it)

In this JPanel, my JButton "BackToTheMenu" is at the top of the panel and I know where it is so I can click it, but I cannot see it. When I click it, it takes me to the next panel perfectly. How do I get it to show up!? Help is really appreciated!

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

A lot of your code is incorrect.

A painting method is for painting only. You should not:

  1. Invoke ball.paintComponent(). The panel will automatically paint any component added to it.

  2. Get the child component and invoke repaint() on them. Again, The panel will paint all child components.

  3. Invoke setOpaque(...) . The opaque property should be set in the constructor of the class.

The "back to the menu" button should not be defined in this class. It should be defined in the parent class.

So the code should look something like:

JButton back = new JButton("Back to the Menu");
back.addActionListener(...);
frame.add(back, BorderLayout.PAGE_START);
JPanel grid = new GridPanel();
frame.add(grid, BorderLayout.CENTER);

I'm not sure if you have this elsewhere in the code, but I don't see you adding backToTheMenu. If this is your code:

public GridPanel()
{
    setBackground(Color.WHITE);
    setPreferredSize(new Dimension(900, 710));
    add(ball);

    backToTheMenu.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {       
            ProjectileGame.gridButtonPressed();
        }
    });
}

You need this:

add(backToTheMenu);

But disregard this if you add it elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM