如何进行java的布局设计?最好给点实例哟!

具体是如何实现布局设计这一方法的,有具体实例更好,谢谢了!

java的GUI界面定义是由awt类和swing类来完成的。它在布局管理上面采用了容器和布局管理分离的方案。也就是说,容器只管将其他小件放入其中,而不管这些小件是如何放置的。对于布局的管理交给专门的布局管理器类(LayoutManager)来完成。

其实,java在GUI方面应该是并不成功的。Awt类和swing类的结构非常复杂,加上充斥其间的子类继承和接口实现,使得要想掌握这两个类非常困难。这也是很多的java程序员抱怨的事情,但GUI已经成了程序发展的方向,所以这里我们也得勉为其难了。

现在我们来看java中布局管理器的具体实现。我们前面说过,java中的容器类(Container),它们只管加入小件(Meta),也就是说,它只使用自己的add()方法向自己内部加入小件。同时他记录这些加入其内部的小件的个数,可以通过container.getComponentCount()方法类获得小件的数目,通过container.getComponent(i)来获得相应小件的句柄。然后LayoutManager类就可以通过这些信息来实际布局其中的小件了。

java已经为我们提供了几个常用的布局管理器类,例如:BorderLayout、FlowLayout、GridBagLayout等等。但在实际的布局上,我们还是会有其他的需要。我在不久前的一个问题中曾经要一个垂直的流式布局,我称之为VflowLayout,其实BoxLayout和GridBagLayout可以完成类似的工作,但前者是swing类的成员,我的客户端是一个applet,不能使用,而后者必须在类生成的时候指定列数,而失去了灵活性,所以我决定重写一个自己的布局管理器来实现。经过分析,所有的LayoutManager都要实现一个接口,就是LayoutManager Inerface或者是他的一个子接口LayoutManager2 Interface,后者用于复杂的布局管理,例如GridCardLayout。LayoutManager有五个方法需要实现,分别是:

1、public void addLayoutComponent(String name, Component comp);

2、public void removeLayoutComponent(Component comp);

3、public Dimension preferredLayoutSize(Container container);

4、public Dimension minimumLayoutSize(Container container);

5、public void layoutContainer(Container container);

第一个方法其实就是你在使用container.add(String name,component comp);时调用的方法,例如BorderLayout为布局管理器时。但在FlowLayout中由于没有其他的附加信息,所以不需要填充这个方法。相应的第二个方法也就不需要填充了。真正核心的方法是第三个和第五个方法,前者是最终确定Container有多大的,而后者就是决定Container中各个小件的实际位置的了。也就是说,当我们用container.setLayout(LayoutManager)后,再加入小件后,最后系统做的工作其实是LayoutManager. layoutContainer(container);和container.setSize(LayoutManager. PreferredLayoutSize(container));。

下面是我的新类:VflowLayout。
package render_account;

import java.awt.*;
import java.io.*;

public class VFlowLayout implements LayoutManager,Serializable{

int hgap;
int vgap;

public VFlowLayout(){
this(5,5);
}

public VFlowLayout(int i,int j){
this.hgap=i;
this.vgap=j;
}

public void addLayoutComponent(String name, Component comp){

}

public void removeLayoutComponent(Component comp){

}

public Dimension preferredLayoutSize(Container container){
synchronized(container.getTreeLock()){
Dimension dimension1=new Dimension(0,0);
int i=container.getComponentCount();
for(int j=0;j Component component = container.getComponent(j);
if(component.isVisible()){
Dimension dimension2=component.getPreferredSize();
dimension1.width=Math.max(dimension1.width,dimension2.width);
if(j>0)
dimension1.height+=vgap;
dimension1.height+=dimension2.height;
}
}
Insets insets=container.getInsets();
dimension1.height+=insets.top+insets.bottom+vgap*2;
dimension1.width+=insets.left+insets.right+hgap*2;
Dimension dimension=dimension1;
return dimension;
file://return(new Dimension(50,200));
}
}

public Dimension minimumLayoutSize(Container container){
synchronized(container.getTreeLock()){
Dimension dimension1=new Dimension(0,0);
int i=container.getComponentCount();
for(int j=0;j Component component = container.getComponent(j);
if(component.isVisible()){
Dimension dimension2=component.getMinimumSize();
dimension1.width=Math.max(dimension1.width,dimension2.width);
if(j>0)
dimension1.height+=vgap;
dimension1.height+=dimension2.height;
}
}
Insets insets=container.getInsets();
dimension1.height+=insets.top+insets.bottom+vgap*2;
dimension1.width+=insets.left+insets.right+hgap*2;
Dimension dimension=dimension1;
return dimension;
}
}

public void layoutContainer(Container container){
synchronized(container.getTreeLock()){
Insets insets=container.getInsets();
int vSpace=container.getSize().height-(insets.top+insets.bottom+vgap*2);
int componentCount=container.getComponentCount();
int left=insets.left+hgap;
int totalHeight=0;
int width=0;
int componentStart=0;
for(int i=0;i Component component=container.getComponent(i);
if(component.isVisible()){
Dimension dimension=component.getPreferredSize();
component.setSize(dimension.width,dimension.height);
if(totalHeight==0 || totalHeight+dimension.height<=vSpace){
if(totalHeight>0)
totalHeight+=vgap;
totalHeight+=dimension.height;
width=Math.max(width,dimension.width);
}else{
moveComponents(container,insets.top+vgap,left,width,componentStart,i);
totalHeight=0;
left+=hgap+width;
width=dimension.width;
componentStart=i;
}
}
}
moveComponents(container,insets.top+vgap,left,width,componentStart,componentCount);
}
}

private void moveComponents(Container container,int top,int left,int width,int componentStart,int componentEnd){
synchronized(container.getTreeLock()){
for(int i=componentStart;i Component component=container.getComponent(i);
if(component.isVisible()){
component.setLocation(left,top);
top+=component.getPreferredSize().height+vgap;
}
}
}
}

public void setHgap(int i){
this.hgap=i;
}

public void setVgap(int i){
this.vgap=i;
}

public int getHgap(){
return(this.hgap);
}

public int getVgap(){
return(this.vgap);
}
}

jTable1 = new JTable(vrow, vcol);

jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //滚动条设置左右滚

this.jScrollPane1.getViewport().add(jTable1, null); //在滚动条中放入表

}

catch (Exception ex) {

JOptionPane.showMessageDialog(null, ex);

}

}

}

class Frame1_jButton1_actionAdapter

implements java.awt.event.ActionListener {

Frame1 adaptee;

Frame1_jButton1_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton1_actionPerformed(e);

}

}

华容道的小游戏玩~用按钮分别表示曹操关羽等,然后设package com.doumonkey.game.hyroad;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Point;

public class HRroadGUI extends JFrame implements KeyListener{
public JButton cao,guan,zhang,zhao,ma,huang,z1,z2,z3,z4;
public HRroadGUI() {
setSize(410,510);
cao=new JButton("曹操");
cao.setSize(200,200); //设置按纽大小
cao.addKeyListener(this); //添加键盘监听
cao.setLocation(100,0); //设置按纽位置
add(cao); //添加按纽
guan=new JButton("关羽");
guan.setSize(200,100);
guan.addKeyListener(this);
guan.setLocation(100,200);
add(guan);
zhang=new JButton("张飞");
zhang.setSize(100,200);
zhang.addKeyListener(this);
zhang.setLocation(0,0);
add(zhang);
zhao=new JButton("赵云");
zhao.setSize(100,200);
zhao.addKeyListener(this);
zhao.setLocation(300,0);
add(zhao);
huang=new JButton("黄忠");
huang.setSize(100,200);
huang.addKeyListener(this);
huang.setLocation(0,200);
add(huang);
ma=new JButton("马超");
ma.setSize(100,200);
ma.addKeyListener(this);
ma.setLocation(300,200);
add(ma);
z1=new JButton("卒一");
z1.setSize(100,100);
z1.addKeyListener(this);
z1.setLocation(100,300);
add(z1);
z2=new JButton("卒二");
z2.setSize(100,100);
z2.addKeyListener(this);
z2.setLocation(200,300);
add(z2);
z3=new JButton("卒三");
z3.setSize(100,100);
z3.addKeyListener(this);
z3.setLocation(0,400);
add(z3);
z4=new JButton("卒四");
z4.setSize(100,100);
z4.addKeyListener(this);
z4.setLocation(300,400);
add(z4);

this.validate();
setVisible(true);
}

public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}

public static void main(String[] args) {
HRroadGUI hrroadgui = new HRroadGUI();
}
}
//你可以使用绝对位置,把取消布局管理器
修改后
public HRroadGUI() {
this.setLayout(null); //加上这条语句
setSize(410,510);
温馨提示:答案为网友推荐,仅供参考