【问题标题】:Assiging IP address to a JButton in Java在 Java 中为 JButton 分配 IP 地址
【发布时间】:2015-04-19 10:04:26
【问题描述】:

我有一个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,以便单击该按钮,一条小消息会发送到该客户端(来自该 IP 地址)。

我怎样才能做到这一点?

【问题讨论】:

  • 澄清一下,您想存储 IP 地址,然后在单击按钮时,您希望按钮向该 IP 发送消息?如果是这样,您能否发布此实现的代码,以便我们了解 Button 的功能并从那时起为您提供帮助
  • 您无法凭空创建 JButton。你需要有一个 UI 来把它放进去。去创建一个,然后尝试创建按钮。如果这不起作用,您可以在此处返回并提出具体问题。
  • 看看我的编辑@TejjD
  • 我的 UI 中有一个按钮,我只想将它分配给连接到我的服务器系统的每个 IP。
  • 请用我们讨论过的所有更改更新问题,以便我看看可能出了什么问题。谢谢

标签: java client server lan


【解决方案1】:

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

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

例如:

更新

我看到的唯一选择是创建一个变量,该变量将跟踪连接的客户端数量并根据此命名按钮。同样仅出于测试目的,将按钮命名为主机名,以便您知道哪个按钮与哪个按钮相关。见下文:

创建一个名为“clientIP”的全局变量,以及一个用于跟踪客户端数量的 Integer,以及一个用于存储所需客户端按钮的 ArrayList。

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

接下来在 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++;
}

就操作的实施而言,您可以执行以下操作:

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
}

为了显示按钮,您可以简单地创建一个方法,该方法将在每次创建按钮时打印出该位置的按钮(从您的 Try 语句开始)

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);
}

希望这能回答你的问题!

让我知道结果。

【讨论】:

  • 我会试试看,一会儿告诉你。
  • 别担心,希望对您有所帮助。
  • 此方法将包含一个按钮。但是例如,如果我有 30 个系统连接到服务器,我必须有 30 个按钮才能与各个客户端进行通信。希望你能明白我想告诉你的。
  • 那么您是否希望在每次新客户端连接时同时创建新按钮。而且这些按钮中的每一个都必须指向为其创建它的客户端的 IP?
  • 完全是你想要的。希望你能指导我。
猜你喜欢
  • 2015-04-19
  • 2013-09-15
  • 1970-01-01
  • 2014-06-06
  • 2013-12-23
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
  • 2016-03-16
相关资源
最近更新 更多