当用户单击窗口的关闭按钮时隐藏框架而不是关闭

时间:2012-11-30 17:29:20

标签: java swing scala jframe

我有一个显示窗口的应用程序。当用户按下“关闭”按钮时,我只想隐藏此窗口,而不是关闭窗口,我该怎么做?

这是我的代码(它不起作用,应用程序在点击时关闭):

  object UxMonitor {

  def main(args: Array[String]) {
    val frame = new MainScreen()
    frame.peer.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE)
    initTrayIcon(frame.peer)
  }

...

class MainScreen extends MainFrame {
      private val HEADERS = Array[Object]("Security", "Last Price", "Amount", "Cost")
      private val quotes = new Quotes()
      private val tableModel = new DefaultTableModel()

      title = "UX Monitor"

      tableModel.setColumnIdentifiers(HEADERS)

      private val table = new Table {
        model = tableModel
      }

      contents = new ScrollPane {
        contents = table
      }

      quotes.setListener(onQuotesUpdated)
      quotes.startUpdating

      peer.addWindowListener(new WindowAdapter{
        def windowClosing(e : WindowEvent){
            self.setVisible(false)
        }
      })

      pack

1 个答案:

答案 0 :(得分:3)

最简单的方法是:

final JFrame frame=...;

....

frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

或者,您可以通过添加WindowAdapter覆盖windowClosed(...)并在setVisible(false)上调用JFrame)来实现相同的结果,如下所示:

...

 frame.addWindowListener(new WindowAdapter() {
    @Override
  public void windowClosing(WindowEvent evt) {
    frame.setVisible(false);
  }
});