Gui Builder的源代码并不能成为gui

时间:2016-01-08 11:44:23

标签: java swing user-interface netbeans gui-builder

我使用Netbeans Builder设计了一个JPanel。我复制了源代码并在此类中使用它。我还使用构造函数添加一个main方法。运行该文件,成功构建,但Java VM doest打开任何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("");
    }
}

1 个答案:

答案 0 :(得分:1)

JPanel延伸JComponent

JComponent的文档中,写有:

  

除顶级容器外的所有Swing组件的基类    要使用继承自JComponent的组件,    您必须将组件放在包含层次结构中    其根是顶级Swing容器。

顶级Swing容器表示JFrameJApplet

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

有关详情,请访问:https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html