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