从JOptionPane获取CheckBox输入

时间:2015-03-14 18:37:47

标签: java swing joptionpane jcheckbox

我正在制作一个摇摆应用程序,我无法从我在JOptionPane中嵌入的复选框中获取输入

此刻我点击一个加载JOptionPane的按钮(比较),然后选择要运行的两个动画。 我在想这个原因不会起作用的原因是因为我得到了按钮的来源而不是嵌入式JCheckBox无论如何都是这样做的吗?

comparison.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

               JPanel a1=new JPanel();
               a1.add(bubbleCheckBox);
               a1.add(quickCheckBox);
               a1.add(insertionCheckBox);
               a1.add(selectionCheckBox);
               a1.add(mergeCheckBox);
              JOptionPane.showConfirmDialog(null, a1);

                  Object buttonPressed =e.getSource();

        if (buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(bubbleCheckBox)) {
            SortAnimator animator = new SortAnimator(new InsertionSorter(),new BubbleSorter());

        }else if(buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(quickCheckBox)){
         //    SortAnimator animator = new SortAnimator(new InsertionSorter(),new quickSorter());

        }else if(buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(selectionCheckBox)){
             SortAnimator animator = new SortAnimator(new InsertionSorter(),new SelectionSorter());

        }else if(buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(mergeCheckBox)){
                 // SortAnimator animator = new SortAnimator(new InsertionSorter(),new mergeSorter());

        }else if(buttonPressed.equals(quickCheckBox)&&buttonPressed.equals(bubbleCheckBox)){
            //

        }else if(buttonPressed.equals(quickCheckBox)&&buttonPressed.equals(mergeCheckBox)){

        }else if(buttonPressed.equals(quickCheckBox)&&buttonPressed.equals(selectionCheckBox)){

        }else if(buttonPressed.equals(selectionCheckBox)&&buttonPressed.equals(bubbleCheckBox)){
              SortAnimator animator = new SortAnimator(new SelectionSorter(),new BubbleSorter());

        }else if(buttonPressed.equals(selectionCheckBox)&&buttonPressed.equals(mergeCheckBox)){

        }else if (buttonPressed.equals(mergeCheckBox)&&buttonPressed.equals(bubbleCheckBox)){

        }else{
            //Invalid selection please select a maximum of two different algorithms
        }

2 个答案:

答案 0 :(得分:0)

此外,您无法像使用getSource()一样同时检测两个不同的事件来源

if (buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(bubbleCheckBox))
getSource()返回最初发生事件的对象。

要解决您的问题,您可以使用布尔值,如isBubbleSelected,isMergeSelected,isInsertionSelected等来检查复选框的状态,即如果根据这些布尔值选择它们,您可以决定要播放哪些动画 例如: isBubbleSelected = bubleCheckBox.isSelected();

if (isBubbleSelected&&isInsertionSelected) {
        SortAnimator animator = new SortAnimator(new InsertionSorter(),new BubbleSorter());

    }else if(isInsertionSelected&&isQuickSelected){
     //    SortAnimator animator = new SortAnimator(new InsertionSorter(),new quickSorter());

    }else if(isInsertionSelected&&isSelectionSelected){
         SortAnimator animator = new SortAnimator(new InsertionSorter(),new SelectionSorter());

    }else if(isInsertionSelected&&isMergeSelected){
             // SortAnimator animator = new SortAnimator(new InsertionSorter(),new mergeSorter());

    }else if(isQuickSelected&&isBubbleSelected){
        //

    }else if(isQuickSelected&&isMergeSelected){

    }else if(isQuickSelected&&isSelectionSelected){

    }else if(isSelectionSelected&&isBubbleSelected){
          SortAnimator animator = new SortAnimator(new SelectionSorter(),new BubbleSorter());

答案 1 :(得分:0)

在ActionListener()类中,您可以通过构造函数传递JCheckBox,然后在操作发生时获取选择。

...前

public class JCheckBoxActionListener() implements ActionListener {
   JCheckBox jCheckBox;

   public JCheckBoxActionListener(JCheckBox jCheckBox) {
      this.jCheckBox = jCheckBox;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println(jCheckBox.isSelected());
   }
}

添加听众...假设'比较'是JCheckBox

comparison.addActionListener(new JCheckBoxActionListener(comparison));