Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以强制JButton始终为正方形?_Java_Swing_Layout_Jbutton_Layout Manager - Fatal编程技术网

Java 是否可以强制JButton始终为正方形?

Java 是否可以强制JButton始终为正方形?,java,swing,layout,jbutton,layout-manager,Java,Swing,Layout,Jbutton,Layout Manager,我正在编写一个扫雷舰克隆,我现在的问题是网格上按钮的比例——我想让用户随意调整窗口大小,但我希望按钮保持1:1的宽度/高度比例不变,而不是在某些情况下变成煎饼或监狱酒吧:)有什么办法可以轻松地强制jb按钮是否始终保持方形而不提供固定的宽度和高度?您可以编写自定义的布局管理器,例如网格布局扩展名并设置子项大小 import javax.swing.*; import java.awt.*; public class SquareButtonsTestApp extends JFrame {

我正在编写一个扫雷舰克隆,我现在的问题是网格上按钮的比例——我想让用户随意调整窗口大小,但我希望按钮保持1:1的宽度/高度比例不变,而不是在某些情况下变成煎饼或监狱酒吧:)有什么办法可以轻松地强制jb按钮是否始终保持方形而不提供固定的宽度和高度?

您可以编写自定义的
布局管理器
,例如
网格布局
扩展名并设置子项大小

import javax.swing.*;
import java.awt.*;

public class SquareButtonsTestApp extends JFrame {

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

    public SquareButtonsTestApp() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new MyGridLayout(10, 20));
        for (int i=0; i<10; i++) {
            for (int j=0; j<20; j++) {
                getContentPane().add(new JButton(" "));
            }
        }
        setSize(800,600);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    class MyGridLayout extends GridLayout {
        public MyGridLayout(int rows, int cols) {
            super(rows, cols);
        }
        public void layoutContainer(Container parent) {
            synchronized (parent.getTreeLock()) {
                Insets insets = parent.getInsets();
                int ncomponents = parent.getComponentCount();
                int nrows = getRows();
                int ncols = getColumns();
                boolean ltr = parent.getComponentOrientation().isLeftToRight();

                if (ncomponents == 0) {
                    return;
                }
                if (nrows > 0) {
                    ncols = (ncomponents + nrows - 1) / nrows;
                } else {
                    nrows = (ncomponents + ncols - 1) / ncols;
                }
                // 4370316. To position components in the center we should:
                // 1. get an amount of extra space within Container
                // 2. incorporate half of that value to the left/top position
                // Note that we use trancating division for widthOnComponent
                // The reminder goes to extraWidthAvailable
                int totalGapsWidth = (ncols - 1) * getHgap();
                int widthWOInsets = parent.getWidth() - (insets.left + insets.right);
                int widthOnComponent = (widthWOInsets - totalGapsWidth) / ncols;
                int extraWidthAvailable = (widthWOInsets - (widthOnComponent * ncols + totalGapsWidth)) / 2;

                int totalGapsHeight = (nrows - 1) * getVgap();
                int heightWOInsets = parent.getHeight() - (insets.top + insets.bottom);
                int heightOnComponent = (heightWOInsets - totalGapsHeight) / nrows;
                int extraHeightAvailable = (heightWOInsets - (heightOnComponent * nrows + totalGapsHeight)) / 2;

                int size=Math.min(widthOnComponent, heightOnComponent);
                widthOnComponent=size;
                heightOnComponent=size;
                if (ltr) {
                    for (int c = 0, x = insets.left + extraWidthAvailable; c < ncols ; c++, x += widthOnComponent + getHgap()) {
                        for (int r = 0, y = insets.top + extraHeightAvailable; r < nrows ; r++, y += heightOnComponent + getVgap()) {
                            int i = r * ncols + c;
                            if (i < ncomponents) {
                                parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
                            }
                        }
                    }
                } else {
                    for (int c = 0, x = (parent.getWidth() - insets.right - widthOnComponent) - extraWidthAvailable; c < ncols ; c++, x -= widthOnComponent + getHgap()) {
                        for (int r = 0, y = insets.top + extraHeightAvailable; r < nrows ; r++, y += heightOnComponent + getVgap()) {
                            int i = r * ncols + c;
                            if (i < ncomponents) {
                                parent.getComponent(i).setBounds(x, y, widthOnComponent, heightOnComponent);
                            }
                        }
                    }
                }
            }
        }
    }
}
import javax.swing.*;
导入java.awt.*;
公共类SquareButtonsTestApp扩展了JFrame{
公共静态void main(字符串[]args){
新的SquareButtonsTestApp();
}
public SquareButtonsTestApp(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(新的MyGridLayout(10,20));

对于(int i=0;我不确定在您的情况下是否可以接受,但我想您可以将JButton子类化并覆盖大小修改方法?@nClass子类化的原因是错误的(并且没有必要)-大小调整是LayoutManager@kleopatra感谢您的更正并指出这一点:)