使用 NetBeans 与 Eclipse 进行 Java 布局定位

发布于 2024-12-23 16:22:46 字数 3107 浏览 0 评论 0原文

我正在尝试创建一个简单的幻灯片程序,例如 Powerpoint。为了设计它的 GUI,我使用了 Netbeans,然后将 GUI 代码从 Netbeans 复制到 Eclipse。

Netbeans 生成了以下代码:

    pg = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jPanel1 = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(153, 153, 153));

    pg.setBackground(new java.awt.Color(255, 255, 255));
    pg.setForeground(new java.awt.Color(255, 255, 255));
    pg.setPreferredSize(new java.awt.Dimension(255, 234));

    javax.swing.GroupLayout pgLayout = new javax.swing.GroupLayout(pg);
    pg.setLayout(pgLayout);
    pgLayout.setHorizontalGroup(
        pgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 357, Short.MAX_VALUE)
    );
    pgLayout.setVerticalGroup(
        pgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 257, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 62, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );


    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(pg, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(pg, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();

我的问题是,我想将右侧的 jPanel1 更改为我自己的扩展 JPanel 的类。为此,我写道:

JPanel jPanel1 = new JPanel();  
jPanel1.add(new ToolBoxGUI("ToolBox"));

但是当我运行代码时,右侧没有 ToolBoxGUI 类按钮。我应该如何解决这个问题?

I'm trying to create a simple slideshow program like Powerpoint. In order to design its GUI, I used Netbeans and, then copied the GUI code from Netbeans to Eclipse.

Netbeans generated the following code:

    pg = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jPanel1 = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(153, 153, 153));

    pg.setBackground(new java.awt.Color(255, 255, 255));
    pg.setForeground(new java.awt.Color(255, 255, 255));
    pg.setPreferredSize(new java.awt.Dimension(255, 234));

    javax.swing.GroupLayout pgLayout = new javax.swing.GroupLayout(pg);
    pg.setLayout(pgLayout);
    pgLayout.setHorizontalGroup(
        pgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 357, Short.MAX_VALUE)
    );
    pgLayout.setVerticalGroup(
        pgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 257, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 62, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );


    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(pg, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(pg, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();

My problem is, I want to change the jPanel1 on the right to my own class which extends JPanel. To do this, I wrote:

JPanel jPanel1 = new JPanel();  
jPanel1.add(new ToolBoxGUI("ToolBox"));

But when I run the code, there are no ToolBoxGUI class buttons on the right. How should I solve this problem?

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

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

发布评论

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

评论(1

落在眉间の轻吻 2024-12-30 16:22:46

jPanel1 使用 GroupLayout。为了可见,您的 ToolBoxGUI 必须添加到此 GroupLayout 中。如果您想要使 ToolBoxGUI 成为 jPanel1 的唯一组件,则删除设置 jPanel1 布局的代码:

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 62, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 279, Short.MAX_VALUE)
);

并将其替换为

jPanel1.setLayout(new BorderLayout());

或者,如果您想用 ToolBoxGUI 替换 jPanel1,请删除与上面相同的代码,然后初始化 jPanel1

jPanel1 = new ToolBoxGUI();

jPanel1 uses a GroupLayout. To be visible, your ToolBoxGUI would have to be added to this GroupLayout. If what you want is to make the ToolBoxGUI the unique component of jPanel1, then remove the code which sets the layout of jPanel1:

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 62, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 279, Short.MAX_VALUE)
);

and replace it with

jPanel1.setLayout(new BorderLayout());

Or, if you want to replace jPanel1 with the ToolBoxGUI, remove the same code as above, and initialize jPanel1 with

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