Ich erstelle ein TCP-Server/Client-System. Derzeit habe ich eine ConnectionFrame.java
Datei erstellt, einschließlich einer JFrame
mit einer JButton
, und einer JTextField
. Wenn mein Client starten, möchte ich dies JFrame
erscheinen (also wenn ich meine client.jar starten, sollte die ConnectionFrame
auch ausgeführt werdenStarten einer Klasse aus einer separaten Klasse
Ich nahm an, ich könnte dies tun, indem sie folgende Zeile in die Hauptmethode Kunden.
.ConnectionFrame cf = new ConnectionFrame();
Aber ohne Glück noch einmal zu versuchen schrieb:.
ConnectionFrame.main(argv);
die aus irgendeinem Grund zu verwirren meiner ursprünglichen Code schien bis
EDIT: Code Hinzufügen von kontext
Die ConnectionFrame:
public class ConnectionFrame extends JFrame implements ActionListener {
JButton buttonLayout = new JButton("Connect to server");
JTextField textFieldLayout = new JTextField(1);
JTextArea consoleOutput = new JTextArea(1,20);
public void addComponentToPane(Container pane) {
buttonLayout.addActionListener(this);
textFieldLayout.setHorizontalAlignment(JTextField.CENTER);
consoleOutput.setEditable(false);
pane.add(buttonLayout, BorderLayout.PAGE_START);
pane.add(textFieldLayout, BorderLayout.CENTER);
pane.add(consoleOutput, BorderLayout.PAGE_END);
}
public static void ConnectionFrame() {
ConnectionFrame frame = new ConnectionFrame();
frame.addComponentToPane(frame.getContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if("Connect to server".equals(e.getActionCommand())){
consoleOutput.append("CONSOLE: Triggered");
}
}
public static void main(String[] args) {
ConnectionFrame();
}
}
Der Kunde:
public class TCPClient {
public static void main(String argv[]) throws Exception {
ConnectionFrame cf = new ConnectionFrame();
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 54343);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
CanvasFrame myFrame = new CanvasFrame();
myFrame.setVisible(true);
ArrayList<Point> points = myFrame.location;
outToServer.writeInt(points.size());
for(Point p : points)
{
outToServer.writeInt(p.x);
outToServer.writeInt(p.y);
}
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Können Sie bitte ein [mcve] machen? –