java swing组件监听器的问题

时间:2013-01-23 15:06:20

标签: java swing alignment awt componentlistener

我正在netbeans中创建一个GUI,并且我试图使它在框架调整大小,最小化,最大化等时保持组件正确对齐。我添加了一个组件监听器来执行此操作并添加了formComponentMoved和formComponentResized的代码。当窗体调整大小或移动时,这非常有效,问题是当窗体最大化或最小化时它不起作用。我已经在组件监听器的代码下面附加了。我在这里缺少什么?

private void formComponentResized(java.awt.event.ComponentEvent evt) {                                      
     jSplitPane1.setSize(new Dimension((int)(this.getWidth()*.98), (int)(this.getHeight()*.6)));
     jSplitPane1.setDividerLocation(jSplitPane1.getWidth()/2);
     jPanel1.setSize(new Dimension(jSplitPane1.getWidth()/2, jSplitPane1.getHeight()));
     jPanel2.setSize(new Dimension(jSplitPane1.getWidth()/2, jSplitPane1.getHeight())); 
     zoomToPeak.setLocation(jPanel2.getX() + 5, jPanel2.getY() + jPanel2.getHeight() + 30);
     peak.setLocation(zoomToPeak.getX() + zoomToPeak.getWidth() + 10, zoomToPeak.getY() + 1);

}                                     

private void formComponentMoved(java.awt.event.ComponentEvent evt) {                                    
    jSplitPane1.setSize(new Dimension((int)(this.getWidth()*.98), (int)(this.getHeight()*.6)));
    jSplitPane1.setDividerLocation(jSplitPane1.getWidth()/2);
    jPanel1.setSize(new Dimension(jSplitPane1.getWidth()/2, jSplitPane1.getHeight()));
    jPanel2.setSize(new Dimension(jSplitPane1.getWidth()/2, jSplitPane1.getHeight())); 
    zoomToPeak.setLocation(jPanel2.getX() + 5, jPanel2.getY() + jPanel2.getHeight() + 30);
    peak.setLocation(zoomToPeak.getX() + zoomToPeak.getWidth() + 10, zoomToPeak.getY() + 1);
}  

提前致谢

1 个答案:

答案 0 :(得分:2)

您缺少布局管理器。它们是保持组件对齐的正确方法。