栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从另一个类使用JFrame

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从另一个类使用JFrame

你应该通过

Client
ServerThread
通过构造。在
Client
你内实例
run()
是不一样的参考
Client
你创建
main()
。所以你的
ServerThread
课就像

ServerThread(Client client, Socket socket,  String userName) {    this.client = client;    this.socket = socket;    this.userName = userName;    messagesToSend = new linkedList<String>();}public void run() {    try    {        Jtextarea test2 = this.client.gettextarea_Receive();        String test3 = "Hello World";        test2.append(test3);    }     catch (IOException e)    {}}

您的

startClient()
方法将更新为这样的内容

private void startClient(Client client, Scanner scan){    try    {        //Create new socket and wait for network communication        Socket socket = new Socket(serverHost, serverPort);        Thread.sleep(1000);        //Create thread and start it        ServerThread serverThread = new ServerThread(client, socket, userName);        serverAccessThread.run();    }}

话虽如此,

我建议将您

main()
移出
Client
与SWING UI代码不太相关的类。像这样:

public class MySwingApplication {    private static final String host = "localhost";    private static final int portNumber = 4444;    public static void main(String[] args) {        // Requests user to enter name        // Start client    }}

Client
然后,您的构建更像实例对象

public class Client extends Jframe {    public Jtextarea gettextarea_Receive(){        // Return the text area    }    // Constructor -- public to allow instantiation from main()    public Client(String userName, String host, int portNumber) {        // Do stuff    }    private void startClient(Scanner scan) {        // Show the Jframe on screen        // Spawn Server    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508068.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号