在 Java 中为 JButton 分配 IP 地址

Assiging IP address to a JButton in Java

我有一个 Server-Client 程序,当客户端使用以下代码行连接到我的服务器系统时,我会获取客户端的 IP 地址:

//连接到客户端

void connect_clients()
{
    try {
        ServerSocket listener = new ServerSocket(7700);
        jButton2.setText("Server Running!");
        jButton2.setEnabled(false);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    System.out.println("Client conneted from :" + socket.getLocalAddress().getHostName();
                }
                finally {
                    socket.close();
                }
            }
        }
        finally {
            listener.close();
        }
    }
    catch (IOException ex) {
        Logger.getLogger(Test_Frame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

现在我需要将它分配给 JButton,以便单击 Button 时,一条小消息会发送到该客户端(来自该 IP 地址)。

我怎样才能做到这一点?

根据您向我们展示的内容,我看不到相关按钮的实现或代码(即,将向 IP 发送消息的按钮。)

但是我可以告诉你的是你如何才能做到这一点。只需在打印之前分配 IP 地址即可。然后在 Buttons Action 方法中,您可以使用变量。

例如:

UPDATED

The only option I see to this is to create a variable that will track how many clients are connected and name the buttons according to this. Also just for testing purposes, name the button the Host name so you know which button is relevant to which button. See below:

Create a Global Variable called "clientIP", as well as an Integer to tract the number of clients, as well as an ArrayList to store the client buttons required.

//note that .getHostName returns a String represenation of the hostname from that IP
String clientIP = "";
int clientNumber = 0;
ArrayList<JButton> buttons = new ArrayList<JButton>();

Next assign the hostname when it is fetched within the try/catch:

try{
    clientIP = socket.getLocalAddress().getHostName();

    //This will add a new Button to the ArrayList with the Text of the Button being the Clients Host Name
    buttons.add(new JButton(clientIP));

    System.out.println("Client conneted from :" + clientIP);
    displayButton(buttons.get(clientNumber);
    clientNumber++;
}

In terms of implementation of Actions you can then do the following:

public void actionPerformed( ActionEvent e ){
    if ( e.getSource() == buttons.get(0) )
        //this line to assign the hostname to a variable so that we can use it to send the message
        String thisIP = buttons.get(0).getText();
        //now use this to send your message
    if ( e.getSource() == buttons.get(1) )
        String thisIP = buttons.get(1).getText();
        //now use this to send your message
    if ( e.getSource() == buttons.get(2) )
        String thisIP = buttons.get(2).getText();
        //now use this to send your message
    if ( e.getSource() == buttons.get(3) )
        String thisIP = buttons.get(3).getText();
        //now use this to send your message
}

In order to display the buttons, you can simply have a method created that will print out the Button at that position everytime it is created (From with your Try statement)

public static void displayButton(JButton newButton){
    // this line is to display the button on your JFrame. Edit the "frame" according to what the name of your JFrame is.
    frame.add(newButton);
}

希望这能回答您的问题!

让我知道结果。