1.JFrame 调用 2. JFrame - wait() => 都被阻止?

发布于 2024-10-18 21:20:02 字数 653 浏览 7 评论 0原文

我有一个问题:我有一个 jframe1,它在 ActionPerformed 处调用 jframe2。 JFrame 是线程还是?所以我在jframe2中尝试了wait()方法,然后我会通知jframe1中的jframe2..

我在jframe2中的代码(单击按钮时运行的方法):

private void read(){

    synchronized(jframe1){
        try {

            if(writer.checkLast() == null){
                this.wait();
                jLabel.setText(writer.getLast());
            }
            else{
                jLabel.setText(writer.getLast());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}

但问题是,如果我使用 this.wait();在jframe2中,我的jframe1也被锁定..我做错了什么?

抱歉我的英语不好,谢谢如果有人得到答案!

I've got a problem: I've got a jframe1 who calls at ActionPerformed the jframe2.
JFrames are threads or? and so I've tried in the jframe2 the wait() method, and then I would notify the jframe2's in jframe1..

my code in jframe2 (a method what run, when a button is clicked):

private void read(){

    synchronized(jframe1){
        try {

            if(writer.checkLast() == null){
                this.wait();
                jLabel.setText(writer.getLast());
            }
            else{
                jLabel.setText(writer.getLast());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}

But the Problem is, that if I use this.wait(); in jframe2, my jframe1 is also locked.. what I do wrong?

sry for my bad english, thanks if anyone have got an answer!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

(り薆情海 2024-10-25 21:20:02

框架是线程还是?

不,绝对不是。有一个单个线程,所有绘制和用户输入事件都在其中发生,即事件调度线程。但是,该线程与应用程序的主线程不同,这可能会让您相信每个框架都有自己的线程。

由于所有事件都发生在事件分派线程上,因此您不必执行任何同步,并且您的框架可以调用彼此的方法,而无需任何同步或通知。这就是单线程设计的首要原因(普遍的共识是多线程 GUI 几乎不可能使用)。

Frames are threads or?

No, absolutely not. There is one single thread in which all painting and user input events happen, the Event Dispatch Thread. However, this thread is different from the application's main thread, which is probably what lead you to believe that each frame has its own thread.

Since all events happen on the event dispatch thread, you don't have to do any synchronization, and your frames can call each other's methods without requiring any synchronization or notification. Which is the reason for the single threaded design in the first place (The general consensus is that a multithreaded GUI is nearly impossible to work with).

故事还在继续 2024-10-25 21:20:02

我感觉您正在尝试使用 wait() 方法来模拟模式对话框的行为,但正如 Michael 上面所解释的那样,不要在 Swing 组件上调用 wait 也不要使用 Thread.sleep。相反,如果您想以模态方式显示另一个窗口,请使用 JOptionPane 或模态 JDialog。教程中对此都有很好的解释。

I get the feeling that you're trying to emulate the behavior of a modal dialog by using the wait() method, but as Michael explains well above, don't call wait on a Swing component and don't use Thread.sleep. Instead if you want to display another window modally use a JOptionPane or a modal JDialog. It's all well explained in the tutorials.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文