如何在 JFrame 和 JPanel 中设置玻璃窗格的位置
我想在 JFrame 上显示玻璃面板。我可以做到这一点,现在我的问题是,玻璃板在左上角可见。我想知道如何根据需要设置玻璃窗格的位置(JFrame 中的给定位置或 JFrame 上的 JPanel)。我尝试使用设置位置 设置位置(x,y);和 setBounds(x,y,w,h);但它并不成功。我使用 Netbeans。这是我的代码如何显示玻璃板。
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));}
这是我的测试代码。这里我在玻璃板上放置了一个 JList 和一些面板。请任何人告诉我如何设置玻璃板的位置。谢谢
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。即使您没有发布整个相关代码,我也会尝试给出答案。
我想“g”是您想要像 GlassPane 一样使用的组件。
您不必将其他组件添加到玻璃窗格本身,而是像通常没有玻璃窗格一样构建组件层次结构。然后使用 setGlassPane() 方法。
这是一个很好的教程。
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.