如何使用 NetBeans 从 JPanel 中删除布局?

发布于 2024-10-10 02:36:20 字数 671 浏览 5 评论 0原文

我使用 MainWindow (JFrame) 和 JPanel,使用简单的 CardLayout,CardLayout 中充满了一些 JPanel。

如果我拖动 & 就可以正常工作了将 JPanels 从选项板拖放到 CardLayout,然后将内容放入面板中。但是,我想将不同的 JPanel 放在单独的文件中,因此我使用 NetBeans 创建了一些 JPanel 表单。

我现在遇到的唯一问题是,当我将派生的 JPanel 类放到 CardLayout 上时(例如,使用 NetBeans 中的“选择 Bean”功能),NetBeans 总是为面板设置新的布局,因此我的原始布局JPanel 类被覆盖,我得到的只是一个空白的 JPanel。

那么有谁知道是否有一种方法可以简单地从我的 JPanel 类中删除布局?我的意思是我无法将布局设置为“None”或 NetBeans 的某些内容,如果我将其设置为“Null Layout”,它仍然会调用“jPanel.setLayout(null);”当然,但我只是不想调用 setLayout,这不是可能的吗?

抱歉,如果我在这里找到解决方案太愚蠢了。我对 NetBeans 还很陌生,但必须有一种方法可以手动更改代码,我不能只使用“自定义代码”选项,因为它显示“//子组件和布局的代码 - 此处未显示” ...

我希望任何人都能理解我的问题所在。 :)

I'm using a MainWindow (JFrame) with a JPanel using a simple CardLayout, the CardLayout is filled with some JPanels.

It's working fine if I drag & drop JPanels from the palette to the CardLayout and then place the contents in the panels. However, I want to place the different JPanels in separated files so I've created some JPanel Forms with NetBeans.

The only problem I have now, when I put my derived JPanel class onto the CardLayout (e.g. using the "Choose Bean" function from NetBeans) NetBeans always sets a new layout for the panel, so my original layout from the JPanel class gets overridden and all I get is a blank JPanel.

So does anyone know if there is a way to simply remove the layout from my JPanel classes? I mean I cannot set the Layout to "None" or something with NetBeans, if I set it to "Null Layout" it still invokes "jPanel.setLayout(null);" off course, but I just want no call to setLayout at all, isn't that possible somehow?

And sorry if I'm just to stupid to find the solution here. I'm quite new to NetBeans, but there must be a way to manually change the code, I can't just use the "Customize Code" option because it says "// Code of sub-components and layout - not shown here"...

I hope anyone understands what my problem is here. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

胡渣熟男 2024-10-17 02:36:20

这是不使用 GUI 生成器的主要原因之一。您应该能够在 Netbeans 中找到代码,但是,我建议您手动重新启动 GUI。它会给你更多的灵活性,并且你会对你正在做的事情有更好的理解。

This is one of the major reasons not to use a GUI Builder. You should be able to get to the code someone in Netbeans, however, I suggest to restart your GUI by hand. It will give you more flexibility and you will have a better understanding of what you are doing.

天邊彩虹 2024-10-17 02:36:20

相当烦人,不是吗?您可以像这样围绕它进行编码...

在派生的 JPanel 类在其构造函数中调用 initComponent() 后,它可以禁用对 setLayout() 的任何进一步调用 ,比如 setAllowLayoutChange(false);

将以下代码放入派生自 JPanel 的公共基类中:

protected boolean mAllowLayoutChange;

/** Creates new form CommonPanel */
public CommonPanel()
{
    super();
    mAllowLayoutChange=true;
}

public void setAllowLayoutChange(boolean b)
{
    mAllowLayoutChange=b;
}

@Override
public void setLayout(LayoutManager mgr)
{
    if(mAllowLayoutChange) super.setLayout(mgr);
}

Rather annoying, isn't it? You can code around it like this...

After your derived JPanel class calls initComponent() in its constructor, it could disable any further calls to setLayout() with, say, setAllowLayoutChange(false);.

Put the following code in a common base class that derives from JPanel:

protected boolean mAllowLayoutChange;

/** Creates new form CommonPanel */
public CommonPanel()
{
    super();
    mAllowLayoutChange=true;
}

public void setAllowLayoutChange(boolean b)
{
    mAllowLayoutChange=b;
}

@Override
public void setLayout(LayoutManager mgr)
{
    if(mAllowLayoutChange) super.setLayout(mgr);
}
等待我真够勒 2024-10-17 02:36:20

我也遇到了同样的问题,并且为此浪费了很多时间。我看不到任何 JLabel 添加到扩展类中。

我认为这是 Netbeans 设计器中令人难以置信的缺陷,都是由于愚蠢的生成且不可删除的 Panel1.setLayout(new ...

通过使用对先前答案的适应来解决的。基本上在 initComponents 之后锁定了类 setLayout。

protected boolean mAllowLayoutChange=true;

/** Creates new form CommonPanel */
public CommonPanel()
{
    initComponents();
    //no more layout changes allowed
    mAllowLayoutChange=false;
}

@Override
public void setLayout(LayoutManager mgr)
{
    if(mAllowLayoutChange) super.setLayout(mgr);
}

I had the same issue and lost a lot of time with it. I could not see any JLabel added to the extended class.

I think it is an unbelievable flaw in Netbeans designer all due to a silly generated and undeletable Panel1.setLayout(new ...

Solved it with using an addaptation to a previous answer. Basically locking the class setLayout after initComponents.

protected boolean mAllowLayoutChange=true;

/** Creates new form CommonPanel */
public CommonPanel()
{
    initComponents();
    //no more layout changes allowed
    mAllowLayoutChange=false;
}

@Override
public void setLayout(LayoutManager mgr)
{
    if(mAllowLayoutChange) super.setLayout(mgr);
}
白况 2024-10-17 02:36:20

只需将其设置为默认布局 - 这会阻止代码生成器执行任何操作。注意,Netbeans 称之为 Default/FlowLayout 但实际上它什么也没做......

Just set it to the default layout - that prevents the code generator from doing anything. Note, Netbeans calls this Default/FlowLayout but in fact it just does nothing ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文