本文介绍了Java Swing GridLayout拉伸了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有以下Java代码可以生成您在图像中看到的内容.布局几乎是我想要的,除了将按钮拉伸到相等的大小和填充.无论如何,是否像其他答案一样避免使用GridBagLayout来避免这种情况?我尝试将面板的内部尺寸设置为较小,但无论如何都会拉伸.

So I have the following Java Code which produces what you see in the image. The layout is pretty much what I wanted except that the buttons are stretched to be equal size and fill. Is there anyway to avoid this without using GridBagLayout like other answers suggest? I tried setting the size of the panel inside to something smaller but it got stretched anyway.

代码:

    //Set-up main window
    JFrame mainwindow = new JFrame();
    mainwindow.setSize(500, 500);
    mainwindow.setTitle("Example");

    //Add components
    JTabbedPane tabs = new JTabbedPane();

    GridLayout experimentLayout = new GridLayout(0,2,0,5);
    JPanel pnl_Logon = new JPanel();
    pnl_Logon.setLayout(experimentLayout);
    JLabel lbl_Username = new JLabel("Username: ");
    JLabel lbl_Password = new JLabel("Password: ");
    JTextField txt_Username = new JTextField(10);
    JPasswordField psw_Password = new JPasswordField(10);
    JButton btn_Logon = new JButton("Logon");
    JButton btn_Clear = new JButton("Clear");
    pnl_Logon.add(lbl_Username);
    pnl_Logon.add(txt_Username);
    pnl_Logon.add(lbl_Password);
    pnl_Logon.add(psw_Password);
    pnl_Logon.add(btn_Logon);
    pnl_Logon.add(btn_Clear);

    tabs.addTab("Logon", pnl_Logon);

    //Draw window
    mainwindow.add(tabs);
    mainwindow.show();

结果:

推荐答案

GridLayout管理器不遵守最小值,首选值和最大值其子项的大小值. GridLayout中的所有单元格都具有相同的大小.每个单元的大小等于具有最大成分的单元的大小.它不是一个非常有用的管理器,很少在实际应用中使用.但是似乎要做的是造成混乱.

The GridLayout manager does not honour the minimum, preferred, and maximumsize values of its children. All the cells in GridLayout have the same size.The size of each cell is equal to the size of a cell with the largest component.It is not a very useful manager and it seldom can be used in real-world applications.But what it seems to do is to create confusion.

您提到要避免使用GridBagLayout管理器.但是那里还有许多其他选择.这种布局可以很容易地与更多灵活的经理.

You mentioned that you would like to avoid the GridBagLayout manager. But thereare many other options. This kind of layout can be easily accomplised with moreflexible managers.

我提供了一个MigLayout管理器的简单解决方案. MigLayout是基于网格的布局管理器,这种布局可以轻松地在几行中创建.

I provide a simple solution with a MigLayout manager. MigLayout is a grid-basedlayout manager too and this kind of layout can be easily created in a few lines.

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;



public class MigLayoutLogin extends JFrame {

    public MigLayoutLogin() {

        initUI();

        setTitle("Log in");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void initUI() {

        setLayout(new MigLayout("ins 15"));


        add(new JLabel("User name:"));
        add(new JTextField(10), "pushx, growx, wrap");
        add(new JLabel("Password: "));
        add(new JPasswordField(10), "growx, wrap");

        add(new JButton("Log in"), "span 2, split 2, center, gaptop 15");
        add(new JButton("Log out"));

        pack();
    }


    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigLayoutLogin ex = new MigLayoutLogin();
                ex.setVisible(true);
            }
        });
    }
}

我不知道您到底多么想要这种布局,所以我以自己的方式创建了它.文本字段水平增长,两个按钮水平增长与中心对齐.

I do not know how exactly you wanted this layout so I created it my way.The text fields grow horizontally and the two buttons are horizontallyaligned to the center.

这篇关于Java Swing GridLayout拉伸了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 17:05
查看更多