GUI不会显示

时间:2016-03-20 14:50:10

标签: java user-interface

public class NewClass extends javax.swing.JFrame {

    /**
     * Creates new form NewClass
     */
    public NewClass() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 583, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(285, 285, 285)
                        .addComponent(jButton1)))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(39, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 506, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewClass().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration  

伙计这是我第一次尝试从Netbeans生成器创建GUI时,没有编译错误但没有显示任何弹出窗口。这不是我的代码是创建设计部分后自动生成的代码。但是我尝试手动相同没有编译错误但没有弹出

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assigment1;

import java.awt.*;

import javax.swing.*;


public class RectangleProgram extends JFrame

{

    private static final int WIDTH = 400;

    private static final int HEIGHT = 300;



    private JLabel lengthL, widthL, areaL, perimeterL;

    private JTextField lengthTF, widthTF, areaTF, perimeterTF;

    private JButton calculateB, exitB;


    public RectangleProgram()

    {

        //Instantiate the labels:

        lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);

        widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);

        areaL = new JLabel("Area: ", SwingConstants.RIGHT);

        perimeterL = new JLabel("Perimeter: ", SwingConstants.RIGHT);



        //Text fields next:

        lengthTF = new JTextField(10);

        widthTF = new JTextField(10);

        areaTF = new JTextField(10);

        perimeterTF = new JTextField(10);



        //Buttons too:

        calculateB = new JButton("Calculate");

        exitB = new JButton("Exit");



        //Set the window's title.

        setTitle("Sample Title: Area of a Rectangle");



        //Get the content pane (CP).

        Container pane = getContentPane();



        //Set the layout.

        pane.setLayout(new GridLayout(5, 2));



        //Other JFrame stuff.

        setSize(WIDTH, HEIGHT);

        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }



}

这是另一个不会弹出或显示任何错误消息的示例代码,这是第一次在此计算机中使用NetBeans,顺便说一下,这是一台新计算机。

怎么办:(。

0 个答案:

没有答案