2

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

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

  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
4

1 回答 1

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);
  }
});
于 2012-11-30T20:04:18.087 回答