【问题标题】:Applet error: The method add(String, Component) in the type Container is not applicable小程序报错:Container类型中的方法add(String, Component)不适用
【发布时间】:2014-11-21 15:58:25
【问题描述】:

我正在尝试学习我在互联网上看到的一个教程的源代码(看起来有些过时)。在 Applet 类中,我从 add(string, Component) 部分收到错误。

Container类型中的add(String, Component)方法不适用于参数(String, ChatClient)

为什么我不能在下面的代码中使用 add?

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class ClientApplet extends Applet
{

    public void init() {

        String host = getParameter( "host" );
        int port = Integer.parseInt( getParameter( "port" ) );
        ChatClient client = new ChatClient(host,port);
        setLayout( new BorderLayout() );
        add(client, "Center" );
    }
}

【问题讨论】:

    标签: java eclipse applet


    【解决方案1】:

    你必须改变

    添加(客户端,“中心”);

    添加(客户端,BorderLayout.CENTER)

    【讨论】:

    • 嗯,添加下仍有红线。 Container类型中的add(String, Component)方法不适用于参数(ChatClient, String)
    • "not applicable for the arguments (ChatClient, String)" 代码试图将一个小程序实例添加到同一个小程序的另一个实例中。那是行不通的。顺便说一句 - 该代码不会编译。为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。
    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多