java中gui布局,Java GUI布局

Could somebody tell me which java layout I need to use to achieve the layout below:

VbSik.jpg

I am currently playing out with the FlowLayout however I can’t get the entry fields to line up beside the output window:

FPZjB.jpg

Apologies if this is a simple question this is my first time using java. Here is my frame code:

private void makeFrame()

{

setLayout(new FlowLayout(0));

JPanel panel1 = new JPanel();

JPanel panel2 = new JPanel();

JPanel panel3 = new JPanel();

JPanel panel4 = new JPanel();

JPanel panel5 = new JPanel();

JPanel panel6 = new JPanel();

JPanel panel7 = new JPanel();

panel1.setLayout(new FlowLayout(0));

panel2.setLayout(new FlowLayout(0));

panel3.setLayout(new FlowLayout(0));

panel4.setLayout(new FlowLayout(0));

panel5.setLayout(new FlowLayout(0));

panel6.setLayout(new FlowLayout(0));

panel7.setLayout(new FlowLayout(0));

JLabel firstnameJLabel = new JLabel("First Name");

JLabel lastnameJLabel = new JLabel("Last Name");

JLabel streetJLabel = new JLabel("Street");

JLabel townJLabel = new JLabel("Town");

JLabel postcodeJLabel = new JLabel("Post Code");

panel1.add(listAllBtn);

panel1.add(listPersonalBtn);

panel1.add(listBusinessBtn);

panel1.add(addPersonalBtn);

panel1.add(addBusinessBtn);

panel1.add(deleteBtn);

panel1.add(findBtn);

panel1.add(quitBtn);

panel2.add(firstnameJLabel);

panel2.add(this.firstNameField);

panel2.add(this.bookScrollPane);

this.outputArea.setEditable(false);

panel3.add(lastnameJLabel);

panel3.add(this.lastNameField);

panel4.add(streetJLabel);

panel4.add(this.streetField);

panel5.add(townJLabel);

panel5.add(this.townField);

panel6.add(postcodeJLabel);

panel6.add(this.postcodeField);

panel7.add(enterBtn);

add(panel1);

add(panel2);

add(panel3);

add(panel4);

add(panel5);

add(panel6);

add(panel7);

enterBtn.addActionListener(this);

}

解决方案

Been a while since I worked with Swing, but it looks like the architecture is something like this:

2xqZq.png

You have a panel in the bottom which is BorderLayout

Inside that, you add a total of 4 new panels, NORTH, WEST, CENTER and EAST

in BorderLayout.NORTH you add a panel which have FlowLayout.LEFT

in BorderLayout.WEST you add a panel which have GroupLayout.YAXIS. this panel contains the labels for names etc and the ENTER button

in BorderLayout.CENTER you add the textfields that corresponds with the labels

in BorderLayout.EAST you add the JSCrollpane.

This might give you an idea and you can play around with these different panels to achive what you want

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值