2013-04-29 3 views
1

actionListeners이 엉망이됐다고 생각합니다. 두 개의 버튼 블록이 있어야합니다.여러 ActionListener가 문제를 만듭니다

첫 번째 패널은 16 버튼 x 16 버튼 (총 256 개의 버튼)입니다. 두 번째 패널은 8 개의 버튼 x 2 개의 버튼 (총 16 개의 버튼) "ColorChooser"의 "blue"를 두 패널 모두에 전달할 수 있습니다.

그러나 두 번째 패널이 빨간색으로 바뀌어야합니다. 파랑이 아님. 내 두 번째 문제 - 패널 2가 올바르게 보입니다. 1에서 시작하여 16으로갑니다. 그러나 패널 1은 17에서 시작하여 256으로 이동합니다. 어떤 도움을 주셔서 감사합니다.

버튼 배열에 대한 내 도구 팁이 작동하지 않는 이유가 궁금합니다.

감사합니다.

example of what I mean

  final JFrame MaineFrame = new JFrame("Kola Color Crapper"); 
      MaineFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      MaineFrame.setPreferredSize(new Dimension(400, 400)); 

      final JFrame PatternGeneratorFrame = new JFrame("Pattern Generator"); 
      PatternGeneratorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      ActionListener PatternMakerActionListener = new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 

        PatternGeneratorFrame.setVisible(true); 

       } 
      }; 
      JButton PatternMakerBtn = new JButton("Make Pattern"); 
      PatternMakerBtn.addActionListener(PatternMakerActionListener); 
      PatternMakerBtn.setToolTipText("This will allow you to create your own patterns 1 frame at a time."); 



      JPanel contentPane = new JPanel(); 



      ActionListener myActionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent aef) { 
        if (aef.getSource() instanceof JButton) { 
         ((JButton) aef.getSource()).setBackground(ColorChooser.PassColor); 

        } 
       } 
      }; 



      ActionListener ColorChooserActionListener = new ActionListener() { 
       public void actionPerformed(ActionEvent aee) { 
        if (aee.getSource() instanceof JButton) { 
         ((JButton) aee.getSource()).setBackground(Color.red); 

        } 
       } 
      }; 
     JButton button[] = new JButton[256]; 
      JPanel GridPanel = new JPanel(); 
      GridPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2)); 
      GridPanel.setLayout(new GridLayout(16, 16, 10, 10)); 
      for (int i = 0; i < button.length; i++) { 
       button[i] = new JButton(Integer.toString(i + 1)); 
       button[i].addActionListener(myActionListener); 
       GridPanel.add(button[i]); 
       GridPanel.setToolTipText("lets make noise."); 
      } 
      contentPane.add(GridPanel); 


      JButton ColorPickerBtn[] = new JButton[16]; 
      JPanel ActionPanel = new JPanel(); 
      ActionPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 6)); 
      ActionPanel.setLayout(new GridLayout(2, 8, 0, 10)); 
      for (int l = 0; l < 16; l++) { 
       ColorPickerBtn[l] = new JButton(Integer.toString(l+ 1)); 
       ColorPickerBtn[l].addActionListener(ColorChooserActionListener); 
       ColorPickerBtn[l].setToolTipText("Choose the color you would like to use. * Colors 1-8 are static. You may change colors 9-16."); 
       ActionPanel.add(button[l]); 
      } 
      contentPane.add(ActionPanel); 

      MaineFrame.getContentPane().add(PatternMakerBtn); 

      PatternGeneratorFrame.setContentPane(contentPane); 
      PatternGeneratorFrame.pack(); 
      PatternGeneratorFrame.setVisible(false); 
      MaineFrame.pack(); 
      MaineFrame.setVisible(true); 
      ; 
     } 
    }); 

} 
} 
} 

답변

0

이 문제에 대한 나의 접근 방식은 단지 전체 프레임을 듣는 것 하나 ActionListener를 사용하는 것입니다.

+0

발견했습니다! 그냥 일어나서 다시 보았습니다. ActionPanel.add (button [l]); <--- 변경됨 ---> ActionPanel.add (ColorPickerBtn [l]); –

+0

모든 것이 작동하지만 도구 팁이 !! –

관련 문제