我想实现这样的一个布局.
图见http://topic.csdn.net/u/20080414/11/0408d450-bb54-4576-9bed-1448f940f14c.html的43楼的那个布局
希望和他类似
大家顺带帮忙讲一下界面布局的经验,比如说思考方式等
呵呵谢了.....
好的话继续加100分
图见http://topic.csdn.net/u/20080414/11/0408d450-bb54-4576-9bed-1448f940f14c.html的43楼的那个布局
希望和他类似
大家顺带帮忙讲一下界面布局的经验,比如说思考方式等
呵呵谢了.....
好的话继续加100分
Box布局的我写了一个好象没达到那个效果
主面板用的是BorderLayout.
加了菜单,ToolBar.中间的是功能面板,这个功能面板采用的是JSplitPane,左右放置.底部放了一个信息面板(FlowLayout布局)--------
再细说功能面板:左边可采用GridBagLayout实现(tooooo easy)
右边是4个JTabbedPane.
over
有空的话,些个和43楼右边用GridBagLayout布局的差不都的界面
谢谢大家了
import java.awt.GridBagLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Test extends JFrame {
private void initUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 400);
this.getContentPane().add(getMainPanel());
this.setVisible(true);
} public Test() {
initUI();
} private JPanel getLeftPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
JButton button1 = new JButton("B1");
JButton button3 = new JButton("B3");
GridBagConstraints b1C = new GridBagConstraints();
b1C.gridx = 0;
b1C.gridy = 0;
b1C.weightx = 1.0;
b1C.weighty = 2.0;
b1C.fill = GridBagConstraints.BOTH;
panel.add(button1, b1C);
b1C.gridx = 0;
b1C.gridy = 1;
b1C.weighty = 1.0;
panel.add(button3, b1C);
return panel;
} private JPanel getRightPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
JButton button2 = new JButton("B2");
JButton button4 = new JButton("B4");
GridBagConstraints b2C = new GridBagConstraints();
b2C.gridx = 0;
b2C.gridy = 0;
b2C.weightx = 1.0;
b2C.weighty = 3.0;
b2C.fill = GridBagConstraints.BOTH;
panel.add(button2, b2C);
b2C.gridx = 0;
b2C.gridy = 1;
b2C.weighty = 2.0;
panel.add(button4, b2C);
return panel;
} private JPanel getMainPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints b3C = new GridBagConstraints();
b3C.fill = GridBagConstraints.BOTH;
b3C.gridx = 0;
b3C.gridy = 0;
b3C.weighty = 1.0;
b3C.weightx = 3.0;
panel.add(getLeftPanel(), b3C);
b3C.gridx = 1;
b3C.gridy = 0;
b3C.weightx = 2.0;
panel.add(getRightPanel(), b3C);
return panel;
} /**
* @param args
*/
public static void main(String[] args) {
new Test(); }}构造函数:
GirdBagLayout()建立一个新的GridBagLayout管理器。
GridBagConstraints()建立一个新的GridBagConstraints对象。
GridBagConstraints(int gridx,int gridy,
int gridwidth,int gridheight,
double weightx,double weighty,
int anchor,int fill, Insets insets,
int ipadx,int ipady)建立一个新的GridBagConstraints对象,并指定其参数的值。
看着这一堆的参数就快烦死了,下面就了解一下参数的意思:参数说明:
gridx,gridy —— 设置组件的位置,
gridx设置为GridBagConstraints.RELATIVE代表此组件位于之前所加入组件的右边。
gridy设置为GridBagConstraints.RELATIVE代表此组件位于以前所加入组件的下面。
建议定义出gridx,gridy的位置以便以后维护程序。gridx=0,gridy=0时放在0行0列。 gridwidth,gridheight —— 用来设置组件所占的单位长度与高度,默认值皆为1。
你可以使用GridBagConstraints.REMAINDER常量,代表此组件为此行或此列的最后一个组件,而且会占据所有剩余的空间。 weightx,weighty —— 用来设置窗口变大时,各组件跟着变大的比例。
当数字越大,表示组件能得到更多的空间,默认值皆为0。 anchor —— 当组件空间大于组件本身时,要将组件置于何处。
有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择。 insets —— 设置组件之间彼此的间距。
它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。ipadx,ipady —— 设置组件间距,默认值为0。GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当我们将GridBagConstraints的参数都设置
好了之后,必须new一个GridBagConstraints的对象出来,以便GridBagLayout使用。代码片断:
JButton b;
GridBagConstraints c;
int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
double weightx,weighty;
Insets inset;
JFrame f=new JFrame();
GridBagLayout gridbag=new GridBagLayout();
Container contentPane=f.getContentPane();
contentPane.setLayout(gridbag);
b=new JButton("first");
gridx=0;
gridy=0;
gridwidth=1;
gridheight=1;
weightx=10;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
GridBagLayout这种管理器是十分灵活的,只不过他写起来比较麻烦,不过用了之后才发现他对界面的部署帮助很大。
有也是在国外的书籍中见过
大部分人要么就说GridBagLayout,要么就说BoxLayout,还有null,
对于GridBagLayout和BoxLayout,如果不使用相应的IDE工具,恐怕添加30个组件会累死你。
哥们还是想想自己实现想要的布局管理器吧。
JMenuBar
+
JSplitPane
+
JTabbedPane
+
JPanel
=
OK..
JMenuBar
+
JSplitPane
+
JTabbedPane
+
JPanel
=
可现在我想通过GridBagLayout和BoxLayout来具体实现他
自己放位置 很好用
没用过楼上可以讲具体点
谢了
例子可以去google
现在贴一个我常用的方法,可以来自动调整组件分成几行几列,并让每列等高,每行等宽。 /**
* Aligns the first rows * cols
* components of parent in
* a grid. Each component is as big as the maximum
* preferred width and height of the components.
* The parent is made just big enough to fit them all.
*
* @param rows number of rows
* @param cols number of columns
* @param initialX x location to start the grid at
* @param initialY y location to start the grid at
* @param xPad x padding between cells
* @param yPad y padding between cells
*/
public static void makeGrid(Container parent,
int rows, int cols,
int initialX, int initialY,
int xPad, int yPad) {
SpringLayout layout;
try {
layout = (SpringLayout)parent.getLayout();
} catch (ClassCastException exc) {
System.err.println("The first argument to makeGrid must use SpringLayout.");
return;
} Spring xPadSpring = Spring.constant(xPad);
Spring yPadSpring = Spring.constant(yPad);
Spring initialXSpring = Spring.constant(initialX);
Spring initialYSpring = Spring.constant(initialY);
int max = rows * cols; //Calculate Springs that are the max of the width/height so that all
//cells have the same size.
Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
getWidth();
Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
getWidth();
for (int i = 1; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(
parent.getComponent(i)); maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
} //Apply the new width/height Spring. This forces all the
//components to have the same size.
for (int i = 0; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(
parent.getComponent(i)); cons.setWidth(maxWidthSpring);
cons.setHeight(maxHeightSpring);
} //Then adjust the x/y constraints of all the cells so that they
//are aligned in a grid.
SpringLayout.Constraints lastCons = null;
SpringLayout.Constraints lastRowCons = null;
for (int i = 0; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(
parent.getComponent(i));
if (i % cols == 0) { //start of new row
lastRowCons = lastCons;
cons.setX(initialXSpring);
} else { //x position depends on previous component
cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
xPadSpring));
} if (i / cols == 0) { //first row
cons.setY(initialYSpring);
} else { //y position depends on previous row
cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
yPadSpring));
}
lastCons = cons;
} //Set the parent's size.
SpringLayout.Constraints pCons = layout.getConstraints(parent);
pCons.setConstraint(SpringLayout.SOUTH,
Spring.sum(
Spring.constant(yPad),
lastCons.getConstraint(SpringLayout.SOUTH)));
pCons.setConstraint(SpringLayout.EAST,
Spring.sum(
Spring.constant(xPad),
lastCons.getConstraint(SpringLayout.EAST)));
}
呵呵谢了试试
参数是行列数以及占几行几列 横纵向是否扩展这些
返回GridBagConstraints然后自己摆就行了,技术上没任何难度,
就是需要态度认真些,建议用excel之类的先摆再写代码。
希望各位高手能给个具体点的类似
图见http://topic.csdn.net/u/20080414/11/0408d450-bb54-4576-9bed-1448f940f14c.html的43楼的那个布局
呵呵急用
谢谢了啊
实现LayoutManager2,自己写,在layoutContainer方法中,定位各个控件
UI的布局并不需要花费太多的时间,除非要做customized component. 所有的LayoutManager 也只是为了给Developer提供方便的现成的implementation去用。你应该用更多的时间去研究其他的东西。一种Layout Manager的使用并不能对你分析和设计有太多帮助。
多个布局混用,适当的控件组合,相信并不难顺便问下,他的LAF用的什么?window?还是第三方LAF?