Java:从GUI内删除GUI对象 [英] Java: Deleting a GUI object from within the GUI

查看:214
本文介绍了Java:从GUI内删除GUI对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能在这里帮助我吗?一个非常简单的问题,但是我无法解决该问题!

Can you help me out here? A really simple problem but I just can't get what the solution is!

我正在编写一个在其自己的线程&上运行的侦听器应用程序;在ServerSocket上侦听传入的连接.

I am coding a listener application that runs on its own thread & listens on a ServerSocket for incoming connections.

当连接到达时,在新线程上创建一个新的消息"对象,并传递传入的文本数据"messageData".然后,该对象应在弹出窗口中向用户显示文本.

When a connection arrives, a new 'Message' object is created on a new thread and passed the incoming text data "messageData". This object should then display the text to the user in a popup window.

在Listener.java中:

In Listener.java:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {

        Message.display(messageData);

    }
});

然后,在Message.java中

Then, in Message.java:

public Message {

    public static void display(String data) {

        Message message = new Message(data);

        message.initGUI();

    }

    String messageData;

    GUI gui;

    public Message(String data) {

        messageData = data;

        gui = new GUI();

    }

    public void initGUI() {

        gui.init();

        // add listeners
        gui.addOKListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                // close GUI
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        gui.close();
                    }
                });

                gui = null;

            }
        });

    }

    public class GUI {
        do GUI stuff 
        ... initialise gui
        ... add components
        ... setVisible=true
    }
}

想法是内部类GUI是视图",而Message对象是模型和控制器的组合.

The idea is that the inner class GUI is the 'View' and that the Message object is the model and controller combined.

每个Message对象最初都是通过静态Message.display()方法创建的.这意味着可以有无限多个独立存在的Message对象,并且Listener不必担心对其进行管理.

Each Message object initially creates itself via the static Message.display() method. This means that there can be unlimited Message objects all existing independently and the Listener doesn't have to worry about managing them.

太好了.但是我现在有一个问题.

Great. However I now have a problem.

当用户在GUI上单击确定"时,我希望(a)删除/隐藏GUI,并且(b)从内存中删除Message对象.

When the user clicks 'OK' on the GUI, I want (a) the GUI to be deleted/hidden, AND (b) the Message object to be deleted from memory.

我该怎么做(a)和(b)?我可以(a)通过 gui.addOKListener()中的gui=null(请参见上文),但我不能执行(b),因为我没有对Message对象的引用.

How can I do (a) and (b)? I can do (a) by gui=null in gui.addOKListener() (see above) but I can't do (b) because I don't have a reference to the Message object.

我试图在Message.display()中定义addOKListener(),但这不起作用,因为我必须将我创建的message对象做为final,因此message=null是非法的.

I have tried to define addOKListener() in Message.display() but this doesn't work because I have to make the message object that I create final, therefore message=null is illegal.

一个非常简单的问题-我猜解决方案也很简单.这非常令人沮丧.

A really simple problem- I am guessing the solution is also simple. This is very frustrating..

非常感谢

推荐答案

如果您要做的就是向用户显示一条带有简短测试消息的对话框,那么使用JOptionPane和其中的一个选项应该会容易得多.那里有静态工厂方法.

If all you want to do is to display a dialog with a short test message to the user it should be a lot easier if you used JOptionPane and one of the static factory methods in there.

看看教程

这篇关于Java:从GUI内删除GUI对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆