Swing:GUI不显示我的组件

时间:2012-04-03 13:22:24

标签: java swing user-interface

我确信这只是一个小错误,但我找不到它。当我启动我的服务器GUI时,其中的组件不会出现。我所有的都是一个空白的白页,大小正确。

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
}

这个课程在这里被调用:

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

3 个答案:

答案 0 :(得分:1)

这是很多代码,使用netbeans的缺点之一:)我唯一能发现的是你应该在致电initComponents();之前致电setVisible(true);

此外,请确保您没有通过阅读正在创建的日志文件(或添加打印行)来捕获任何异常

答案 1 :(得分:1)

您似乎在一个线程上进行初始化,但在另一个线程上调用setVisible方法(在构造函数中包含在invokeLater中)。从事件调度程序线程以外的线程调用UI东西通常是不好的方法,更不用说结果竞争条件了。尝试从构造函数中删除invokeLater,然后包装initalization:

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();
    }
};

答案 2 :(得分:0)

正如我所看到的,这个代码是用netbeans生成的..重新设计页面,因为它是一个拖放和复制功能的问题: - 只在适当的地方的业务功能..小心你放在哪里业务功能因为如果你把它放在错误的地方那么你可能会再次遇到同样的问题。例如..如果你正在为听众编写代码 如下所示

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

是对的。这里要注意的好处是你在方法中包含你的代码,然后从你想要调用它的地方调用该方法。

你可以关心jimmy发布的答案。我觉得通过这段代码找出问题是浪费时间。它的拖放问题。

还要记住,任何组件都不应重叠,并且不应该删除任何已删除但尚未释放其内存的组件。