观海听涛BBS's Archiver

yiyanwan77 发表于 2008-5-17 22:19

GridBagLayout布局

想吧毕设用java重做一遍
刚开始就遇到布局问题
这个是我vc的对话框
[attach]70320[/attach]
到了java就成这个鬼样子了
[attach]70321[/attach]
当然有些控件我没加
我用了四个面板,使用GridBagLayout布局
各面板控件暂使用的都是BorderLayout
问题是GridBagLayout能不能精确的设置面板的大小
我试着改了面板里的控件大小之后,面板的布局都变形了
在GridBagLayout布局给面板设置位置。比例大小的时候
框架面板的大小需设定么?
代码如下:
import javax.swing.*;
import java.awt.*;
public class GoET_CLIENT extends JFrame{
private Container container;
//四个面板
private JPanel DisplayPanel;//显示面板
  private JTextArea taRecv;//多行文本框
  private JScrollPane spRecv; //滚动条
private JPanel InputPanel;//右下角输入面板
  private JTextArea taSend;
  private JScrollPane spSend;
  private JButton btnSend;//发送按钮
  private JButton btnClose;//关闭按钮
private JPanel ChangePanel;//上方功能面板
  private JButton btnSound;//语音聊天按钮
private JPanel InfoPanel;//右面信息面板
  private JLabel lbInfo;

private GridBagLayout gbLayout=new GridBagLayout();//框架布局
private GridBagConstraints constraints=new GridBagConstraints();//网格包组件控制
public void SetDPPanelLayout(){
  //添加显示面板
  /*constraints.fill=GridBagConstraints.NONE;
  constraints.anchor=GridBagConstraints.HORIZONTAL;
  setConstraints(constraints,0,2,5,5,0,0);
  */
  DisplayPanel=new JPanel();
  DisplayPanel.setBackground(Color.red);
  //gbLayout.setConstraints(DisplayPanel, constraints);
   //添加多行文本框
  DisplayPanel.setLayout(new BorderLayout());
  taRecv=new JTextArea("你号",15,31);
  taRecv.setLineWrap(true);
  taRecv.setAutoscrolls(true);
  spRecv=new JScrollPane(taRecv);
  
  DisplayPanel.add(spRecv,BorderLayout.CENTER);
  //container.add(DisplayPanel);
}
public void SetIPPanelLayout(){
  //添加输入面板
  //constraints.fill=GridBagConstraints.NONE;
  //constraints.anchor=GridBagConstraints.HORIZONTAL;
  //setConstraints(constraints,0,6,2,5,0,0);
  InputPanel=new JPanel();
  InputPanel.setBackground(Color.BLUE);
  //gbLayout.setConstraints(InputPanel, constraints);
   //添加按钮
  InputPanel.setLayout(new BorderLayout());
  btnSend=new JButton("发送");
  taSend=new JTextArea("发送",5,31);
  btnClose=new JButton("关闭");
  taSend.setLineWrap(true);
  taSend.setAutoscrolls(true);
  spSend=new JScrollPane(taSend);
  InputPanel.add(spSend,BorderLayout.CENTER);
  InputPanel.add(btnClose,BorderLayout.EAST);
  InputPanel.add(btnSend,BorderLayout.SOUTH);
  //container.add(InputPanel);
}
public void SetCHPanelLayout(){
  //添加功能面板
  //constraints.fill=GridBagConstraints.NONE;
  //constraints.anchor=GridBagConstraints.HORIZONTAL;
  //setConstraints(constraints,0,0,7,1,0,0);
  ChangePanel=new JPanel();
  ChangePanel.setBackground(Color.CYAN);
  ChangePanel.setLayout(new BorderLayout());
  //gbLayout.setConstraints(ChangePanel, constraints);
  //container.add(ChangePanel);
  btnSound=new JButton("语音聊天");
  ChangePanel.add(btnSound,BorderLayout.WEST);
}
public void SetIFPanelLayout(){
  //添加信息面板
  //constraints.fill=GridBagConstraints.NONE;
  //constraints.anchor=GridBagConstraints.HORIZONTAL;
  //setConstraints(constraints,5,1,2,7,0,0);
  InfoPanel=new JPanel();
  InfoPanel.setBackground(Color.GREEN);
  //gbLayout.setConstraints(InfoPanel, constraints);
  //container.add(InfoPanel);
  InfoPanel.setLayout(new BorderLayout());
  lbInfo=new JLabel("信息");
  InfoPanel.add(lbInfo,BorderLayout.CENTER);
  
}
public GoET_CLIENT (){
  super("GoET");
  this.setSize(500,500);//初始化框架大小
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setConstraints(GridBagConstraints gbc,int row,int column,
   int numRows,int numColumns,int Weightx,int Weighty){
  gbc.gridx=row;//左上角单元格的坐标
  gbc.gridy=column;
  gbc.gridwidth=numRows;//行占单元格数
  gbc.gridheight=numColumns;//列占单元格数
  gbc.weightx=Weightx;//调整时分配空间
  gbc.weighty=Weighty;//调整时分配空间
}
public void setLayout(){
  container=this.getContentPane();//框架组件控制
  container.setLayout(gbLayout);//设置框架布局
  constraints.gridheight=10;
  constraints.gridwidth=10;
  SetDPPanelLayout();
  SetIPPanelLayout();
  SetCHPanelLayout();
  SetIFPanelLayout();
  
  constraints.fill=GridBagConstraints.BOTH;
  //constraints.anchor=GridBagConstraints.CENTER;
  setConstraints(constraints,0,0,10,1,10,1);
  gbLayout.setConstraints(ChangePanel, constraints);
  container.add(ChangePanel);
  
  //constraints.fill=GridBagConstraints.NONE;
  //constraints.anchor=GridBagConstraints.HORIZONTAL;
  setConstraints(constraints,7,1,3,9,2,10);
  gbLayout.setConstraints(InfoPanel, constraints);
  container.add(InfoPanel);
  
  //constraints.fill=GridBagConstraints.NONE;
  //constraints.anchor=GridBagConstraints.HORIZONTAL;
  setConstraints(constraints,0,1,7,5,0,0);
  gbLayout.setConstraints(DisplayPanel, constraints);
  container.add(DisplayPanel);
  
  //constraints.fill=GridBagConstraints.NONE;
  //constraints.anchor=GridBagConstraints.HORIZONTAL;
  setConstraints(constraints,0,9,7,1,0,0);
  gbLayout.setConstraints(InputPanel, constraints);
  container.add(InputPanel);
  
  
  }
public static void main(String[] args) {
  // TODO Auto-generated method stub
  GoET_CLIENT goet=new GoET_CLIENT();
  goet.setLayout();
  goet.show();
   
  
  
}
}

Lemonade 发表于 2008-5-17 23:36

java我还没见到好点的窗体设计器......
IDE还都差了一点点

花落无声 发表于 2008-5-18 11:56

Swing中的布局不好玩的说~

yiyanwan77 发表于 2008-5-18 15:27

[quote]原帖由 [i]花落无声[/i] 于 2008-5-18 11:56 发表 [url=http://bbs.ghtt.net/redirect.php?goto=findpost&pid=1292315&ptid=108336][img]http://bbs.ghtt.net/images/common/back.gif[/img][/url]
Swing中的布局不好玩的说~ [/quote]
vc里拖几个控件
java里要写半天代码
还真是麻烦事

转角遇到150 发表于 2008-5-18 16:59

这个还没学到,只能瞻仰。。。

牛逼人物 发表于 2008-5-18 22:18

要想做成VC那种的?
可以先做大体的结构设计,然后把结构细分,使用多层结构的布局可以自由分配位置,现在你的布局是一层的,当然不好看。
另外,Java支持空钩件,就是平均分配位置的时候可以给一个空的位置分配自由度,就能出现空行和按钮间距(在按钮间加入一个空的位置,长度==靠支持按钮的布局控件分配,可以制定多个位置)。
其实JAVA可以做得很好看。继续努力吧![em12]

yiyanwan77 发表于 2008-5-18 23:00

[quote]原帖由 [i]牛逼人物[/i] 于 2008-5-18 22:18 发表 [url=http://bbs.ghtt.net/redirect.php?goto=findpost&pid=1295970&ptid=108336][img]http://bbs.ghtt.net/images/common/back.gif[/img][/url]
要想做成VC那种的?
可以先做大体的结构设计,然后把结构细分,使用多层结构的布局可以自由分配位置,现在你的布局是一层的,当然不好看。
另外,Java支持空钩件,就是平均分配位置的时候可以给一个空的位置分配自由度,就能出现空行 ... [/quote]
null布局不是会有窗体拉动是控件不动的情况么
vc里面就在onsize里写一堆控件的移动代码
到了java还得写就郁闷了

牛逼人物 发表于 2008-5-18 23:08

[quote]原帖由 [i]yiyanwan77[/i] 于 2008-5-18 23:00 发表 [url=http://bbs.ghtt.net/redirect.php?goto=findpost&pid=1296388&ptid=108336][img]http://bbs.ghtt.net/images/common/back.gif[/img][/url]

null布局不是会有窗体拉动是控件不动的情况么
vc里面就在onsize里写一堆控件的移动代码
到了java还得写就郁闷了 [/quote]
对,用null布局,你就能看到最大化的尴尬了,控件都在左上角一堆~~~

唐尸三摆手 发表于 2008-5-19 09:32

java的话可以尝试一下SWT 比swing好用的说~[em04]

牛逼人物 发表于 2008-5-19 09:36

[quote]原帖由 [i]唐尸三摆手[/i] 于 2008-5-19 09:32 发表 [url=http://bbs.ghtt.net/redirect.php?goto=findpost&pid=1296867&ptid=108336][img]http://bbs.ghtt.net/images/common/back.gif[/img][/url]
java的话可以尝试一下SWT 比swing好用的说~[em04] [/quote]
居然上课的时候上观海...!
SWT比较框架.

唐尸三摆手 发表于 2008-5-19 09:38

回复 10# 牛逼人物 的帖子

你不也是呢。。。[em07]
说实话我还真不喜欢java.....除了j2ee 哈哈...

牛逼人物 发表于 2008-5-19 09:50

[quote]原帖由 [i]唐尸三摆手[/i] 于 2008-5-19 09:38 发表 [url=http://bbs.ghtt.net/redirect.php?goto=findpost&pid=1296882&ptid=108336][img]http://bbs.ghtt.net/images/common/back.gif[/img][/url]
你不也是呢。。。[em07]
说实话我还真不喜欢java.....除了j2ee 哈哈... [/quote]
我也不喜欢,感觉很麻烦
后面那个除了.............是不是老师在你旁边呢?

yiyanwan77 发表于 2008-5-19 10:01

[quote]原帖由 [i]唐尸三摆手[/i] 于 2008-5-19 09:32 发表 [url=http://bbs.ghtt.net/redirect.php?goto=findpost&pid=1296867&ptid=108336][img]http://bbs.ghtt.net/images/common/back.gif[/img][/url]
java的话可以尝试一下SWT 比swing好用的说~[em04] [/quote]

我试试去

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.