2012-04-18 3 views
0

여러 JPathane 객체를 표시 할 수있는 GUI JPane을 숙제하는 숙제가 있습니다. 내 청취자 중 한 명에게 컴파일 오류가 발생했으며이를 해결하는 데 도움이 될 수 있습니다. 우리가 IDE를 사용할 수 없다는 말로이 모든 것을 서두시겠습니까?Java에서 컴파일 오류 - (<익명 ListSelectionListener>)

오류 :

F:\Java\Lab 8\Lab8.java:84: error: cannot find symbol 
     jcbo.addListSelectionListener(new ListSelectionListener() { 
      ^
    symbol: method addListSelectionListener(<anonymous ListSelectionListener>) 
    location: variable jcbo of type JComboBox<String> 
1 error 

프로젝트 코드는 다음과 같습니다

import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import javax.swing.event.*; 
    import javax.swing.border.*; 
    import java.util.Scanner; 
    import java.util.EventObject; 


    public class Lab8 extends JFrame { 

     public String name; 
     public String[] ageRanges = {"Select an Age Range","Under 20", "20-29", "30-39", "40-49", "50-59", "60 and Above"}; 
     public String[] destination = {"Mercury", "Venus", "Moon", "Mars", "Jupiter/Europa", "Saturn/Triton", "Pluto + Sharon"}; 
     final JTextField txtName = new JTextField(20); 
     String value; 


     public Lab8() 
     { 





      // Create an array of Strings for age ranges 
      final JComboBox<String> jcbo = new JComboBox<String>(ageRanges); 
      jcbo.setForeground(Color.blue); 
      jcbo.setBackground(Color.white); 


      // Create an array of String destinations 




      // Declare radio buttons 
      JRadioButton jrbMonday, jrbTuesday, jrbWednesday, jrbThursday, jrbFriday; 

      // Create a textfield 
      JTextField jMsg = new JTextField(10); 


      // Create panel to hold label and textbox. 
      JPanel p1 = new JPanel(); 
      p1.setLayout(new BorderLayout(5,0)); 
      p1.add(new JLabel("Name: "), BorderLayout.WEST); 

      p1.add(txtName, BorderLayout.CENTER); 
      jMsg.setHorizontalAlignment(JTextField.LEFT); 


      // Create combobox panel. 
      JPanel p2 = new JPanel(); 
      p2.setLayout(new GridLayout(2,0,5,5)); 
      p2.add(p1, BorderLayout.NORTH); 
      p2.add(new JComboBox<String>(ageRanges), BorderLayout.CENTER); 
      p2.setBorder(new TitledBorder("Passenger Name & Age Range")); 

      final JList<String> jlst = new JList<String>(destination); 

      //Create listbox panel. 
      JPanel p3 = new JPanel(); 
      p3.setLayout(new GridLayout(1, 0)); 
      p3.add(jlst); 
      p3.setBorder(new TitledBorder("Destinations")); 



      jlst.addListSelectionListener(new ListSelectionListener() { 

       public void valueChanged(ListSelectionEvent e){ 

        final int index = jlst.getSelectedIndex(); 
        value = destination[index]; 
       } 
      }); 


      jcbo.addListSelectionListener(new ListSelectionListener() { 

       public void valueChanged(ListSelectionEvent e){ 

        final int index = jcbo.getSelectedIndex(); 
        value = ageRanges[index]; 
       } 
      }); 




      // Create a print button 
      JButton jbtPrint = new JButton("Print"); 


      // Create a new panel to hold radio buttons. 
      JPanel r1 = new JPanel(); 
      r1.setLayout(new GridLayout(3,2)); 
      r1.add(jrbMonday = new JRadioButton("Monday")); 
      r1.add(jrbTuesday = new JRadioButton("Tuesday")); 
      r1.add(jrbWednesday = new JRadioButton("Wednesday")); 
      r1.add(jrbThursday = new JRadioButton("Thursday")); 
      r1.add(jrbFriday = new JRadioButton("Friday")); 
      r1.setBorder(new TitledBorder("Departure Days")); 
      r1.add(jbtPrint); 


      // Create a radio button group to group five buttons 
      ButtonGroup group = new ButtonGroup(); 
      group.add(jrbMonday); 
      group.add(jrbTuesday); 
      group.add(jrbWednesday); 
      group.add(jrbThursday); 
      group.add(jrbFriday); 



      // Create grid to hold contents 
      JPanel pMain = new JPanel(); 
      pMain.setLayout(new BorderLayout(5,0)); 
      add(r1, BorderLayout.CENTER); 
      add(p2, BorderLayout.NORTH); 
      add(p3, BorderLayout. EAST); 



      // Create button listener 
      jbtPrint.addActionListener(new ButtonListener()); 

      jrbMonday.addActionListener(new mListener()); 
      jrbTuesday.addActionListener(new tListener()); 
      jrbWednesday.addActionListener(new wListener()); 
      jrbThursday.addActionListener(new rListener()); 
      jrbFriday.addActionListener(new fListener()); 



} 
      int flag = 0; 


      // Declare radio button variable 
      boolean monday, tuesday, wednesday, thursday, friday; 

      public void monday(){ 
       monday = true; 
      } 
      public void tuesday(){ 
       tuesday = true; 
      } 
      public void wednesday(){ 
       wednesday = true; 
      } 
      public void thursday(){ 
       thursday = true; 
      } 
      public void friday(){ 
       friday = true; 
      } 





      public class mListener implements ActionListener 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       monday(); 
       flag = 1; 
       } 
      } 

      public class tListener implements ActionListener 
         { 
       public void actionPerformed(ActionEvent e) 
       { 
       tuesday(); 
       flag = 2; 
       } 
      } 

      public class wListener implements ActionListener 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       wednesday(); 
       flag = 3; 
       } 
      } 

      public class rListener implements ActionListener 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       thursday(); 
       flag = 4; 
       } 
      } 

      public class fListener implements ActionListener 
      { 
       public void actionPerformed(ActionEvent e) 
       { 
       friday(); 
       flag = 5; 
       } 
      } 

      public void setText(){ 
       name = txtName.getText(); 
     } 


      /** Handle the print button */ 
      class ButtonListener implements ActionListener { 
       ButtonListener(){ 
       } 
       public void actionPerformed(ActionEvent e) { 
       // Get values from fields 
        setText(); 
       System.out.println("Passenger's Name: " + name + "\n"); 
       System.out.println("Age Group: " + ageRanges + "\n"); 
       System.out.println("Destination: " + value + "\n"); 
       System.out.println("Departure Day: " + flag + "\n"); 



       } 
       /*jbtPrint.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) 
        { 

        } 
       });*/ 

} 





public static void main(String[] args) 
     { 
      Lab8 frame = new Lab8(); 
      // frame.pack(); 
      frame.setTitle("Lab 8 Application"); 
      frame.setLocationRelativeTo(null); // Center the frame 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setSize(425, 275); 
      frame.setVisible(true); 


     } 


} 
+2

는'JComboBox'은 단순히 어떤 방법'addListSelectionListener (...)를'이 없습니다. 결국 목록이 아닙니다. 너 뭐하려고? 어쩌면 나는 내 대답 아래에 그것을 짐작했다 : –

답변

4

JComboBox은 단순히 어떤 방법 addListSelectionListener(...)이 없습니다. 결국 목록이 아닙니다.

코드에서 사용자가 JComboBox에서 무언가를 선택할 때 일부 코드가 트리거되기를 원합니다. 그에 대한 ActionListener 사용에

jcbo.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
    int index = jcbo.getSelectedIndex(); 
    value = ageRanges[index]; 
    } 
}); 

완전히 무관을 이전 : [Ljava.lang.String;@1283052를 들어

주의 그 배열에 toString()를 호출 (또는 같은 효과를 기존의 문자열로을 연결), 내용은 아니지만 배열 유형과 일부 내부 해시 코드가 혼합되어 있습니다. 내용이 Arrays.toString() methods 중 하나를 사용하십시오 :

System.out.println("Age Group: " + Arrays.toString(ageRanges) + "\n"); 
+0

그게 거의 작동하지만 결과를 얻을 : 나이 그룹 : [Ljava.lang.String; @ 1283052 –

+0

@ 케빈 슐츠 : 그것은 당신이 배열의 값을 인쇄하기 때문에' ageRanges'를 사용합니다. 코드를 다시 확인하십시오. – Tudor

+0

@Kevin 배열의 내용을 출력하는 방법을 포함하여 답변을 업데이트했습니다. 오, 내가 잘못 생각한 것 같아. 튜더 (Tudor)가 가정 한 것과 같이 하나의 값만 출력하고 싶다면 내'Arrays.toString()'advice는 유용하지 않다. –

3

JComboBox 클래스가 더 addListSelectionListener 방법이 없습니다. javadoc을 참조하십시오.

현재 선택 대신 addActionListener를 사용해보십시오 변화의 이벤트 차단하려면 다음

jcbo.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) { 

     final int index = jcbo.getSelectedIndex(); 
     value = ageRanges[index]; 
    } 
}); 
+0

addItemListener (ItemListener listener)는 어떻습니까? 선택 변경 이벤트를 처리하지 않습니까? –

+0

@ 튜더 오답 – mKorbel