JPanel 不显示在 BorderLayout 中
我有一个边框布局,右侧有一个 JPanel。 JPanel 具有绝对布局,并且它不包含任何内容,因为我使用它来绘制图形(覆盖 PaintComponent(Graphics g))。
问题是面板不显示(它在那里,但宽度约为 1 个像素)。我尝试在面板上设置大小..但这不起作用..
我该怎么做?
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
JPanel panelCenter = new JPanel();
panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setSize(200, 400);
panelRight.setLayout(null);
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
table = new JTable(new MyTableModel());
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);
I have a border layout, and a JPanel on the right. The JPanel has absolute layout, and it doesn't contain anything because I'm using it to draw graphics (overriding paintComponent(Graphics g))
The problem is that the panel doesn't show (it's there but like 1 pixel wide). I tried to setSize on the panel.. but that doesn't work..
How do I do it?
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
JPanel panelCenter = new JPanel();
panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setSize(200, 400);
panelRight.setLayout(null);
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
table = new JTable(new MyTableModel());
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 JPanel.setPreferredSize() 方法。另外,我认为 JFrame.setBounds() 调用不是您正在寻找的。这是一个例子:
Look into the JPanel.setPreferredSize() method. Also, I think the JFrame.setBounds() call is not what you are looking for. Here's an example:
BorderLayout 遵循东部组件的首选大小,并在必要时拉伸其高度。您是否尝试过设置面板的首选大小(或重写 getPreferredSize 方法以返回您喜欢的大小)?
BorderLayout honors the preferred size of the east component, and stretches its height if necessary. Have you tried setting the preferred size of the panel (or overriding the
getPreferredSize
method to return the size you prefer)?解决方案来了!!!我也在做同样的事情。
所以基本上 JLabel 要么位于南方,要么位于北方,它将有力地拉伸 MyDraw。
here comes the solution!!! I was doing the same stuff.
So basically the JLabel is either located in the South or North and it is going to strech the MyDraw forcefully.