2013-12-08 6 views
0

2 개의 목록과 1 개의 단추가 있습니다. 사용자가 하나의 jlist에서 일부 항목을 선택한 다음 단추를 클릭하고 선택에 따라 다른 jlist에 일부 요소가 나타나는 경우를 원합니다. 내 문제는 & & 조건입니다. 프로그램이 처음으로 || 그것을 무시합니다. 따라서 사용자가 "a"와 "c"를 선택하면 모든 요소를 ​​추가하는 세 번째 요소는 일치하지 않습니다. 어떻게 해결할 수 있을까요? 당신은 else ifif로 루프를 확인해야ListSelectionListener가 조건의 합집합을 인식하지 못함

public class test extends JPanel implements ActionListener { 

static JFrame frame = new JFrame(""); 

public test() { 

    String[] feedStrings = { "a", "b", "c", "d" } 

    final JList feedList = new JList(feedStrings); 

              feedList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 

    final DefaultListModel model = new DefaultListModel(); 

      final JList prodList = new JList(model); 


      prodList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 

    JButton arrow = new JButton("button"); 

    arrow.addActionListener(new ActionListener() { 

       public void actionPerformed(ActionEvent e) 
       { 
        feedList.addListSelectionListener(new ListSelectionListener() { 

         @Override 
         public void valueChanged(ListSelectionEvent e) 
         {   
         List<String> templist = new ArrayList<String>(); 

         templist = feedList.getSelectedValuesList(); 

         if (templist.contains("a")|| templist.contains("b"){ 

             model.removeAllElements(); 
           model.addElement("x"); 
           model.addElement("y"); 
           model.addElement("z"); 


       }else if(templist.contains("c") ||templist.contains("d") ) { 
              model.removeAllElements(); 
           model.addElement("t"); 
           model.addElement("g"); 
           model.addElement("h"); 
           model.addElement("k");     

       }else if(templist.contains("a") && templist.contains("c") { 
              model.removeAllElements(); 
              model.addElement("t"); 
           model.addElement("g"); 
           model.addElement("h"); 
           model.addElement("k"); 
              model.addElement("x"); 
           model.addElement("y"); 
           model.addElement("z"); 
         } 
        }); 

       } 
      }); 


     JPanel flow = new JPanel(new FlowLayout()); 
      flow.add(feedList); 
      flow.add(arrow); 
      flow.add(prodList); 

        frame.add(flow); 


} 

private static void createAndShowGUI() { 


      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
      frame.setSize(500, 500); 
      JFrame.setDefaultLookAndFeelDecorated(true); 
      //Add content to the window. 
      frame.add(new test()); 
      frame.setResizable(false); 

      frame.pack(); 
      frame.setVisible(true); 
     } 

public static void main(String[] args) { 
      //Schedule a job for the event dispatch thread: 
      //creating and showing this application's GUI. 
      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
      //Turn off metal's use of bold fonts 
       UIManager.put("swing.boldMetal", Boolean.TRUE); 


      try { 
       createAndShowGUI(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       } 
      }); 

    } 
     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

} 

답변

1

:

여기 내 코드입니다. 코드에 ifelse if을 함께 사용하면 첫 번째 조건이 true로 설정되어 코드가 실행되고 다른 else if 루프는 확인하지 않습니다. 귀하의 경우 'a'와 'c'를 선택하면 첫 번째 if 조건 만 실행되고 나머지 else if 루프는 무시됩니다.

+0

맞습니다. 내 머리가 두렵다. Hehe 감사합니다 – user2598911

+0

당신은 환영합니다 선생님 :) – Keerthivasan

+0

내 대답을 받아 들인 것으로 표시하십시오. 나는 몇 가지 포인트를 얻을 것이다 .. Hehe .. – Keerthivasan