java math reminder_java – 是否可以强制JButton始终是正方形?

您可以编写自定义LayoutManager,例如GridLayout扩展并设置子项大小.

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

}

}

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值