0

我有一个家庭作业,我正在构建一个 GUI JPane,它包含其他 JPanes 以允许显示多个对象。我的一位听众出现编译错误,可以使用一些帮助来解决这个问题。让我先说我们不允许使用 IDE。

错误是:

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);


        }


}
4

3 回答 3

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()方法之一:

System.out.println("Age Group: " + Arrays.toString(ageRanges) + "\n");
于 2012-04-18T20:59:30.977 回答
3

该类JComboBox没有addListSelectionListener方法。请参阅javadoc

如果您想拦截更改当前选择的事件,请尝试使用addActionListener

jcbo.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

         final int index = jcbo.getSelectedIndex();
         value = ageRanges[index];
    }
});
于 2012-04-18T20:59:47.100 回答
0

唔。如果你不允许使用像eclipse这样的IDE,我只会给你一个方向标记......

检查该方法的 jcombobox 的 javadoc。

你可以在这里找到它:http: //docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html

于 2012-04-18T21:01:12.627 回答