GridBagLayout中的JPanel位置不正确

编程入门 行业动态 更新时间:2024-10-28 10:27:25
本文介绍了GridBagLayout中的JPanel位置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

im使用GridBag在JScrollPane中显示带有图像的一些JPanel. 当有3张或3张以上图像时,GridBagConstraints可以正常工作,但当我有1张或2张图像时,它们会与JScrollPane的中心对齐,而不是位于顶部(例如在画廊中) 这是我的代码:

im using GridBag to display some JPanels with images inside a JScrollPane. When there are 3 or more images the GridBagConstraints work ok but when i have 1 or 2, they get aligned to the center of the JScrollPane instead of being in their position in the top (like in a gallery) Here is my code:

JPanel jPanel1 = new JPanel(); GridBagLayout layout = new GridBagLayout(); jPanel1.setLayout(layout); GridBagConstraints gbc = new GridBagConstraints(); JPanel photo = new JPanel(); Dimension d = new Dimension(100,100); photo.setPreferredSize(d); gbc.insets = new Insets(0,3,3,3); gbc.gridx = i; gbc.gridy = j; jPanel1.add(photo, gbc); jScrollPane1.setViewportView(jPanel1);

我已经省略了将图像分配给Jpanel图片的部分. 我希望JPanels在它们的位置保持静态,并且如果有可用空间,则不要对齐. 如果即时通讯不清楚,我可以上传一些快照. 谢谢!

I have omitted the part where i assign an image to the photo Jpanel. I want the JPanels to stay static in their places and do not align if there is free space. If im not being clear i can upload a few snaps. Thanks!

推荐答案

GridBagLayout将其组件围绕容器的中心布局,这是它的(有时是令人讨厌的)默认功能.

GridBagLayout layouts its components out around the center of the container, this is it's (and sometimes annoying) default functionality.

您可以尝试添加一个空的填充"组件(例如JLabel),其中weightx=1和weighty=1的GridBagConstraints在容器中其他组件的右侧和下方.这将迫使它们到达容器的左上角...

You could try adding an empty "filler" component (say a JLabel) with the GridBagConstraints of weightx=1 and weighty=1 at a position right of and below the other components in the container. This will force them to the top/left corner of the container...

已更新...

居中/默认...

import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class GridBagLayoutTest { public static void main(String[] args) { new GridBagLayoutTest(); } public GridBagLayoutTest() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.setSize(600, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; JLabel picture = new JLabel(); try { picture.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/your/picture")))); } catch (IOException ex) { ex.printStackTrace(); picture.setText("Bad picture"); } add(picture, gbc); } } }

已对齐...

import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class GridBagLayoutTest { public static void main(String[] args) { new GridBagLayoutTest(); } public GridBagLayoutTest() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.setSize(600, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; JLabel picture = new JLabel(); try { picture.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/your/picture")))); } catch (IOException ex) { ex.printStackTrace(); picture.setText("Bad picture"); } add(picture, gbc); JLabel filler = new JLabel(); gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 1; gbc.weighty = 1; add(filler, gbc); } } }

更多推荐

GridBagLayout中的JPanel位置不正确

本文发布于:2023-10-08 02:13:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   位置   GridBagLayout   JPanel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!