0

I'm creating a GUI in netbeans and am attempting to make it so that the components stay properly aligned when the frame is resized, minimized, maximized, etc. I've added a component listener to do this and have added the code to both formComponentMoved and formComponentResized. This works perfectly when the form is resized or moved, the problem is it doesn't work when the form is maximized or minimized. I've attached below the code for the component listener. What am I missing here?

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);
}  

Thanks in advance

4

1 回答 1

2

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

于 2013-01-23T15:08:35.627 回答