2015-05-15 38 views
0

我是使用swing和使用Jcombo框的新手,我试图创建一个列车线路,站点的下拉列表,我收到错误,我有一种感觉,我可能是将数据导入数组列表错误,但程序吐出火车线阵列,到控制台,但我已经注释掉了println命令,因为我现在正在使用GUI。Java Combobox与Arraylist错误

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.swing.ComboBoxModel 
at TouchOn.setPanels(TouchOn.java:63) 
at TouchOn.<init>(TouchOn.java:52) 
at GUI$2.actionPerformed(GUI.java:51) 

我从下面的代码中收到上述错误。

import javax.swing.*; 

import java.awt.Dialog.ModalityType; 
import java.awt.event.*; 
import java.awt.*; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 

public class TouchOn extends JDialog { 
private JPanel mainPanel; 


public ArrayList Reader() { 
try { 

    ArrayList<String> Trains = new ArrayList<String>(); 
    int count = 0; 
    String testing = ""; 
    File file = new File("Trainlines.txt"); 
    FileReader fileReader = new FileReader(file); 
    BufferedReader bufferedReader = new BufferedReader(fileReader); 
    StringBuffer stringBuffer = new StringBuffer(); 
    String line; 
    while ((line = bufferedReader.readLine()) != null) 
    { 
     stringBuffer.append(line); 
     count += count; 
     Trains.add(line + "\n"); 
     stringBuffer.append("\n"); 



    } 
    fileReader.close(); 
    //Arrays.asList(Trains).stream().forEach(s -> System.out.println(s)); 
    return Trains; 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
//return toString(); 
return null; 


} 


public TouchOn() 
{ 
    setPanels(); 

    setModalityType(ModalityType.APPLICATION_MODAL); 
    setSize(200, 200); 
    setVisible(true); 
} 
public void setPanels() 
{ 
    mainPanel = new JPanel(new GridLayout(0, 2)); 
    JPanel containerPanel = new JPanel(new GridLayout(0, 1)); 
    ArrayList stations = Reader(); 
    JComboBox<ArrayList> cb = new JComboBox<ArrayList>((ComboBoxModel<ArrayList>) stations); 
    JPanel lowerPanel = new JPanel(new FlowLayout()); 
    JButton apply = new JButton("Touch on ?"); 
    JButton cancel = new JButton("Cancel"); 
    cancel.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      dispose(); 
     } 
    }); 
    apply.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      System.out.println("HellO"); 
     } 
    }); 

    JLabel touchOnDate = new JLabel("Date: "); 
    JLabel touchOnTimehr = new JLabel("Time Hour: "); 
    JLabel touchOnTimem = new JLabel("Time Minute:"); 
    JLabel station = new JLabel("Station: "); 

    JTextField touchOnFieldDate = new JTextField(); 
    JTextField touchOnTimeFieldhour = new JTextField(); 
    JTextField touchOnTimeFieldminute = new JTextField(); 
    //JTextField touchOnStation = new JTextField(); 

    cb.setVisible(true); 

    mainPanel.add(touchOnDate); 
    mainPanel.add(touchOnFieldDate); 
    mainPanel.add(touchOnTimehr); 
    mainPanel.add(touchOnTimeFieldhour); 
    mainPanel.add(touchOnTimem); 
    mainPanel.add(touchOnTimeFieldminute); 
    mainPanel.add(station); 
    mainPanel.add(cb); 
    //mainPanel.add(touchOnStation); 
    lowerPanel.add(apply); 
    lowerPanel.add(cancel); 
    touchOnTimeFieldhour.setSize(10,10); 
    containerPanel.add(mainPanel); 
    containerPanel.add(lowerPanel); 

    add(containerPanel); 
} 

} 

善良我是新的,任何文章或教程阅读将不胜感激,学习Java是相当困难的。

回答

1

不应该(ComboBoxModel<ArrayList>) stationsstations?似乎没有成为一个需要投

提醒你...

ArrayList stations = Reader(); 
JComboBox<ArrayList> cb = new JComboBox<ArrayList>((ComboBoxModel<ArrayList>) stations); 

可能应该更喜欢......

ArrayList<String> stations = Reader(); 
JComboBox<String> cb = new JComboBox<>(); 
for (String value : stations) { 
    cb.addItem(value); 
} 

ArrayList<String> stations = Reader(); 
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()])); 

如果你感觉很懒...

详细了解How to Use Combo Boxes以获取更多详细信息

+0

我当时正在玩转转换,并且它们返回未定义的错误。所以这种方式似乎是唯一的方式,似乎没有产生任何错误 –

+0

哇谢谢你.....如果你不介意可以请你解释for循环,它与我使用的非常不同,我只是困惑关于如何抓取和输入值,组合框。 –

+0

这就是所谓的“增强for循环”,看看[this](https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with)和[this](https://docs.oracle.com /javase/tutorial/java/nutsandbolts/for.html)以获取更多详细信息 – MadProgrammer