我正在教自己Java,我正在使用Deitel的书,因为它强烈推荐,而且我遇到了一些麻烦。
所以我尝试复制Java:How to Program一书中的图27.5-8。我想我需要.5数字,因为它是服务器和.7数字,因为它是客户端。所以我在同一个项目中创建它们然后组合它们的主要类(图.6和.8),这样当我运行程序时它会启动服务器和客户端。但是,当我告诉netBeans编译并运行时,它会打开我为服务器和客户端设置的窗口,但textFields不会启用(因为它们应该在接收到连接时)。就我而言告诉他们没有相互联系。
server.java和client.java文件应该与书中的文件完全相同,所以我认为当我混合主文件以启动它们时我一定搞砸了。这是我的组合主文件。也许我在这里做错了什么?
package server_client;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
Server application = new Server(); //create server
Client applicationClient; //declare client application
//if no command line args
if (args.length==0)
applicationClient = new Client ("127.0.0.1"); //connect to localhost
else
applicationClient = new Client (args[0]); //use args to connect
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applicationClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.runServer(); //run server application
applicationClient.runClient(); //run client application
}//end main
}//end class Main
答案 0 :(得分:0)
你正在混淆。让我们从头开始。首先,这是您创建简单UI的方式。
public static void main(String[] args){
JFrame frame = new JFrame(); // This will be holding your future buttons
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Request Generator");
frame.setSize(300, 300); // Size x = 300, size y = 300
frame.setLocationRelativeTo(null); // Puts the frame in the middle of the screen
frame.setVisible(true); // Without this line of code, the frame won't show
}
但在这种情况下,我不建议为测试目的创建UI,因为它非常耗时且不必要。改为使用控制台。您可以在控制台中输出任何内容,如下所示:
System.output.println("Hello, world!");
甚至是变量,比如数字。
int number = 10;
System.output.println("Variable number has value: " + number);
其次,我建议您将客户端和服务器拆分为两个单独的项目,然后单独启动它们。或者甚至更好,如果您愿意,我可以给您一个小型的客户端/服务器连接示例。因为,就我个人而言,我之前从未遇到过这种实现。
答案 1 :(得分:0)
解决起来可能很棘手。
有一个名为netstat
的Windows实用程序,它将显示所有网络连接。
同时学习如何使用debugger也会有所帮助。
一种可能性是在GUI检查之前建立连接,因此GUI不知道连接在那里。
尝试在一个应用程序中启动服务器,在另一个应用程序中启动客户端。