Gui Builders 源代码不生成 gui

Gui Builder's source code doesnt make gui

我使用 Netbeans 设计了一个 JPanel Builder.I 复制了源代码并在此使用它 class.I 还添加了一个带有 constructor.Run 文件的主要方法,构建成功但是 Java VM 不打开任何 gui。 这是完整的代码。

import java.awt.Dimension;
import javax.swing.*;
/**
 *
 * @author Fotis
 */
public class Gui extends javax.swing.JPanel {

    public javax.swing.JButton exitBtn;
    public javax.swing.JTextArea filePreviewArea;
    public javax.swing.JList folderList;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel2;
    public javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JScrollPane jScrollPane2;
    public javax.swing.JButton openFileBtn;
    public javax.swing.JButton openFolderBtn;
    public javax.swing.JLabel welcomeLabel;

    /**
     * Creates new form Gui
     */
    public static void main (String[] args){
        Gui a=new Gui();
        a.createGui();
        a.setPreferredSize(new Dimension(800,600));
    }

    @SuppressWarnings("unchecked")
    public void createGui() {

        jPanel1 = new javax.swing.JPanel();
        welcomeLabel = new javax.swing.JLabel();
        openFolderBtn = new javax.swing.JButton();
        exitBtn = new javax.swing.JButton();
        openFileBtn = new javax.swing.JButton();


        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        filePreviewArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        folderList = new javax.swing.JList();

        jPanel1.setMaximumSize(new java.awt.Dimension(32767, 60));
        jPanel1.setMinimumSize(new java.awt.Dimension(800, 60));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 60));

        welcomeLabel.setText("Welcome to Alpha Reader.Choose a single file or a folder.");

        openFolderBtn.setLabel("Open Folder");
        openFolderBtn.setPreferredSize(new java.awt.Dimension(100, 32));

        exitBtn.setLabel("Exit");
        exitBtn.setName(""); // NOI18N
        exitBtn.setPreferredSize(new java.awt.Dimension(100, 32));

        openFileBtn.setLabel("Open File");
        openFileBtn.setMaximumSize(new java.awt.Dimension(100, 32));
        openFileBtn.setMinimumSize(new java.awt.Dimension(100, 32));
        openFileBtn.setPreferredSize(new java.awt.Dimension(100, 32));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(welcomeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addComponent(openFileBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(openFolderBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(exitBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(welcomeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(openFolderBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exitBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(openFileBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        welcomeLabel.getAccessibleContext().setAccessibleName("WelcomeLabel");
        welcomeLabel.getAccessibleContext().setAccessibleDescription("");
        openFolderBtn.getAccessibleContext().setAccessibleName("openFolderBtn");
        exitBtn.getAccessibleContext().setAccessibleName("exitBtn");
        openFileBtn.getAccessibleContext().setAccessibleName("openBtn");

        jPanel2.setMinimumSize(new java.awt.Dimension(800, 100));
        jPanel2.setPreferredSize(new java.awt.Dimension(800, 536));

        filePreviewArea.setColumns(20);
        filePreviewArea.setRows(5);
        jScrollPane1.setViewportView(filePreviewArea);

        folderList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(folderList);
        folderList.getAccessibleContext().setAccessibleName("folderList");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 32, Short.MAX_VALUE))
        );

        jPanel1.getAccessibleContext().setAccessibleName("");
        jPanel1.getAccessibleContext().setAccessibleDescription("");
    }
}

JPanel 扩展 JComponent.

JComponent的文档中写道:

The base class for all Swing components except top-level containers.
To use a component that inherits from JComponent, you must place the component in a containment hierarchy whose root is a top-level Swing container.

top-level Swing 容器 表示 JFrameJApplet

因此,您需要其中之一来显示您的 GUI。

更多信息,您可以访问:https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html