如何设置按钮的大小? [英] How can I set size of a button?

查看:39
本文介绍了如何设置按钮的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将按钮放在带有 GridLayout 的 JPane 中.然后我将 JPanel 放入另一个带有 BoxLayout.Y_AXIS 的 JPanel 中.我希望 GridLayout 中的按钮是方形的.我使用 tmp.setSize(30,30) 但它不起作用.我也尝试使用 new GridLayout(X, Y, 4, 4) 但我不知道 X 和 Y 是什么.那么,做这些事情的正确方法是什么?

I put my buttons in a JPane with GridLayout. Then I put JPanel into another JPanel with BoxLayout.Y_AXIS. I want buttons in the GridLayout to be square. I use tmp.setSize(30,30) and it does not work. I also try to use new GridLayout(X, Y, 4, 4) but I cannot figure out what X and Y are. So, what is the correct way to do this stuff?

添加:

我仍然无法解决问题.这是我正在尝试做的代码:

I still cannot solve the problem. Here is the code of what I am trying to do:

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

public class PanelModel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Colored Trails");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        JPanel firstPanel = new JPanel();
        firstPanel.setLayout(new GridLayout(4, 4));
        JButton btn;
        for (int i=1; i<=4; i++) {
            for (int j=1; j<=4; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(100, 100));
                firstPanel.add(btn);
            }
        }

        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(5, 13));
        for (int i=1; i<=5; i++) {
            for (int j=1; j<=13; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(40, 40));
                secondPanel.add(btn);
            }
        }

        mainPanel.add(firstPanel);
        mainPanel.add(secondPanel);
        frame.add(mainPanel);

        frame.setSize(400,600);
        frame.setVisible(true);
    }
}

问题是Java试图使firstPanelsecondPanel的宽度相等!此外,Java 尝试填充窗口的所有高度.我怎样才能消除这种行为?

The problem is that Java tries to make width of the firstPanel and secondPanel equal! Moreover, Java tries to to fill all height of the window. How can I remove this behavior?

推荐答案

以下代码可以满足您的要求.只需确保分配足够的空间,以便按钮上的文本可见

The following bit of code does what you ask for. Just make sure that you assign enough space so that the text on the button becomes visible

JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,4,4,4));

for(int i=0 ; i<16 ; i++){
    JButton btn = new JButton(String.valueOf(i));
    btn.setPreferredSize(new Dimension(40, 40));
    panel.add(btn);
}
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);

X 和 Y(GridLayout 构造函数的两个第一个参数)分别指定网格中的行数和列数.如果您希望该值不受限制,您可以将其中之一保留为 0.

The X and Y (two first parameters of the GridLayout constructor) specify the number of rows and columns in the grid (respectively). You may leave one of them as 0 if you want that value to be unbounded.

编辑

我已经修改了提供的代码,我相信它现在符合预期:

I've modified the provided code and I believe it now conforms to what is desired:

JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
firstPanel.setMaximumSize(new Dimension(400, 400));
JButton btn;
for (int i=1; i<=4; i++) {
    for (int j=1; j<=4; j++) {
        btn = new JButton();
        btn.setPreferredSize(new Dimension(100, 100));
        firstPanel.add(btn);
    }
}

JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
secondPanel.setMaximumSize(new Dimension(520, 200));
for (int i=1; i<=5; i++) {
    for (int j=1; j<=13; j++) {
        btn = new JButton();
        btn.setPreferredSize(new Dimension(40, 40));
        secondPanel.add(btn);
    }
}

mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.setContentPane(mainPanel);

frame.setSize(520,600);
frame.setMinimumSize(new Dimension(520,600));
frame.setVisible(true);

基本上我现在设置面板的首选尺寸和框架的最小尺寸.

Basically I now set the preferred size of the panels and a minimum size for the frame.

这篇关于如何设置按钮的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆