WindowListener问题

时间:2015-12-02 17:52:59

标签: java

我正在尝试向我的程序添加一个WindowListener,但由于我的GUI中没有JFrame,因此我的类扩展了JFrame,因此无法完成。 有人知道如何解决这个问题吗?

这是我的一段代码,因为我没有JFrame

,所以我无法工作
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
   int confirmed = JOptionPane.showConfirmDialog(null, 
      "Are you sure you want to exit the program?", "Exit Program Message     Box",
      JOptionPane.YES_NO_OPTION);

if (confirmed == JOptionPane.YES_OPTION) {
  dispose();
   }
 }
});

我应该在“addWindowListener”之前写什么呢?`

my file structure is define

1 个答案:

答案 0 :(得分:1)

如果您在JFrame扩展类中,只需将WindowListener添加到该类的当前对象F:\IBM BLUEMIX\DW001\testappl1> F:\IBM BLUEMIX\DW001\testappl1>cd dir The system cannot find the path specified. F:\IBM BLUEMIX\DW001\testappl1>dir Volume in drive F is Comp Stuff Volume Serial Number is 5635-6A95 Directory of F:\IBM BLUEMIX\DW001\testappl1 01-12-2015 23:12 <DIR> . 01-12-2015 23:12 <DIR> .. 01-12-2015 17:40 13 .cfignore 01-12-2015 17:40 7,171 .jshintrc 01-12-2015 17:40 429 .project 01-12-2015 23:12 <DIR> .settings 01-12-2015 17:40 9,873 app.js 01-12-2015 17:40 176 manifest.yml 01-12-2015 17:40 429 package.json 01-12-2015 23:12 <DIR> public 01-12-2015 17:40 445 README.md 01-12-2015 23:12 <DIR> routes 01-12-2015 23:12 <DIR> views 7 File(s) 18,536 bytes 6 Dir(s) 1,294,770,176 bytes free F:\IBM BLUEMIX\DW001\testappl1>cf push BI-MyFirstDeploy-3 -c "node app.js" -m 128M --no-manifest Updating app BI-MyFirstDeploy-3 in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... OK Creating route bi-myfirstdeploy-3.mybluemix.net... FAILED Server error, status code: 400, error code: 210003, message: The host is taken: bi-myfirstdeploy-3 F:\IBM BLUEMIX\DW001\testappl1>cf push testappl -c "node app.js" -m 128M --no-manifest Creating app testappl in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... OK Creating route testappl.mybluemix.net... FAILED Server error, status code: 400, error code: 210003, message: The host is taken: testappl F:\IBM BLUEMIX\DW001\testappl1>cf bs testappl testappl1-cloudantNoSQLDB Binding service testappl1-cloudantNoSQLDB to app testappl in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... OK TIP: Use 'cf restage testappl' to ensure your env variable changes take effect F:\IBM BLUEMIX\DW001\testappl1>cf start testappl Starting app testappl in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... FAILED Server error, status code: 400, error code: 150001, message: The app package is invalid: bits have not been uploaded F:\IBM BLUEMIX\DW001\testappl1>cf cs cloudantNoSQLDB Shared testservice1 Creating service instance testservice1 in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... OK Attention: The plan `Shared` of service `cloudantNoSQLDB` is not free. The instance `testservice1` will incur a cost. Contact your administrator if you think this is in error. F:\IBM BLUEMIX\DW001\testappl1>cf bs testappl testservice1 FAILED App testappl not found F:\IBM BLUEMIX\DW001\testappl1>cf push testappl -c "node app.js" -m 128M --no-manifest Creating app testappl in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... OK Creating route testappl.mybluemix.net... FAILED Server error, status code: 400, error code: 210003, message: The host is taken: testappl F:\IBM BLUEMIX\DW001\testappl1>cf bs testappl testservice1 Binding service testservice1 to app testappl in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... OK TIP: Use 'cf restage testappl' to ensure your env variable changes take effect F:\IBM BLUEMIX\DW001\testappl1>cf start testappl Starting app testappl in org pr00330912@techmahindra.com / space Test1 as pr00330912@techmahindra.com... FAILED Server error, status code: 400, error code: 150001, message: The app package is invalid: bits have not been uploaded 即可。看起来你是在匿名的内部类中调用它,如果是,那么使用类名this完全限定你的this

因此,如果您的班级名为MyJFrameClass.this,那么

MyJFrameClass

变为

dispose();