在处理中禁用关闭按钮 [英] Disable close button in Processing

查看:119
本文介绍了在处理中禁用关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些事件期间,是否有办法在正在处理"中禁用窗口的关闭按钮?

Is there a way to disable the window's close button in Processing, during a certain event?

以下是代码段:

frame.addWindowListener(new WindowAdapter()
  {
    public void windowClosing(WindowEvent we)
    {
      if (youWin == 0) // condition that is supposed to keep the application opened
      {
        JOptionPane.showMessageDialog(frame,"You can't exit until you finish this game. OK?");
        // keep applet opened
      }
    }
  }
);

我想不使用JFrames来做.

I want to do it without using JFrames.

推荐答案

该答案适用于Processing2.不适用于较新版本的Processing.

This answer is for Processing 2. It won't work with newer versions of Processing.

我想不使用JFrames来做.

I want to do it without using JFrames.

太糟糕了.您已经在使用JFrame,只是不知道.

Too bad. You're already using a JFrame, you just don't know it.

处理将为您创建一个JFrame,即使它存储在Frame变量中.如果您不相信我,请查看

Processing will create a JFrame for you, even though it's stored in a Frame variable. If you don't believe me, check out line 453 of PSurfaceAWT.

这意味着您可以使用JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);告诉框架,当您单击X按钮时,不要执行任何操作.这将禁用自动关闭JFrame的低级侦听器.

That means you can use JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); to tell the frame to, well, do nothing when you click the X button. This disables the low-level listeners that automatically close the JFrame.

import javax.swing.JFrame;
void setup() {
  ((JFrame)frame).setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
void draw() {
  background(0);
  ellipse(mouseX, mouseY, 10, 10);
}

那只是成功的一半.处理还具有自己的侦听器,该侦听器检测用户何时单击低级侦听器顶部的X按钮.该侦听器调用exit()函数,该函数仍然会关闭草图.

That's only half the battle though. Processing also has its own listener that detects when the user clicks the X button, on top of the low-level listener. This listener calls the exit() function, which closes the sketch anyway.

要绕过那个,您必须重写exit()函数. (您也可以删除正在处理中添加的侦听器,但这更容易恕我直言.)

To get around that, you have to override the exit() function. (You could also remove the listener that Processing adds, but this is easier imho.)

import javax.swing.JFrame;
void setup() {
  ((JFrame)frame).setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
void exit() {
  println("not exiting");
}
void draw() {
  background(0);
  ellipse(mouseX, mouseY, 10, 10);
}

好的,我们已经完全取消了使用X按钮关闭框架的功能.现在,我们必须添加想要关闭它的功能.您可以通过在exit()函数中添加对super.exit()的调用来做到这一点:

Okay, we've completely removed the ability to close the frame with the X button. Now we have to add in the ability to close it when we want to. You can do this either by adding a call to super.exit() in the exit() function:

void exit() {
  if(reallyExit){
    super.exit();
  }
}

调用super.exit()将为您关闭草图.设置reallyExit变量由您决定.

Calling super.exit() will close the sketch for you. It's up to you how you set the reallyExit variable.

另一种方法是将WindowListner添加到处理关闭事件的JFrame:

Another approach would be to add a WindowListner to the JFrame that handles the closing event:

void setup() {

  ((JFrame)frame).setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

  frame.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent we) {
      if (reallyExit) {
        frame.dispose();
      }
    }
  }
  );
}

这是使用两种方法的完整示例.不过,您只需要 调用frame.dispose() 调用super.exit().这完全取决于个人喜好.

Here's a full example that uses both approaches. You only need either a call to frame.dispose() or a call to super.exit() though. It's really up to personal preference.

import javax.swing.JFrame;

void setup() {

  ((JFrame)frame).setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

  frame.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent we) {
      if (mouseX < 10) {
        frame.dispose();
      }
    }
  }
  );
}

void exit() {
  if(mouseX <10){
    super.exit();
  }
}


void draw() {
  background(0);
  ellipse(mouseX, mouseY, 10, 10);
}

这篇关于在处理中禁用关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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