java中面板有什么用_java-为什么这三个使用GridBagLayout的面板使用...

我有一个包含多个面板的应用程序;我想自由地为不同的面板使用不同的布局管理器,但是希望它们在用户调整窗口大小时具有类似的行为.

package example;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.BoxLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTabbedPane;

import javax.swing.JTextField;

public class TP1 extends JFrame

{

public static void main(String[] args)

{

TP1 tp1 = new TP1();

tp1.go();

}

public void go()

{

setDefaultCloseOperation(EXIT_ON_CLOSE);

// create a panel with some labels on it

JPanel innerFirst = new JPanel();

innerFirst.setLayout(new BoxLayout(innerFirst, BoxLayout.PAGE_AXIS));

innerFirst.add(new JLabel("one"));

innerFirst.add(new JLabel("two"));

innerFirst.add(new JLabel("three"));

innerFirst.add(new JLabel("four"));

// put that panel in a scroll pane

JScrollPane firstSP = new JScrollPane(innerFirst);

// make another panel and put our scrolled panel in it

JPanel outerFirst = new JPanel();

outerFirst.setLayout(new BoxLayout(outerFirst, BoxLayout.PAGE_AXIS));

outerFirst.add(firstSP);

// create a GridBagLayout panel with some text fields on it

JPanel innerSecond = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

gbc.weightx = .25;

gbc.anchor = GridBagConstraints.LINE_START;

innerSecond.add(new JTextField(8), gbc);

gbc.gridx = 0;

gbc.gridy = 1;

innerSecond.add(new JTextField(10), gbc);

gbc.gridx =0;

gbc.gridy = 2;

innerSecond.add(new JTextField(12), gbc);

// put that panel in a scroll pane

JScrollPane secondSP = new JScrollPane(innerSecond);

// make another panel and put our second scrolled panel in it

JPanel outerSecond = new JPanel();

outerSecond.setLayout(new BoxLayout(outerSecond, BoxLayout.LINE_AXIS));

outerSecond.add(secondSP);

JPanel innerThird = new JPanel(new GridBagLayout());

GridBagConstraints gbc3 = new GridBagConstraints();

gbc3.anchor = GridBagConstraints.LINE_END;

gbc.weightx = .25;

gbc3.gridx = 0;

gbc3.gridy = 0;

innerThird.add(new JLabel("1st label"), gbc3);

gbc3.gridy = 1;

innerThird.add(new JLabel("second label"), gbc3);

gbc3.gridy = 2;

innerThird.add(new JLabel("IIIrd label"), gbc3);

gbc3.anchor = GridBagConstraints.LINE_START;

gbc3.gridx = 1;

gbc3.gridy = 0;

innerThird.add(new JTextField(8), gbc3);

gbc3.gridy = 1;

innerThird.add(new JTextField(12), gbc3);

gbc3.gridy = 2;

innerThird.add(new JTextField(14), gbc3);

JScrollPane thirdSP = new JScrollPane(innerThird);

JPanel outerThird = new JPanel();

outerThird.setLayout(new BoxLayout(outerThird, BoxLayout.LINE_AXIS));

outerThird.add(thirdSP);

// put the scrolled panes onto a tabbed pane

JTabbedPane tp = new JTabbedPane();

tp.add("text fields", outerSecond);

tp.add("labels", outerFirst);

tp.add("mixed", outerThird);

// add the tabbed pane to the frame

this.add(tp);

// pack it and ship it.

pack();

setVisible(true);

}

}

运行上面的代码,我们得到一个带有三个选项卡的带有选项卡式窗格的窗口.如果我们将窗口缩小,所有的对象都会按预期获得滚动条.如果将其放大,则三个选项的行为会有所不同:带有标签的选项卡仅将其留在窗口的左上方,带有字段的选项卡仅将其垂直居中在左边缘,而带有标签的选项卡则将其混合在一起在放大的窗口中将它们水平和垂直居中.

这对于应用程序是不可接受的;我需要以某种方式使所有面板的行为类似.我需要它们全部具有滚动条,并且如果窗口的尺寸大于内部面板的尺寸,我希望它们都保持在左上方.

另一个要求:我的选项卡被扩展JPanel的东西占据,在将JScrollPane直接放入选项卡之前,有人告诉我,但是对于我的应用程序,我也不想这样做.这只会使其他事情变得比他们需要的复杂.

除了希望在底部和右侧放置所有额外的空间之外,我非常想了解为什么这三种情况的行为会有所不同.我仍然相信,如果我们了解我们正在做的事情背后的原则,而不是仅仅左右复制示例,然后反复尝试,直到它们起作用,我们所有人都会变得更好.

(顺便说一句,我有一个GroupLayout面板,它似乎确实偏向左上方,但我认为这不是我的问题所必需的,因为它是100行代码.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值