首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Java Swing窗口大小监听解决办法

2012-03-04 
Java Swing窗口大小监听今天用SWING实现一个窗口,然后希望在修改窗口大小的同时,修改内部一些控件的大小,

Java Swing窗口大小监听
今天用SWING实现一个窗口,然后希望在修改窗口大小的同时,修改内部一些控件的大小,于是继承了ComponentAdapter类,但是写完以后,发现每次拖动修改后,内部组件大小并没有实时改变,而是在下次再修改外部窗口大小时,内部控件才会修改成上次设置的值。。
  求问,如何使用ComponentAdapter做到实时更新内部控件的大小,如果该方法不行,是否有其他方法,望高手指教。。。

import com.ms.main.MainUI;
import com.ms.util.Constants;
public class MainComponentAdapt extends ComponentAdapter {

private MainUI mainUI = new MainUI();

public MainComponentAdapt(){}

public MainComponentAdapt(MainUI mainUI){
this.mainUI = mainUI;
}

/* (non-Javadoc)
* @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent)
* 监听窗口大小的变化
*/
public void componentResized(ComponentEvent arg0) {
System.out.println(this.mainUI.getSize().width);
System.out.println(this.mainUI.getSize().height);

int w = this.mainUI.getSize().width - Constants.MAIN_WID;
int h = this.mainUI.getSize().height - Constants.MAIN_HEI;

if(w != 0){
Constants.MAIN_USERUI_WID = Constants.MAIN_USERUI_WID + w;
Constants.MAIN_SEARCHUI_WID = Constants.MAIN_SEARCHUI_WID + w;
Constants.MAIN_SEARCHUI_TEXT_WID = Constants.MAIN_SEARCHUI_TEXT_WID + w;
Constants.MAIN_TASKUI_WID = Constants.MAIN_TASKUI_WID + w;
Constants.MAIN_FUNCTIONUI_WID = Constants.MAIN_FUNCTIONUI_WID + w;
Constants.MAIN_FUNCTIONUI_MSG_WID = Constants.MAIN_FUNCTIONUI_MSG_WID + w;
Constants.MAIN_WID = this.mainUI.getSize().width;
}

if(h != 0 ){
Constants.MAIN_FUNCTIONUI_HEI = Constants.MAIN_FUNCTIONUI_HEI + h;
Constants.MAIN_FUNCTIONUI_CHI_HEI = Constants.MAIN_FUNCTIONUI_CHI_HEI + h;
Constants.MAIN_FUNCTIONUI_MSG_HEI = Constants.MAIN_FUNCTIONUI_MSG_HEI + h;
Constants.MAIN_HEI = this.mainUI.getSize().height;
}

if(w != 0 || h !=0){
System.out.println(w);
System.out.println(h);
  //设置内部控件的大小
Dimension dim = new Dimension(Constants.MAIN_USERUI_WID,Constants.MAIN_USERUI_HEI);
this.mainUI.getUserUI().setMaximumSize(dim);
this.mainUI.getUserUI().setMinimumSize(dim);
this.mainUI.getUserUI().setPreferredSize(dim);
dim = new Dimension(Constants.MAIN_SEARCHUI_WID,Constants.MAIN_SEARCHUI_HEI);
this.mainUI.getSearchUI().setPreferredSize(dim);
this.mainUI.getSearchUI().setMaximumSize(dim);
this.mainUI.getSearchUI().setMinimumSize(dim);
dim = new Dimension(Constants.MAIN_SEARCHUI_TEXT_WID,Constants.MAIN_SEARCHUI_TEXT_HEI);
this.mainUI.getSearchUI().getSearchText().setPreferredSize(dim);
this.mainUI.getSearchUI().getSearchText().setMaximumSize(dim);
this.mainUI.getSearchUI().getSearchText().setMinimumSize(dim);
dim = new Dimension(Constants.MAIN_FUNCTIONUI_WID,Constants.MAIN_FUNCTIONUI_HEI);
this.mainUI.getFunctionUI().setPreferredSize(dim);
this.mainUI.getFunctionUI().setMaximumSize(dim);
this.mainUI.getFunctionUI().setMinimumSize(dim);
dim = new Dimension(Constants.MAIN_FUNCTIONUI_CHI_WID,Constants.MAIN_FUNCTIONUI_CHI_HEI);
this.mainUI.getFunctionUI().getFunctionCHI().setPreferredSize(dim);
this.mainUI.getFunctionUI().getFunctionCHI().setMaximumSize(dim);
this.mainUI.getFunctionUI().getFunctionCHI().setMinimumSize(dim);
dim = new Dimension(Constants.MAIN_FUNCTIONUI_MSG_WID,Constants.MAIN_FUNCTIONUI_MSG_HEI);
this.mainUI.getFunctionUI().getFunctionMSG().setPreferredSize(dim);
this.mainUI.getFunctionUI().getFunctionMSG().setMaximumSize(dim);
this.mainUI.getFunctionUI().getFunctionMSG().setMinimumSize(dim);
dim = new Dimension(Constants.MAIN_TASKUI_WID,Constants.MAIN_TASKUI_HEI);
this.mainUI.getTaskUI().setPreferredSize(dim);
this.mainUI.getTaskUI().setMaximumSize(dim);
this.mainUI.getTaskUI().setMinimumSize(dim);

//this.mainUI.getRootPane().repaint();
}
}


}


 

[解决办法]
我最近也在研究Swing,我用Netbeans里带的Swing布局管理器进行布局,在设计窗口可以实时看到组件改动情况,或许我们可以交流交流:企鹅:517723961

热点排行