开发者

Java : JLayeredPane resize problem

I am writing a program GUI in java. I want to hava a JLayeredPane as ContentPane of my JFrame. On my JLayeredPane there are two layers : one layer a Canvas(canvas1) and another layer has a JPanel(jPanel5). I use Netbeans IDE and this code is generated:

    canvas1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    canvas1.setBounds(0, 0, -1, -1);
    jLayeredPane1.add(canvas1, javax.swing.JLayeredPane.PALETTE_LAYER);

    jPanel5.setBounds(0, 0, 670, 550);
    jLayeredPane1.add(jPanel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
    );

pack();

whe开发者_如何学Gon I run my program I saw a strange event! When I change size of tje JFrame; the size of main JLayeredPane did not change. I used diffrerent backGround colours for components and it is obvious to me that the size of JLayered is permanent however I set it vertical and horizonal resizable in it's properties. The JFrame is resizable but main jLayeredPane's size is permamnet.

How can I cause the jLayeredPane to resize as my JFrame ?!

thank you

Java : JLayeredPane resize problem


Try using the default BorderLayout instead of the GroupLayout you're using now. Simply comment out the group of lines that start with creating the GroupLayout and add this line:

getContentPane().add(jLayeredPane, BorderLayout.CENTER);

The BorderLayout.CENTER parameter to the .add() method tells the BorderLayout manager that you want the given widget to fill the center of your window.


You can change the JLayeredPane along with the JFrame. While in (NetBeans IDE) Design mode - right click on the JLayeredPane, then on the popup menu hover over "Auto Resizing", then check bothe verticaland horizontal. That way the size of the JLayeredPane will change relatively to the JFrame.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜