简体   繁体   English

直到将JButton悬停在面板上,它才会加载到面板上吗?

[英]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. 这是代码:我已经调用了诸如repaint()和revalidate()之类的东西,但是它们似乎都没有解决问题。 The main and the Frame are seperate classes from the StartPanel. main和Frame是与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. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 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 Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

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... 我可能建议尝试将GridLayoutEmptyBorder类的东西结合使用,例如,您应该能够接近想要的东西。

纽扣

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 有关更多详细信息,请参见如何使用GridLayout

GridBagLayout GridBagLayout

Another solution might be to use a GridBagLayout , which is more flexible, but also more complicated, for example... 另一个解决方案可能是使用GridBagLayout ,它更灵活,但也更复杂,例如...

更多按钮

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 有关更多详细信息,请参见如何使用GridBagLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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