2010-06-22 47 views
0

我有幾個JSpinners,它們是從10開始的數值,可以遞增到99.如何知道JSpinner上的遞增/遞減是否被按下?

在我的程序中,用戶有15點可以平均分散6個技能。每個JSpinner都有一個EventListener來檢測它是否被按下,但更具體地說,我需要知道哪個按鈕被按下,所以我知道要採取哪種操作。他們不希望從強度中減去一點,並將總分減1,而如果按下減數,則應該加1。

執行此操作的最佳方法是什麼?

(另外我使用NetBeans因此該方案的一個位被自動生成的。)

回答

1

想必你是某處ChangeListener的內線stateChanged方法 - 看看ChangeEvent#getSource()


好,你的編輯讓我原來的回答毫無意義。

會創建自己的SpinnerModel是一個可行的選擇嗎?

+0

我已經有了一個SpinnerModel用於定義默認的每個JSpinner t值,增量等。 – Kitsune 2010-06-22 05:49:52

+0

你看過重寫'getNextValue'和'getPreviousValue'方法嗎? – 2010-06-22 05:56:31

+0

不,這不會讓我去我需要的地方。 – Kitsune 2010-06-22 06:10:21

-1

我遇到同樣的問題,這是我如何解決了我的行動方案的執行情況:

首先我收集所有箭頭按鈕

private static HashMap<String, BasicArrowButton> getSpinnerButtons(JSpinner spinner, String[] arrowNames) { 
    final Stack<String> arrows = new Stack<String>(); 
    arrows.addAll(Arrays.asList(arrowNames)); 
    final HashMap<String, BasicArrowButton> buttons = new HashMap<String, BasicArrowButton>(); 
    while (buttons.size()<2) { 
     for (final Component c : spinner.getComponents()) { 
      if (c instanceof BasicArrowButton) { 
       final BasicArrowButton bab = (BasicArrowButton)c; 
       for (final String sName : arrows) { 
        if (sName.equals(bab.getName())&&!buttons.containsKey(sName)) { 
         buttons.put(sName,bab); 
         break; 
        } 
       } 
      } 
     } 
    } 
    return buttons; 
} 

然後附上一些聽衆

final String KEY_PROP = ".DIRECTION"; 
    final String BS = spinner.getName(), BN="Spinner.nextButton", BP="Spinner.previousButton"; 
    final String BSKEY = BS+KEY_PROP, BNKEY = BN+KEY_PROP, BPKEY = BP+KEY_PROP; 

    final HashMap<String, BasicArrowButton> buttons = getSpinnerButtons(spinner, new String[]{BN,BP}); 

    spinner.putClientProperty(BSKEY, 1000); 
    spinner.putClientProperty(BNKEY, buttons.get(BN).getDirection()*+10000); 
    spinner.putClientProperty(BPKEY, buttons.get(BP).getDirection()*-10000); 

    final PropertyChangeListener pcl = new PropertyChangeListener() { 
     @Override 
     public void propertyChange(PropertyChangeEvent evt) { 
      final JSpinner spinnerRef = ((JSpinner)evt.getSource()); 
      final String pName = evt.getPropertyName(); 
      final short swing = Short.parseShort(String.valueOf(evt.getOldValue())); 
      final short val = Short.parseShort(String.valueOf(evt.getNewValue())); 
      if (Math.abs(swing)<2D) 
       System.out.printf("This is a DIRECTION CHANGE\nOld Direction=%s;\nNew Direction=%s;\nProp Value: %s", swing, val, spinnerRef.getClientProperty(pName)).println(); 
      else //arrows 
       System.out.printf("This is the CURRENT DIRECTION\nArrow=%s;\nDirection=%s;\nProp Value: %s", swing, val, spinnerRef.getClientProperty(pName)).println(); 

      System.out.println("=============="); 
     } 
    }; 

    spinner.addPropertyChangeListener(BSKEY, pcl); 
    spinner.addPropertyChangeListener(BNKEY, pcl); 
    spinner.addPropertyChangeListener(BPKEY, pcl); 

    final ActionListener spinnerActions = new ActionListener() { 
     private short oldDir=0; 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      final BasicArrowButton bab = ((BasicArrowButton)e.getSource()); 
      final short swingDir = (short)bab.getDirection(); 
      final short newDir = (swingDir!=SwingConstants.NORTH&&swingDir!=SwingConstants.WEST) ? Integer.valueOf(-1).shortValue() : Integer.valueOf(+1).shortValue(); 
      bab.getParent().firePropertyChange(bab.getName()+KEY_PROP, swingDir*1000, newDir); 
      bab.getParent().firePropertyChange(bab.getParent().getName()+KEY_PROP, oldDir, newDir); 
      this.oldDir=newDir; 
     } 
    }; 
    buttons.get(BP).addActionListener(spinnerActions); 
    buttons.get(BN).addActionListener(spinnerActions);