gpt4 book ai didi

java - Swing:GUI 不显示我的组件

转载 作者:行者123 更新时间:2023-12-02 07:53:07 30 4
gpt4 key购买 nike

我确信这只是一个小错误,但我找不到它。当我启动服务器 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();

最佳答案

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

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

关于java - Swing:GUI 不显示我的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9994178/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com