0

I have a class BoardGUI that extends JFrame. I have added buttons in a JPanel. When I try to add the panel into the frame with a mouselistoner on the frame, the buttons (undo and replay) become invisible. When I mouse over the buttons, they become visible.

Here is my code:

public class BoardGUI extends JFrame {
    JButton a=new JButton("Undo");
    JButton r=new JButton("replay");
    JPanel jp=new JPanel();

    public BoardGUI() {

        // TODO Auto-generated constructor stub
        setTitle("Checkers Game");
        setSize(645, 700);

        jp.setLayout(new FlowLayout());
        jp.setPreferredSize(new Dimension(645,35));
        a.setVisible(true);
        r.setVisible(true);
        jp.add(a);
        jp.add(r);
        add(jp,BorderLayout.SOUTH);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent arg0) {
                // TODO Auto-generated method stub
                repaint();

            }
        });


    }
    public void paint(Graphics g)
    {
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                g.fillRect(i*100, j*100, 100, 100);
            }
        }
    }
}

Can anybody help me to fix this please?

mKorbel
  • 109,525
  • 20
  • 134
  • 319

3 Answers3

4
  • override getPreferredSize for JPanel, then to call JFrame.pack() instead of any sizing

  • don't to set PreferredSize

  • don't to override paint for JFrame, override paintComponent for (another, separate) JPanel, put this JPanel to the JFrames CENTER area

.

.

.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BoardGUI extends JFrame {

   private JButton a = new JButton("Undo");
   private  JButton r = new JButton("replay");
   private  JPanel jp = new JPanel();

    public BoardGUI() {
        setTitle("Checkers Game");
        jp.setLayout(new FlowLayout());
        jp = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(new Dimension(645, 35));
            }

           /* @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                //
            }*/
        };
        jp.add(a);
        jp.add(r);
        add(jp, BorderLayout.SOUTH);
        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BoardGUI();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • what does this method do? and how it will fix the problem? –  Jan 14 '14 at 13:00
  • @Wasiq Noor please which one you meant – mKorbel Jan 14 '14 at 13:01
  • @mKorbel paintComponent() –  Jan 14 '14 at 13:13
  • it piece of code is working. but as i said i am very beginner to java programming, can u please upload any even simple code of it so that i can understand it more thoroughly? –  Jan 14 '14 at 13:15
  • please to read [Oracle tutorial Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html), rest of you can see in [posts by (leader here) @MadProgrammer](http://stackoverflow.com/search?tab=newest&q=user%3a992484%20paintcomponent) – mKorbel Jan 14 '14 at 13:16
0

You've got a 645 x 700 JFrame onto which you paint an 800 x 800 checkerboard. It's probably overwriting the buttons.

Put the checkerboard in its own JPanel, and draw within that panel only. Put that panel in the center of the JFrame.

arcy
  • 12,845
  • 12
  • 58
  • 103
0

Do not use:

a.setVisible(true);
r.setVisible(true);

NO need to use this. When you make frame visible using setVisible(true), all components will get painted.

Rahul
  • 3,479
  • 3
  • 16
  • 28