如何可视化缺失的内面板?
我正在尝试形成以下框架:
主框架使用 BorderLayout。
在此框架中,我添加了一个使用 BoxLayout 的面板 - 我将其称为 P1。
由于某种原因,当我运行程序时看不到我添加到 P1 中的面板。
更令人困惑的是,如果 P1 使用 GridLayout 而不是 BoxLayout,则会显示我添加到 P1 中的所有面板。
代码中使用的 EventPanel 类扩展了 Panel 并使用 SpringLayout。
有什么想法可以让 P1 发挥作用吗?
这是相关代码:
public static void main(String[] args)
{
//Setting Frame
JFrame m_CalendarFrame = new JFrame();
m_CalendarFrame.getContentPane().setLayout(new BorderLayout());
//Setting inner Panel
JPanel P1 = new JPanel();
P1.setLayout(new BoxLayout(P1, BoxLayout.Y_AXIS));
EventPanel Ev = new EventPanel("8:00", "16:00", "Bday", "Go party!");
EventPanel Ev2 = new EventPanel("8:00", "16:00", "Java", "Handing Java Project");
//Adding Two pannels into previous inner Panel
P1.add(Ev);
P1.add(Ev2);
m_CalendarFrame.getContentPane().add(P1, BorderLayout.NORTH);
m_CalendarFrame.setVisible(true);
m_CalendarFrame.setSize(800,800);
}
I am trying to form the following Frame:
The main Frame uses a BorderLayout.
Into at this Frame and I added a Panel which uses BoxLayout - I'll call it P1.
For some reason the Panels I add into P1 are not seen when I run the program.
What's even more confusing is that if P1 uses a GridLayout instead of a BoxLayout, all the Panels I added into P1 are shown.
The EventPanel Class used in the code extends Panel and uses a SpringLayout.
Any ideas how to make P1 work?
Here's the relevant code:
public static void main(String[] args)
{
//Setting Frame
JFrame m_CalendarFrame = new JFrame();
m_CalendarFrame.getContentPane().setLayout(new BorderLayout());
//Setting inner Panel
JPanel P1 = new JPanel();
P1.setLayout(new BoxLayout(P1, BoxLayout.Y_AXIS));
EventPanel Ev = new EventPanel("8:00", "16:00", "Bday", "Go party!");
EventPanel Ev2 = new EventPanel("8:00", "16:00", "Java", "Handing Java Project");
//Adding Two pannels into previous inner Panel
P1.add(Ev);
P1.add(Ev2);
m_CalendarFrame.getContentPane().add(P1, BorderLayout.NORTH);
m_CalendarFrame.setVisible(true);
m_CalendarFrame.setSize(800,800);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜问题出在您使用 SpringLayout 的面板中。首先阅读有关 如何使用 SpringLayout 的 Swing 教程示例和解释。
如果您仍然需要帮助,请发布您的 SSCCE 来说明您的问题。并从简单开始。尝试将 SpringLayout 与一个组件一起使用,然后是两个,然后三个,直到您感觉更舒服为止。
I'm guess the problem is in your panel that uses the SpringLayout. Start by reading the Swing tutorial on How to Use SpringLayout for working examples and explanations.
If you still need help then post your SSCCE that demonstrates your problem. And start simple. Try using the SpringLayout with one comopnent, then two, then three until you feel more comforatable with it.
我相信这与可用空间管理有关,但我自己没有测试过。
BoxLayout
使用其绘制的组件的首选大小,而GridLayout
在绘制的对象之间均匀分配可用空间。如果您为
EventPanel
指定首选大小,会发生什么?根据您的代码示例,您可以通过强制EventPanel
实例的首选大小 (800,400) 来快速测试这一点,并查看使用BoxLayout
时它是否确实发生了变化。I believe it has to do with free space management, but I haven't tested it myself. The
BoxLayout
uses the preferred size for the components it draws, and theGridLayout
distributes the available space evenly among the objects drawn.What happens if you specify a preferred size for your
EventPanel
? Based on your code example, you can quickly test this by forcing a preferred size of (800,400) for yourEventPanel
instances, and see if it does change something when using theBoxLayout
.