-2

JListに問題があります。この JList のリスナーがあります (マウスとキーボードの両方)。リストのオプションの 1 つをダブルクリックした後 (または 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;}
}
4

2 に答える 2

3

次のスニペットをいつでも使用できます。

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

注意: に a を追加する代わりに、Swing KeyBinding の使用を検討しKeyListener/KeyAdapterてください。JList

于 2013-06-19T14:08:21.633 に答える