简体   繁体   中英

Swing: GUI doesnt show my components

I am sure it's just a little mistake, but i can't find it. When I am starting my Server GUI the components in it doesnt appear. All i've got is a blank white page with the correct size.

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerGUI extends javax.swing.JFrame {

    private ServerSocket server;
    private int port;
    private int numberOfObject;
    private int counter = 1;
    private ObjectClass[] Object;
    private Server bss;
    public static volatile int[] Allocation;

    public void setServerGUI(Server bss, int port, int numberOfObject, ObjectClass[] Object) {
        this.bss = bss;
        this.port = port;
        this.numberOfObject = numberOfObject;
        this.Object = Object;
    }

    public ServerGUI() {

        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(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ServerGUI.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() {
                setVisible(true);
            }
        });
        initComponents();

    }

    public void startServing() {
        System.out.println("STARTSERVING");
        try {
            server = new ServerSocket(port);
        } catch (IOException e1) {
            System.out.println("IOException in startServing()");
        }
        while (true) {
            Socket ADClient = null;

            int free=0;
            try {
                ADClient = server.accept();
                for (int j = 0; j < numberOfObject; j++) {
                    if ((Allocation[j] == -1)) {
                        free = j;
                        Allocation[free] = counter;
                    }

                }
                System.out.println("" + Object[free].getNumber());
                new ServerThread(ADClient, counter++, Object[free]).start();


            } catch (IOException e) {
                System.out.println("IOException: server.accept() schlug fehl.");
            }
        }
    }

    /**
     * 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() {

        scrollPaneState1 = new javax.swing.JScrollPane();
        textAreaState1 = new javax.swing.JTextArea();
        scrollPaneServer1 = new javax.swing.JScrollPane();
        textAreaServer1 = new javax.swing.JTextArea();
        scrollPaneState2 = new javax.swing.JScrollPane();
        textAreaState2 = new javax.swing.JTextArea();
        scrollPaneServer2 = new javax.swing.JScrollPane();
        textAreaServer2 = new javax.swing.JTextArea();
        scrollPaneState3 = new javax.swing.JScrollPane();
        textAreaState3 = new javax.swing.JTextArea();
        scrollPaneServer3 = new javax.swing.JScrollPane();
        textAreaServer3 = new javax.swing.JTextArea();
        scrollPaneMain = new javax.swing.JScrollPane();
        textAreaMain = new javax.swing.JTextArea();
        labelNr1 = new javax.swing.JLabel();
        labelNr2 = new javax.swing.JLabel();
        labelNr3 = new javax.swing.JLabel();
        labelStateResponse1 = new javax.swing.JLabel();
        labelStateResponse2 = new javax.swing.JLabel();
        labelStateResponse3 = new javax.swing.JLabel();
        labelServerResponse1 = new javax.swing.JLabel();
        labelServerResponse2 = new javax.swing.JLabel();
        labelServerResponse3 = new javax.swing.JLabel();
        labelMainResponse = new javax.swing.JLabel();
        beendenButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server - Running");

        scrollPaneState1.setEnabled(false);

        textAreaState1.setColumns(20);
        textAreaState1.setRows(5);
        scrollPaneState1.setViewportView(textAreaState1);

        scrollPaneServer1.setEnabled(false);

        textAreaServer1.setColumns(20);
        textAreaServer1.setRows(5);
        scrollPaneServer1.setViewportView(textAreaServer1);

        scrollPaneState2.setEnabled(false);

        textAreaState2.setColumns(20);
        textAreaState2.setRows(5);
        scrollPaneState2.setViewportView(textAreaState2);

        scrollPaneServer2.setEnabled(false);

        textAreaServer2.setColumns(20);
        textAreaServer2.setRows(5);
        scrollPaneServer2.setViewportView(textAreaServer2);

        scrollPaneState3.setEnabled(false);

        textAreaState3.setColumns(20);
        textAreaState3.setRows(5);
        scrollPaneState3.setViewportView(textAreaState3);

        scrollPaneServer3.setEnabled(false);

        textAreaServer3.setColumns(20);
        textAreaServer3.setRows(5);
        scrollPaneServer3.setViewportView(textAreaServer3);

        scrollPaneMain.setEnabled(false);

        textAreaMain.setColumns(20);
        textAreaMain.setRows(5);
        scrollPaneMain.setViewportView(textAreaMain);

        labelNr1.setText("Nr: ");
        labelNr1.setDoubleBuffered(true);

        labelNr2.setText("Nr: ");

        labelNr3.setText("Nr: ");

        labelStateResponse1.setText("State Respone:");

        labelStateResponse2.setText("State Response:");

        labelStateResponse3.setText("State Response:");

        labelServerResponse1.setText("Server Resonse:");

        labelServerResponse2.setText("Server Response:");

        labelServerResponse3.setText("Server Response:");

        labelMainResponse.setText("Main Response:");

        beendenButton.setText("beenden");
        beendenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                beendenButtonActionPerformed(evt);
            }
        });

        backButton.setText("zurück");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(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()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelNr1)
                                    .addComponent(labelStateResponse1))
                                .addGap(199, 199, 199)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(scrollPaneState2, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(labelServerResponse2)
                                    .addComponent(labelStateResponse2)
                                    .addComponent(labelNr2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(scrollPaneState3)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(labelServerResponse3)
                                            .addComponent(labelStateResponse3)
                                            .addComponent(labelNr3))
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(scrollPaneServer1)
                                    .addComponent(scrollPaneState1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scrollPaneServer2, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scrollPaneServer3))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelServerResponse1)
                                .addGap(738, 738, 738))
                            .addComponent(scrollPaneMain)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(labelMainResponse))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(326, 326, 326)
                                .addComponent(backButton)
                                .addGap(18, 18, 18)
                                .addComponent(beendenButton)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNr1)
                    .addComponent(labelNr2)
                    .addComponent(labelNr3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelStateResponse1)
                    .addComponent(labelStateResponse2)
                    .addComponent(labelStateResponse3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrollPaneState2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scrollPaneState1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scrollPaneState3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelServerResponse1)
                    .addComponent(labelServerResponse2)
                    .addComponent(labelServerResponse3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPaneServer2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scrollPaneServer1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scrollPaneServer3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelMainResponse)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPaneMain, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(beendenButton)
                    .addComponent(backButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void beendenButtonActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {
        bss.setVisible(true);
        this.dispose();
    }

    // Variables declaration - do not modify
    private javax.swing.JButton backButton;
    private javax.swing.JButton beendenButton;
    private javax.swing.JLabel labelNr1;
    private javax.swing.JLabel labelNr2;
    private javax.swing.JLabel labelNr3;
    private javax.swing.JLabel labelMainResponse;
    private javax.swing.JLabel labelServerResponse1;
    private javax.swing.JLabel labelServerResponse2;
    private javax.swing.JLabel labelServerResponse3;
    private javax.swing.JLabel labelStateResponse1;
    private javax.swing.JLabel labelStateResponse2;
    private javax.swing.JLabel labelStateResponse3;
    private javax.swing.JScrollPane scrollPaneMain;
    private javax.swing.JScrollPane scrollPaneServer1;
    private javax.swing.JScrollPane scrollPaneServer2;
    private javax.swing.JScrollPane scrollPaneServer3;
    private javax.swing.JScrollPane scrollPaneState1;
    private javax.swing.JScrollPane scrollPaneState2;
    private javax.swing.JScrollPane scrollPaneState3;
    private javax.swing.JTextArea textAreaMain;
    private javax.swing.JTextArea textAreaServer1;
    private javax.swing.JTextArea textAreaServer2;
    private javax.swing.JTextArea textAreaServer3;
    private javax.swing.JTextArea textAreaState1;
    private javax.swing.JTextArea textAreaState2;
    private javax.swing.JTextArea textAreaState3;
    // End of variables declaration
}

This Class get called over here:

ServerGUI bssGUI = new ServerGUI();
bssGUI.setServerGUI(this, 4242, object.length,object);
bssGUI.setVisible(true);
bssGUI.startServing();

That's alot of code, one of the downsides with using netbeans :) The only thing I can spot is that you should call initComponents(); before you call setVisible(true);

Also, make sure that you are not catching any of your exceptions by reading the logfile you are creating (or add printlines)

It seems that you doing initialization on one thread, but calling setVisible method on another thread (wrapped in invokeLater in constructor). It is generally bad approach to call UI stuff from thread other than Event Dispatcher Thread, to say nothing of resulting race conditions. Try to remove invokeLater from constructor, and wrap initalization instead:

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        ServerGUI bssGUI = new ServerGUI();
        bssGUI.setServerGUI(this, 4242, object.length,object);
        bssGUI.setVisible(true);
        bssGUI.startServing();
    }
};

As I can see this code is generated with netbeans.. redesign the page as it is a matter of drag and drop and copy the functionality :-only the business functionality at the appropriate places.. be careful where u are putting the business functionality becse if u put it in the wrong place then u might end up with this same problem again. for example.. if u are writing code for a listener then as given below

    backButton.addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent evt) { 
            backButtonActionPerformed(evt); 
        } 
    });

is correct . the good thing to notice here is that u wrap up ur code in a method and call that method from the place where u are intending to invoke it from.

and u can be care ful about the answer posted y jimmy. I feel it will be wastage of time to go thru this code and find out the probblem. its matter of drag and drop.

Also keep in mind that no componets should overlap and there should be no extra components that have been deleted but their memory has not been released.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM