Topic: 【请教】布局问题(Question about layout)

  Print this page

1.【请教】布局问题(Question about layout) Copy to clipboard
Posted by: yamakasy
Posted on: 2003-08-08 09:22

| leftPanel | centerPanel | rightPanel |

实现如图的布局,有如下要求
1.leftPanel和rightPanel大小一样。
2.centerPanel大小固定,不会随着整体的布局的变化而改变大小,而且始终居中,centerPanel和leftPanle,rightPanle大小不一样。
3.当整个窗体调整大小的时候,首先centerPanel满足以上条件,其次,leftPanel和rightPanel以同样比例放缩。

你们有什么好的建议?

2.Re:【请教】布局问题(Question about layout) [Re: yamakasy] Copy to clipboard
Posted by: jigsaw
Posted on: 2003-08-08 16:14

左中右用BorderLayout
中间用GridBagLayout
GridBagConstraints.anchor = GridBagConstraints.CENTER;

没有动手试过

3.Re:【请教】布局问题(Question about layout) [Re: yamakasy] Copy to clipboard
Posted by: floater
Posted on: 2003-08-08 22:14

simple flowlayout, set preferred size/size on the middle one. My guess

4.Re:【请教】布局问题(Question about layout) [Re: yamakasy] Copy to clipboard
Posted by: yamakasy
Posted on: 2003-08-09 23:05

不能满足要求。

5.Re:【请教】布局问题(Question about layout) [Re: yamakasy] Copy to clipboard
Posted by: fly2fire
Posted on: 2003-08-10 09:08

整体用GridBag布局器就可以了.如果用JBuilder来画的话就非常简单.

6.Re:【请教】布局问题(Question about layout) [Re: yamakasy] Copy to clipboard
Posted by: floater
Posted on: 2003-08-11 06:31


package swingtest;

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

public class LayoutTest extends JFrame
{
public LayoutTest()
{
//same for everyone, just copy and paste
super("Layout Test");
setSize(800, 600); //frame size
setDefaultCloseOperation(EXIT_ON_CLOSE); //close frame on exit action
Container contentPane = this.getContentPane();

//-----here is the problem.
//This layout just pack everything in one row, no size preservation
//upon resizing, unless we set those sizes.
//reference: http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

JPanel leftPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel rightPanel = new JPanel();

Dimension dim = new Dimension(200, 200);
//I don't care which to do the trick, just brutal force, so set all of them
//You may figure out which one does the trick.
centerPanel.setMinimumSize(dim);
centerPanel.setMaximumSize(dim);
centerPanel.setPreferredSize(dim);

leftPanel.add(new JLabel("Left Panel"));
leftPanel.setBorder(BorderFactory.createLineBorder(Color.black));
centerPanel.add(new JTextField("This is center, fixed"));
centerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
rightPanel.add(new JButton("right Panel"));
rightPanel.setBorder(BorderFactory.createLineBorder(Color.black));

contentPane.add(leftPanel, BorderLayout.WEST);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(rightPanel, BorderLayout.EAST);
}

public static void main(String[] args)
{
//same for everyone except the very first line, which should refer
//to this class
LayoutTest ttf = new LayoutTest();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = ttf.getSize();
if (frameSize.height > screenSize.height) frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width) frameSize.width = screenSize.width;
ttf.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
ttf.validate();
ttf.setVisible(true);
}

}

7.Re:【请教】布局问题(Question about layout) [Re: yamakasy] Copy to clipboard
Posted by: yamakasy
Posted on: 2003-08-11 11:49

呵呵,BoxLayout实现这个似乎不错。不过,我这里有一个StackLayout,功能强大,可以方便的实现上面的效果

StackLayout.java (8.57k)


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923