GUIPanel が既に作成されており、それをプログラムに追加する必要があります。メインクラス内から他のプログラムを実行しようとすると、GUIPanel は実行されませんが、実行しないと実行されます (意味がある場合)。
これは、プログラムを実行するとどうなるかです
出力をこの GUIPanel に出力したい (これを実行するには、TourServer への呼び出しを削除する必要がありました) と考えている場合、「コマンド プロンプトにそのままにしておけば、GUIPanel には何もありません。とにかくテキスト領域以外は?」、それは後でさらに追加する予定だからです:D
要約: GUIPanel クラスから TourServer プログラムを実行する必要がありますが、実行しようとしても GUI パネル自体が開きません。
メインクラスは次のとおりです。
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import javax.swing.*;
import java.io.*;
/**
*
* @author Dan
*/
public class GUIPanel extends JFrame {
private PrintStream outStream;
/**
* Creates new form GUIPanel
*/
public GUIPanel() {
initComponents();
}
private void init() throws IOException {
jTabbedPane1.add("Main", jPanel);
jPanel.add(textArea1);
setOutputStream();
TourServer.init(cmd);//Calling the program, I'm assuming I might be doing this wrong.
}
/**
* 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")
private void initComponents() {
textArea1 = new java.awt.TextArea();
jPanel = new javax.swing.JPanel();
jTabbedPane1 = new javax.swing.JTabbedPane();
textArea1.setPreferredSize(new java.awt.Dimension(432, 300));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Game Manager");
setBounds(new java.awt.Rectangle(0, 0, 400, 450));
setPreferredSize(new java.awt.Dimension(450, 420));
getContentPane().setLayout(new java.awt.FlowLayout());
try {
init();
} catch (IOException ioe) {
ioe.printStackTrace();
}
jTabbedPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jTabbedPane1.setFocusable(false);
jTabbedPane1.setOpaque(true);
getContentPane().add(jTabbedPane1);
jTabbedPane1.getAccessibleContext().setAccessibleName("");
pack();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
cmd = args;
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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUIPanel().setVisible(true);
}
});
}
private static String[] cmd;
private javax.swing.JPanel jPanel;
private javax.swing.JTabbedPane jTabbedPane1;
private java.awt.TextArea textArea1;
//Written by MadProgrammer
public class CapturePane extends JPanel implements Consumer {
private TextArea output;
public CapturePane() {
jPanel.setLayout(new BorderLayout());
jPanel.add(new JScrollPane(output));
}
@Override
public void appendText(final String text) {
if (EventQueue.isDispatchThread()) {
output.append(text);
output.setCaretPosition(output.getText().length());
} else {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
appendText(text);
}
});
}
}
}
public interface Consumer {
public void appendText(String text);
}
public class StreamCapturer extends OutputStream {
private StringBuilder buffer;
public StreamCapturer() {
buffer = new StringBuilder(128);
buffer.append("");
}
@Override
public void write(int b) throws IOException {
char c = (char) b;
String value = Character.toString(c);
buffer.append(value);
if (value.equals("\n")) {
textArea1.appendText(buffer.toString());
buffer.delete(0, buffer.length());
}
}
}
}
そして、これがTourServerクラスです
// TourServer.java
/*
The top-level Tour server, which waits for client connections
and creates TourServerHandler threads to handle them.
Details about each client are maintained in a TourGroup object
which is referenced by each thread.
Very similar to the multithreaded Chat server.
*/
import java.net.*;
import java.io.*;
public class TourServer
{
static int PORT = 0;
private TourGroup tg;
static GameSession gameSession=new GameSession();
public TourServer()
// wait for a client connection, spawn a thread, repeat
{
tg = new TourGroup();
try {
ServerSocket serverSock = new ServerSocket(PORT);
Socket clientSock;
while (true) {
new GUIPanel("Waiting for a client...");
clientSock = serverSock.accept();
new TourServerHandler(clientSock, tg).start();
}
}
catch(Exception e)
{ e.printStackTrace(); }
} // end of TourServer()
// -----------------------------------
public static void init(String args[]) {
if (Integer.parseInt(args[0]) > 0) {
PORT = Integer.parseInt(args[0]);
} else {
PORT = 5550;
}
new GUIPanel("Port set to: "+PORT);
new TourServer();
}
} // end of TourServer class