Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a class named BoardGUI extended from JFrame, in a constructor I have made a JPanel with two buttons in it. I have added this panel into my frame. Whenever I run this program, buttons get invisible. As I bring my mouse cursor over the buttons they get visible. Code is as follows:

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

public BoardGUI() {
    pieces = new Play();
    setTitle("Checkers Game");
    setSize(645, 700);
    setVisible(true);

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

I am also using repaint method in my program. Can anybody point out my mistake and suggest any solution for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

I have a class named BoardGUI extended from JFrame, in a constructor i have made a JPanel with two buttons in it. I have added this panel into my frame. Whenever i run this program, buttons get invisible. As i bring my mouse cursor over the buttons they get visible.

  • setVisible(true); should be last code line in constructor, because you added JComponents to the already visible JFrame,

  • or to call revalidate() and repaint() in the case that JComponents are added to visible Swing GUI

  • there no reason to call a.setVisible(true); or r.setVisible(true); for standard JComponents, because JComponents are visible(true) by default in comparing with Top Level Containers, there you need to call JFrame/JDialog/JWindow.setVisible(true);

EDIT

(i used the very first suggestion you gave. problem remains the same.) - for example

enter image description here

from code

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyGridLayout {

    private JFrame frame = new JFrame("GridLayout, JButtons, etc... ");
    private JPanel panel = new JPanel(new GridLayout(8, 8));

    public MyGridLayout() {
        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                JButton button = new JButton("(" + (row + 1) + " / " + (col + 1) + ")");
                button.putClientProperty("column", col);
                button.putClientProperty("row", row);
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        System.out.println(
                                "clicked column : "
                                + btn.getClientProperty("column")
                                + ", row : " + btn.getClientProperty("row"));
                    }
                });
                panel.add(button);
            }
        }
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyGridLayout myGridLayout = new MyGridLayout();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...