我不断收到错误:无与伦比的类型:int和string

时间:2016-10-23 18:42:53

标签: java user-interface

我试图让用户从列表中选择,用户选择的任何选项应该显示在文本区域中,但我很难实现这一点!

我也一直收到一条错误,指出无法胜任的类型:intstring

 import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.event.ListSelectionListener;

    public class Lab4Part3 extends JFrame implements ListSelectionListener {

    JList<String> list;

        public Lab4Part3() {

            JPanel panel = new JPanel();

            Container c = getContentPane();

            JPanel panel1 = new JPanel();

            JLabel new1Label = new JLabel ("Choose your fav subject");
    //create a list with 10 choices
            String choices [] = {"GUI", "Maths", "Database", "Object Oriented", "Web Dev", "Networks", "Switching", "Routing", "accounting", "finance",};

             list = new JList<String>(choices);
            list.addListSelectionListener(this);
            JScrollPane pane = new JScrollPane(list);
            panel1.add(new1Label);
            panel1.add(list);

            JPanel panel2 = new JPanel();
            JTextArea ta = new JTextArea();
            ta.setText("Response will appear here");
            panel2.add(ta);

            c.add(panel1, BorderLayout.NORTH);
            c.add(panel2, BorderLayout.SOUTH);

            setSize(400,300);
            setVisible(true);


        }

            public static void main (String args [])  {


            Lab4Part3 myFrame = new Lab4Part3 ();
        myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


        }


    public void valueChanged(ListSelectionEvent e) {


    if (list.getSelectedIndex()==("GUI"))  {


            ta.setText("GUI");
    }

    }
    }

1 个答案:

答案 0 :(得分:0)

 if (list.getSelectedIndex()==("GUI"))  {

应替换为

 if (list.getSelectedIndex()==0)  {

因为"GUI"choices数组中的第一个值。

更简洁的方法是使用Map<Integer,String>将您选择的索引与String值相关联。 这样,如果UI的选择顺序发生变化,则侦听器在处理过程中保持有效。