如何关闭JList的jFrame?

时间:2013-06-19 13:18:15

标签: java swing jlist listselectionlistener

我有一个JList问题。我有这个JList(鼠标和键盘)的监听器。 我想,双击列表中的一个选项(或按Enter键),JFrame关闭。我找不到任何地方。你可以帮帮我吗?

这是我使用的类(取自StackOverflow):

import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;

public class ActionJList extends JList {

  ActionListener al;
  boolean close=false;

  public ActionJList(String[] it){
    super(it);

    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
        if (al == null) return;
        Object ob[] = getSelectedValues();
        if (ob.length > 1) return;
        if (me.getClickCount() == 2) {
          System.out.println("Sending ACTION_PERFORMED to ActionListener");
          al.actionPerformed(new ActionEvent(this,
          ActionEvent.ACTION_PERFORMED,
          ob[0].toString()));
          me.consume();
          close=true;

        }
      }
    });

    addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent ke) {
        if (al == null) return;
        Object ob[] = getSelectedValues();
        if (ob.length > 1) return;
        if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
          System.out.println("Sending ACTION_PERFORMED to ActionListener");
          al.actionPerformed(new ActionEvent(this,
          ActionEvent.ACTION_PERFORMED,
          ob[0].toString()));
          ke.consume();
        } 
      }
    });
    this.setSelectedIndex(0); 
  }

  public ActionJList(Vector it){
    super(it);

    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
        if (al == null) return;
        Object ob[] = getSelectedValues();
        if (ob.length > 1) return;
        if (me.getClickCount() == 2) {
          System.out.println("Sending ACTION_PERFORMED to ActionListener");
          al.actionPerformed(new ActionEvent(this,
          ActionEvent.ACTION_PERFORMED,
          ob[0].toString()));
          me.consume();
        }
      }
    });

    addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent ke) {
        if (al == null) return;
        Object ob[] = getSelectedValues();
        if (ob.length > 1) return;
        if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
          System.out.println("Sending ACTION_PERFORMED to ActionListener");
          al.actionPerformed(new ActionEvent(this,
          ActionEvent.ACTION_PERFORMED,
          ob[0].toString()));
          ke.consume();
        } 
      }
    });
    this.setSelectedIndex(0); 
  }



  public void addActionListener(ActionListener al){
    this.al = al;
  }
  public boolean getClose(){return close;}
}

2 个答案:

答案 0 :(得分:3)

您始终可以使用以下代码段:

Window window = SwingUtilities.getWindowAncestor(ActionJList.this);
if (window!=null)
    window.setVisible(false);

注意:请考虑使用Swing KeyBindings,而不是向KeyListener/KeyAdapter添加JList

答案 1 :(得分:1)

值得查看List Action的可重用类,为您添加MouseListener和Key Bindings。此外,事件的来源是JList,因此您可以使用Guillaume的建议轻松创建Action。

无需引用可用的JFrame。 SwingUtilities方法是更好的方法。