JButton不会在面板上加载直到盘旋?

时间:2015-03-03 23:02:48

标签: java swing jbutton layout-manager null-layout-manager

当它放在一个框架上时,按钮在用鼠标悬停之前不会加载,然后它们会保持原样。这是代码: 我调用了像repaint()和revalidate()这样的东西,但它们似乎都没有解决问题。 main和Frame是StartPanel中的单独类。谢谢!

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

1 个答案:

答案 0 :(得分:2)

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正

GridLayout的

我可能会建议GridLayoutEmptyBorder结合使用,你应该能够接近你想要的东西,例如......

Buttons

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

}

有关详细信息,请参阅How to Use GridLayout

的GridBagLayout

另一种解决方案可能是使用GridBagLayout,它更灵活,但也更复杂,例如......

More Buttons

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++;
            }
        }
    }

}

有关详细信息,请参阅How to Use GridBagLayout