小问题。。。。只想知道为什么。。。。

camelrob 2003-03-13 11:16:08
在用过.net之后,又初用jbuilder8,在开发Swing中的可视组件,感觉不怎么方便,比如在Design JButton的大小时,为什么在propertyGrid不能直接设置它的size呢?
还比如JList,为什么在propertyGrid中不能直接设置它的items呢。。。。要在代码中加好麻烦。。。
...全文
22 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
camelrob 2003-03-14
  • 打赏
  • 举报
回复
to 楼上的:
怎么不给一个完整的呢
camelrob 2003-03-13
  • 打赏
  • 举报
回复
谢谢楼上的bjzhanghao(八进制)的发言,欢迎继续讨论。。。。
bjzhanghao 2003-03-13
  • 打赏
  • 举报
回复
用布局管理器也有好处,当窗口大小改变时控件会根据设置好的属性改变自己的大小,而且省去不少对齐的操作。如果你的程序是固定大小窗口,可以把布局设为null,也可以试试jbuilder带的XYLayout。
JList不能可视化设计这个问题,我觉得是因为JList可以显示各种对象,没有特别方便的设计方法。可能以后会改变吧,jbuilder的Active Designer可以设计菜单,可能以后可以用来设计其他控件。其实自己加也不麻烦。
camelrob 2003-03-13
  • 打赏
  • 举报
回复
首先感谢楼上的两位回答。
setLayout 为null后,对于控件的size更不拘泥了,为了明了就理应在PropertyGrid中设置SetBounds了。再说,这本来就是java 的特性,跟平台就没多大关系了吧。说到layout 问题,我觉得.net的布局直观,容易,更能让你用户接受。
bluesmile979 2003-03-13
  • 打赏
  • 举报
回复
setLayout(null);
qxjavajava 2003-03-13
  • 打赏
  • 举报
回复
因为java是 可以跨平台的 所以为了在不同的平台所显示的用户界面都是同样的 没有偏差 所以java引用了 布局管理器 的概念 所以有很多地方与window的界面开发不同
camelrob 2003-03-13
  • 打赏
  • 举报
回复
在线等待。。。。。。。。
shine333 2003-03-13
  • 打赏
  • 举报
回复
import java.awt.*;

public class ColumnLayout implements LayoutManager2{
protected int marginWidth;
protected int marginHeight;
protected int spacing;
protected int alignment;

public static final int LEFT=0;
public static final int CENTER=1;
public static final int RIGHT=2;
public static final int JUSTIFY=3;

public static final int PREFERRED_SIZE=0;
public static final int MINIMUM_SIZE=1;
public static final int MAXIMUM_SIZE=2;

public ColumnLayout(int mW, int mH, int s, int a){
if(mW<0 || mH<0 || s<0){
throw new IllegalArgumentException("Size of margin cannot be negative!");
}
if(a<LEFT || a>JUSTIFY){
throw new IllegalArgumentException("Not an alignment!");
}
marginWidth=mW;
marginHeight=mH;
spacing=s;
alignment=a;
}

public ColumnLayout(){
this(5,5,5,JUSTIFY);
}

public ColumnLayout(int a){
this(5,5,5,a);
}

public int getMarginWidth(){
return marginWidth;
}

public int getMarginHeight(){
return marginHeight;
}

public int getSpacing(){
return spacing;
}

public int getAlignment(){
return alignment;
}

public void layoutContainer(Container parent){
Insets insets=parent.getInsets();
Dimension parentSize=parent.getSize();
Component kid;
int kidsCount=parent.getComponentCount();
int x0=insets.left+marginWidth;
int x;
int y=insets.top+marginHeight;

int maxWidth=0;
if(alignment==JUSTIFY){
for(int i=0;i<kidsCount;i++){
kid=parent.getComponent(i);
if(!kid.isVisible()) continue;
Dimension pref=kid.getPreferredSize();
maxWidth=(maxWidth<pref.width)?pref.width:maxWidth;
}
}

for(int i=0;i<kidsCount;i++){
kid=parent.getComponent(i);
if(!kid.isVisible()) continue;
Dimension pref=kid.getPreferredSize();
int kidWidth=pref.width;
switch(alignment){
default:
case LEFT: x=x0; break;
case CENTER: x=(parentSize.width-pref.width)/2; break;
case RIGHT: x=parentSize.width-insets.right-marginWidth-pref.width; break;
case JUSTIFY: x=x0; kidWidth=maxWidth; break;
}
kid.setBounds(x,y,kidWidth,pref.height);
y+=pref.height+spacing;
}
}

public Dimension preferredLayoutSize(Container parent){
return layoutSize(parent, 1);
}

public Dimension minimumLayoutSize(Container parent){
return layoutSize(parent, 2);
}

public Dimension maximumLayoutSize(Container parent){
return layoutSize(parent, 3);
}

public Dimension layoutSize(Container parent, int sizeType){
if(sizeType<PREFERRED_SIZE || sizeType>MAXIMUM_SIZE){
throw new IllegalArgumentException("Wrong size type!");
}
int kidsCount=parent.getComponentCount();
Dimension size=new Dimension(0,0);
Insets insets=parent.getInsets();
int visibleKidsCount=0;
for(int i=0;i<kidsCount;i++){
Component kid=parent.getComponent(i);
Dimension d;
if(!kid.isVisible()) continue;
visibleKidsCount++;
switch(sizeType){
default:
case PREFERRED_SIZE: d=kid.getPreferredSize(); break;
case MINIMUM_SIZE: d=kid.getMinimumSize(); break;
case MAXIMUM_SIZE: d=kid.getMaximumSize(); break;
}
if(d.width>size.width) size.width=d.width;
size.height+=d.height;
}

size.width+=insets.left+insets.right+2*marginWidth;
size.height+=insets.top+insets.bottom+2*marginHeight;
size.height+=(visibleKidsCount-1)*spacing;

return size;
}

public void addLayoutComponent(String constraint, Component component){}
public void addLayoutComponent(Component component, Object constraint){}
public void removeLayoutComponent(Component component){}
public void invalidateLayout(Container parent){}
public float getLayoutAlignmentX(Container parent){
return 0.5f;
}
public float getLayoutAlignmentY(Container parent){
return 0.5f;
}

public String toString(){
String str;
switch(alignment){
default:
case LEFT: str="LEFT"; break;
case CENTER: str="CENTER"; break;
case RIGHT: str="RIGHT"; break;
case JUSTIFY: str="JUSTIFY"; break;
}
return "ColumnLayout [alignment="+str+", margin width="+marginWidth+
", margin height="+marginHeight+", spacing="+spacing+"]";
}

public boolean equals(Object obj){
if(obj==null || !(obj instanceof ColumnLayout)) return false;
ColumnLayout c=(ColumnLayout)obj;
return c.marginWidth==marginWidth && c.marginHeight==marginHeight
&& c.spacing==spacing && c.alignment==alignment;
}

public int hashCode(){
int hashCode=-93567;
hashCode*=alignment;
hashCode+=marginWidth*7403;
hashCode+=marginHeight*281;
return hashCode+spacing;
}
}

new JPanel().setLayout(new ColumnLayout());
camelrob 2003-03-13
  • 打赏
  • 举报
回复
up.....
camelrob 2003-03-13
  • 打赏
  • 举报
回复
用两个JList,四个JButton来布局,它们所处的位置是两个JList位于分别对整位于两左右边,四个JButton呈一列(不是一行)位于中间,其实是《Java专业编程指南》中的一个sample。我觉得书中所用的布局繁琐,有哪位能用最简单明了的代码写出来吗?

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧