开发者

How to set postion to a glass pane in a JFrame and JPanel

开发者 https://www.devze.com 2023-04-05 10:44 出处:网络
I want to show a glasspane over my JFrame. I could do that and now my problem is, the glasspane is visible on the top left开发者_JS百科 corner. I want to know how to set glass pane\'s position as I wa

I want to show a glasspane over my JFrame. I could do that and now my problem is, the glasspane is visible on the top left开发者_JS百科 corner. I want to know how to set glass pane's position as I want(A given location in a JFrame or a JPanel on a JFrame). I tried to set location using setPostion(x,y); and setBounds(x,y,w,h); But it was not successful. I use Netbeans.Here is my code how to show a glasspane.

public void showPanelMy(){

    javax.swing.JList jList1;
    javax.swing.JPanel jPanel1;
    javax.swing.JScrollPane jScrollPane1;

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();
    JPanel g=(JPanel)myJFrame.getGlassPane();
    g.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
    setMaximumSize(new java.awt.Dimension(100, 100));
    g.setBackground(new java.awt.Color(204, 92, 92));

    g.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jList1.setModel(new javax.swing.AbstractListModel() {....}
    });
    jScrollPane1.setViewportView(jList1);
    jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 120, 120));

    g.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 140, 140));}

This is a my testing code. Here I put a JList and some panels on the glasspane. Please any one tell my how to set position to glass pane. Thank you


Ok. I'll try to give an answer even if you didn't post the whole relevant code.

I suppose "g" is the component you want to use like a GlassPane. You don't have to add other Components to the glass pane itself, but instead construct your component hierarchy like you usually do without glasspane. Then add the glass pane to the frame with setGlassPane() method of JFrame.

Here's a nice tutorial.

0

精彩评论

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

关注公众号