gpt4 book ai didi

java - 我不断收到错误 : incomparable types: int and string

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:35 24 4
gpt4 key购买 nike

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

我还不断收到错误消息,指出类型不兼容: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");
}

}
}

最佳答案

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

应替换为

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

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

更简洁的方法是使用 Map<Integer,String>您可以将选择的索引与 String 关联起来。值。
这样,如果 UI 中的选择顺序发生变化,监听器在处理过程中仍然有效。

关于java - 我不断收到错误 : incomparable types: int and string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com