调整布局中的组件的大小

246 阅读1分钟

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
    
}

}