简体   繁体   中英

JButtons don't load on panel until hovered over?

When this is put on a frame, the buttons don't load until they are hovered over with the mouse, and then they stay up like they should. Here's the code: I've calling things like repaint() and revalidate() but none of them seem to have fixed the problem. The main and the Frame are seperate classes from the StartPanel. Thanks!

JButton[][] levels = new JButton[3][8]; 

public StartPanel(){
    setSize(1600, 1000);
    setLayout(null);

    int count = 1;
    int yValue = 150;
    for(int r = 0; r < 3; r++){
        for(int c = 0; c < 8; c++){
            levels[r][c] = new JButton(String.valueOf(count));
            levels[r][c].setLocation(c*190 + 80, yValue);
            levels[r][c].setSize(100, 100);
            this.add(levels[r][c]);
            count++;
        }
        yValue += 200;
    }
}



public static void main(String[] args) {
    Frame f = new Frame();
    StartPanel sp = new StartPanel();
    f.add(sp);
    f.setVisible(true);
}

public Frame() {
    setSize(1600, 1000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setResizable(false);
}

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

GridLayout

I might suggest trying GridLayout , in combination with something like an EmptyBorder , you should be able to get close to what you want, for example...

纽扣

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        JButton[][] levels = new JButton[3][8];

        public TestPane() {
            setBorder(new EmptyBorder(40, 40, 40, 40));
            setSize(1600, 1000);
            setLayout(new GridLayout(0, 8, 40, 40));

            int count = 1;
            int yValue = 150;
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 8; c++) {
                    levels[r][c] = new JButton(String.valueOf(count));
                    levels[r][c].setMargin(new Insets(50, 50, 50, 50));
                    this.add(levels[r][c]);
                    count++;
                }
                yValue += 200;
            }
        }
    }

}

See How to Use GridLayout for more details

GridBagLayout

Another solution might be to use a GridBagLayout , which is more flexible, but also more complicated, for example...

更多按钮

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        JButton[][] levels = new JButton[3][8];

        public TestPane() {
            setSize(1600, 1000);
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(25, 25, 25, 25);
            gbc.fill = GridBagConstraints.BOTH;

            int count = 1;
            for (int r = 0; r < 3; r++) {
                gbc.gridx = 0;
                for (int c = 0; c < 8; c++) {
                    levels[r][c] = new JButton(String.valueOf(count));
                    levels[r][c].setMargin(new Insets(50, 50, 50, 50));
                    this.add(levels[r][c], gbc);
                    gbc.gridx++;
                    count++;
                }
                gbc.gridy++;
            }
        }
    }

}

See How to Use GridBagLayout for more details

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