求助:如何调整布局中的组件的大小
我是一名初学Java的新手,在学习过程中想做自己一个类似Windows文件对话框的对话框界面,一来可以巩固所学的编程知识,而来也许在以后的程序中可以用得着,对话框采用了GridBagLayout布局,组件一一添加后,界面效果也出来了,看看也向那么回事,但是界面上的组件却不是那么听话,无法随心所欲的布置,大家看看下面这个对话框的界面图:对于这个界面,说句实话是不满意,主要下面几方面需要调整:
1、预留区域本来预留作为文件对话框的工具栏(参见Windows的记事本程序文件-打开命令弹出对话框),但是预留区域太小(m_toolbar域),我想将其前面的组件(在文件菜单上为组合框,m_comboPath域)缩小,以放大预留区域。
2、左面的列表框(对应文件对话框的搜索范围列表框,m_listSearch域)过大,需要缩小;
3、下面3个TextField对象(m_comboFileName、m_comboFileTyoe,m_comboFileCode域)太小,且远离前面的标签,不符和一般程序的界面习惯,需要对最后三行进行调整,要求TextField对象紧靠三个标签开始,将TextField组件对象所占区域扩大,并将最右面的两个“确定”、“取消”按钮缩小并靠右排列。但各组件之间的间隔距离要保持最小距离(gc.insets)中定义。
上面的界面程序代码如下:
package MenuTestUI;
import java.awt.*;
import java.awt.event.*;
public class CFileDialog extends Dialog implements ActionListener,WindowListener {
/**
*
*/
private static final long serialVersionUID = -6362342592776154306L;
//private Panel m_panel = new Panel();
private StringBuffer m_strTitle = new StringBuffer("打开");
private Label m_lbSearch = new Label("搜索范围");
private TextField m_comboPath = new TextField();
private Label m_toolbar = new Label("预留区域");
private List m_listSearch = new List();
private TextArea m_listFiles = new TextArea();
private Label m_lbFileName = new Label("文件名");
private Label m_lbFileType = new Label("文件类型");
private Label m_lbFileCode = new Label("编码");
private TextField m_comboFileName = new TextField();
private TextField m_comboFileType = new TextField();
private TextField m_comboFileCode = new TextField();
private Button m_btnOK = new Button("确定");
private Button m_btnCancel = new Button("取消");
public CFileDialog(Dialog owner){
super(owner);
}
public CFileDialog(Frame owner){
super(owner);
//InitDialog();
}
public void InitDialog(){
// 设置对话框整体属性
setTitle(m_strTitle.toString());
addWindowListener(this);
setResizable(false); // 禁止用户调整对话框大小
setIconImage(null); // 去除标题框图标
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
// 设置对话框和组件的大小
setSize(600,400);
SetGridBagLayout();
// 定位对话框到窗口中央
Component winParent = (Window)getParent();
int nLeft = winParent.getX()+winParent.getWidth()/2 - getWidth()/2;
int nTop = winParent.getY()+winParent.getHeight()/2 - getHeight()/2;
setLocation(nLeft,nTop);
}
protected void AddComponent(Component com,
GridBagLayout gridbag,
GridBagConstraints gc){
gridbag.setConstraints(com, gc);
add(com);
}
private void SetGridBagLayout(){
// 将对话框布局型式设置为GridBagLayou(网格包)型式
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
setFont(new Font(Font.DIALOG, Font.PLAIN, 14));
setLayout(gridbag);
gc.fill = GridBagConstraints.BOTH;
gc.gridheight = 1;
gc.gridwidth = 1;
gc.weightx =1.0;
gc.insets = new Insets(4,4,4,4);
// 设置第一行组件
AddComponent(new Label(""),gridbag,gc);
AddComponent(m_lbSearch,gridbag,gc);
AddComponent(new Label(""),gridbag,gc);
//gc.gridx = 3;
gc.gridwidth = 15;
AddComponent(m_comboPath,gridbag,gc);
gc.gridwidth = GridBagConstraints.REMAINDER;
AddComponent(m_toolbar,gridbag,gc);
// 设置第二行组件
gc.gridwidth = 3;
gc.gridheight = GridBagConstraints.REMAINDER;
//gc.weighty = 1.0;
AddComponent(m_listSearch,gridbag,gc);
gc.weighty = 0.0;
gc.gridwidth = GridBagConstraints.REMAINDER;
gc.gridheight = 20;
AddComponent(m_listFiles,gridbag,gc);
// 设置第三行组件
//gc.weightx = 1.0;
//gc.weighty = 1.0;
gc.gridwidth = 1;
gc.gridheight = 1;
AddComponent(m_lbFileName,gridbag,gc);
gc.gridwidth = 13;
gc.weightx = 1.0;
AddComponent(m_comboFileName,gridbag,gc);
gc.gridwidth = GridBagConstraints.REMAINDER;
AddComponent(m_btnOK,gridbag,gc);
gc.gridheight = 1;
gc.gridwidth = 1;
AddComponent(m_lbFileType,gridbag,gc);
gc.gridwidth = 13;
AddComponent(m_comboFileType,gridbag,gc);
gc.gridwidth = GridBagConstraints.REMAINDER;
AddComponent(m_btnCancel,gridbag,gc);
gc.gridwidth = 1;
AddComponent(m_lbFileCode, gridbag, gc);
gc.gridwidth = 13;
AddComponent(m_comboFileCode, gridbag, gc);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
setVisible(false);
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
其中的SetGridBagLayout方法即为设置对话框布局的方法,为了试着对组件大小进行调整,我试着对倒数第二行进行调整,即将第二行的TextField对象(m_comboFileType域)放大,即将上面蓝色的代码:
gc.gridwidth = 13;
AddComponent(m_comboFileType,gridbag,gc);
改为
gc.gridwidth = 15;
AddComponent(m_comboFileType,gridbag,gc);
界面马上变成下图:
其中的取消按钮消失了,而m_comboFileType域展位远远超过预期,请各位高手帮我会诊一下,看看毛病出在哪里,还有没有其他毛病,谢谢了!
----------------解决方案--------------------------------------------------------
怎么没有人指点啊?
----------------解决方案--------------------------------------------------------
楼主可以试着下个Eclipse插件Swing,进行可视化编辑,这样的话就方便多了
----------------解决方案--------------------------------------------------------
使用setBounds(int x,int y,int length,int height);x:组件横坐标,y:组件纵坐标,length:组件长度,heigth:组件宽度
----------------解决方案--------------------------------------------------------
非常感谢以上shiying和lixingjiang两位的指点,不过两位对我的意图并未了解,由于我是一名初学者,我想搞清楚上面问题的目的是对窗口和控件的布局进行掌握,而并不是要解决目前编程的难题,说句实话我目前编制的程序就是一个练习,而没有什么实际价值,只是为了掌握和巩固java编程知识而为,所以shiying所说的简便方法在这里不是解决方案,二lixingjiang指点的方法我试过了,好像不起作用。
----------------解决方案--------------------------------------------------------