java社长组件大小,JFrame组件的大小和位置

本文探讨了在Java Swing中使用GridBagLayout布局管理器时遇到的问题,包括如何统一按钮尺寸和调整组件间距。建议使用GridBagConstraints的fill属性来填充组件,并通过设置Insets调整边距。同时,提出了将导航控件移到单独面板以简化复杂布局的设计思路。
摘要由CSDN通过智能技术生成

i am learning GridBagLayout and did very simple layout that is attached,There are two very small confusions

1- Size of Buttons i have used New.setPreferredSize(new Dimension(70,23)); is it standard way to make all the buttons same size

2-Placing of components does not look good

KXPSV.png

now see there is much more padding on all sides of components , so how to put it in right way from top left corner , should i decrease size of JFrame? or use frame.pack(); both work but dont know what is standard practice (i have tried pagestart etc) ,

this is how my code looks

frame= new JFrame("Hello ");

frame.setSize(300, 150);

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

frame.addWindowListener(this);

frame.setResizable(false);

frame.setLocation(0, 0);

p1= new JPanel (new GridBagLayout());

gbc = new GridBagConstraints();

lblname= new JLabel("Name");

gbc.gridx=0;

gbc.gridy=0;

p1.add(lblname, gbc);

textname = new JTextField(11);

gbc.gridx=1;

gbc.gridy=0 ;

p1.add(textname, gbc);

New= new JButton("New");

gbc.gridx=2;

gbc.gridy=0;

New.setPreferredSize(new Dimension(70,23));

p1.add(New, gbc);

lblEmail = new JLabel("Email ");

gbc.gridx=0;

gbc.gridy=1;

p1.add(lblEmail , gbc);

TextEmail = new JTextField(11);

gbc.gridx=1;

gbc.gridy=1;

p1.add(TextEmail,gbc);

Edit = new JButton("Edit") ;

gbc.gridx= 2 ;

gbc.gridy=1;

Edit.setPreferredSize(new Dimension(70,23));

p1.add(Edit , gbc);

lblgender= new JLabel("Gender");

gbc.gridx=0;

gbc.gridy=2;

p1.add(lblgender, gbc);

TextGender= new JTextField(11);

gbc.gridx=1;

gbc.gridy=2;

p1.add(TextGender, gbc);

Gender= new JButton("Gender");

gbc.gridx=2;

gbc.gridy=2;

Gender.setPreferredSize(new Dimension(70,23));

p1.add(Gender, gbc);

pre= new JButton("<

gbc.gridx=0;

gbc.gridy=3;

p1.add(pre, gbc);

count = new JTextField(5);

gbc.gridx=1;

gbc.gridy=3;

p1.add(count, gbc);

next= new JButton(">>");

gbc.gridx=2;

gbc.gridy=3;

next.setPreferredSize(new Dimension(70,23));

p1.add(next, gbc);

p1.setVisible(true);

frame.add(p1);

frame.setVisible(true);

解决方案

GridBagLayout will drive you nuts, it's also one of the most flexible layout managers available in the JDK.

Don't be afraid to use compound layouts. In the example below, I've moved the navigation controls to there own panel, which makes it much easier to define complex layouts (you can also mix layout managers this way)

GridBagConstraints#fill allows you to determine how components may be filled within there cell. You have GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL and GridBagConstraints.BOTH ... I think there meaning is self explanatory.

In the example below, I've used GridBagConstraints.HORIZONTAL to allow the buttons to fill all the available space within their cell/column

UfOLf.png

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.border.LineBorder;

public class TestLayout20 {

public static void main(String[] args) {

new TestLayout20();

}

public TestLayout20() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException ex) {

} catch (InstantiationException ex) {

} catch (IllegalAccessException ex) {

} catch (UnsupportedLookAndFeelException ex) {

}

JFrame frame = new JFrame("Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

public TestPane() {

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.WEST;

JLabel lblname = new JLabel("Name");

gbc.gridx = 0;

gbc.gridy = 0;

add(lblname, gbc);

JTextField textname = new JTextField(11);

gbc.gridx = 1;

gbc.gridy = 0;

add(textname, gbc);

JLabel lblEmail = new JLabel("Email ");

gbc.gridx = 0;

gbc.gridy = 1;

add(lblEmail, gbc);

JTextField TextEmail = new JTextField(11);

gbc.gridx = 1;

gbc.gridy = 1;

add(TextEmail, gbc);

JLabel lblgender = new JLabel("Gender");

gbc.gridx = 0;

gbc.gridy = 2;

add(lblgender, gbc);

JTextField TextGender = new JTextField(11);

gbc.gridx = 1;

gbc.gridy = 2;

add(TextGender, gbc);

JButton New = new JButton("New");

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets(0, 12, 0, 0);

gbc.gridx = 2;

gbc.gridy = 0;

add(New, gbc);

JButton edit = new JButton("Edit");

gbc.gridx = 2;

gbc.gridy = 1;

add(edit, gbc);

JButton Gender = new JButton("Gender");

gbc.gridx = 2;

gbc.gridy = 2;

add(Gender, gbc);

JPanel pnlNav = new JPanel(new GridBagLayout());

gbc.insets = new Insets(12, 0, 0, 0);

gbc.gridx = 0;

gbc.gridy = 3;

gbc.gridwidth = GridBagConstraints.REMAINDER;

gbc.fill = GridBagConstraints.HORIZONTAL;

add(pnlNav, gbc);

JTextField count = new JTextField(5);

gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 0;

pnlNav.add(count, gbc);

JButton pre = new JButton("<

gbc.anchor = GridBagConstraints.WEST;

gbc.weightx = 1;

gbc.gridx = 0;

gbc.gridy = 0;

pnlNav.add(pre, gbc);

JButton next = new JButton(">>");

gbc.anchor = GridBagConstraints.EAST;

gbc.gridx = 2;

gbc.gridy = 0;

pnlNav.add(next, gbc);

}

}

}

I highly recommend that you have a read through How to use GridBagLayout for a better explanation :P

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值