JPLayout中的GridLayout中的组件错误地填充了网格 [英] Components in GridLayout wit JPanel fills the grid incorrectly

查看:74
本文介绍了JPLayout中的GridLayout中的组件错误地填充了网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图防止JPanel中的GridLayout完全填充单元格并忽略组件的任何setSize

I am trying to prevent the GridLayout in a JPanel from filling the cells entirely and ignoring any setSize of the components

我正在使用以下代码:

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

public class GrideComponents{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Laying Out Components in a Grid");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(5,2,0,0));
    panel.add(new JLabel("Enter name"));
    JTextField a = new JTextField(5);
    a.setSize(10, 10);
    panel.add(a);
    panel.add(new JLabel("Enter Roll"));
    panel.add(new JTextField(3));
    panel.add(new JLabel("Enter Class"));
    panel.add(new JTextField(3));
    panel.add(new JLabel("Enter Total Marks"));
    panel.add(new JTextField(3));
    panel.add(new JButton("Ok"));
    panel.add(new JButton("Cancel"));
    frame.add(panel);
    frame.setSize(400,400);
    frame.setVisible(true);
  }
}

每个组件都在填充gris单元,而不是指定的大小.

And every component is filling the gris cell instead of being in the size specified.

谢谢

推荐答案

阅读使用布局管理器,以便您了解布局管理器的工作原理.

Read the Swing tutorial on Using Layout Managers so you understand the basics of how layout managers work.

GridLayout将所有组件的大小调整为相同的大小.

A GridLayout resizes all components to the same size.

创建表单时,通常将使用不同的布局管理器嵌套不同的面板以获得所需的效果.

When creating a form you will generally nest different panels using different layout managers to get the effect you desire.

在这种情况下,您可以对按钮使用FlowLayout.然后,您可以将GridBagLayout或SpringLayout用于其他组件.

In this case you might use a FlowLayout for the buttons. Then you might use a GridBagLayout or SpringLayout for the other components.

这篇关于JPLayout中的GridLayout中的组件错误地填充了网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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