【问题标题】:Struggling to connect main method to java classes努力将主要方法连接到java类
【发布时间】:2019-05-09 23:30:14
【问题描述】:

这是我的头等舱客户

package hwch33;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.*;


public class Client extends Application {
// IO streams 
DataOutputStream toServer = null; 
DataInputStream fromServer = null; 

@Override // Override the start method in the Application class 
public void start(Stage primaryStage) {
 // Panel p to hold the label and text field 
  BorderPane paneForTextField = new BorderPane();
  paneForTextField.setPadding(new Insets(5, 5, 5, 5)); 
  paneForTextField.setStyle("-fx-border-color: green");
  paneForTextField.setLeft(new Label("Enter a radius: "));

  TextField tf = new TextField();
  tf.setAlignment(Pos.BOTTOM_RIGHT);
  paneForTextField.setCenter(tf);

  BorderPane mainPane = new BorderPane();
  // Text area to display contents 
  TextArea ta = new TextArea();
  mainPane.setCenter(new ScrollPane(ta));
  mainPane.setTop(paneForTextField);

  // Create a scene and place it in the stage 
  Scene scene = new Scene(mainPane, 450, 200);
  primaryStage.setTitle("Client"); // Set the stage title 
  primaryStage.setScene(scene); // Place the scene in the stage 
  primaryStage.show(); // Display the stage 

  tf.setOnAction(e -> {
    try {
      // Get the radius from the text field 
      double radius = Double.parseDouble(tf.getText().trim());

      // Send the radius to the server
      toServer.writeDouble(radius);
      toServer.flush();

      // Get area from the server 
      double area = fromServer.readDouble();

      // Display to the text area 
      ta.appendText("Radius is " + radius + "\n");
      ta.appendText("Area received from the server is " 
        + area + '\n');
    }
    catch (IOException ex) {
      System.err.println(ex);
    }
  });

  try {
    // Create a socket to connect to the server 
    Socket socket = new Socket("localhost", 8000); 
    //Socket socket = new Socket("130.254.204.36", 8000); 
    //Socket socket = new Socket("drake.Armstrong.edu", 8000); 

    // Create an input stream to receive data from the server 
    fromServer = new DataInputStream(socket.getInputStream()); 

    // Create an output stream to send data to the server 
    toServer = new DataOutputStream(socket.getOutputStream()); 
  }
  catch (IOException ex) {
    ta.appendText(ex.toString() + '\n');
  }
 }
 }

这是我的二等服务器

 package hwch33;
 import java.io.*;
 import java.net.*;
 import java.util.Date;
 import javafx.application.Application;
 import javafx.application.Platform;
 import javafx.scene.Scene;
 import javafx.scene.control.ScrollPane;
 import javafx.scene.control.TextArea;
 import javafx.stage.Stage;


 public class Server extends Application {

 /**
 *
 * @param primaryStage
 */
    @Override
    public void start(Stage primaryStage) {
 // Text area for displaying contents 
    TextArea ta = new TextArea();

    // Create a scene and place it in the stage 
    Scene scene = new Scene(new ScrollPane(ta), 450, 200);
    primaryStage.setTitle("Server"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage 

    new Thread(() -> {
        try {
        // Create a server socket 
            ServerSocket serverSocket = new ServerSocket(8000); 
            Platform.runLater(() ->
            ta.appendText("Server started at " + new Date() + '\n'));


            Socket socket = serverSocket.accept(); 

            DataInputStream inputFromClient = new DataInputStream(
                socket.getInputStream());
            DataOutputStream outputToClient = new DataOutputStream(
                socket.getOutputStream());

            while (true) {
                double radius = inputFromClient.readDouble(); 
                double area = radius * radius * Math.PI;
                outputToClient.writeDouble(area); 

                Platform.runLater(() -> {
                    ta.appendText("Radius received from client: " 
                      + radius + '\n');
                    ta.appendText("Area is: " + area + '\n'); 
                });
            }
        }
            catch(IOException ex) {
              ex.printStackTrace();
            }
          }).start();
         }
        } 

这是我需要添加代码以使其正常工作的地方

 /*
 * To change this license header, choose License Headers in Project 
 Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hwch33;





/**
*
* @author 
*/
public class HWCH33 {

/**
 * @param args the command line arguments
 */



 public static void main(String[] args) {        

}



}

不知道如何从这里构建一个 main。 我一直在尝试形成一个,但无法做到这一点。我要么得到静态错误,要么根本不起作用。 任何帮助表示赞赏。不包括我以前的任何尝试,因为它们没用,我把它们都去掉了。

【问题讨论】:

  • construct a main 是什么意思?你的意思是主要的虚空吗? public static void main(String[] args){}?
  • 是的,这正是我的意思。这两组代码都是单独的类文件。
  • connect 他们是什么意思?你得到什么错误?
  • 我只是在努力让它们运行。我想运行比连接更好用。他们应该与该地区一起返回。
  • 我试过做 Client.start(primaryStage);

标签: java class methods


【解决方案1】:

如果错误是非静态变量与静态方法或类似内容不兼容,只需从 public static void main(String[] args){} 中省略“静态”一词。

【讨论】:

  • 你需要main方法是public和static才能工作,否则Java虚拟机将无法运行你的程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 2014-03-25
  • 2012-05-11
  • 1970-01-01
  • 2020-06-20
  • 2012-02-08
  • 1970-01-01
相关资源
最近更新 更多