2015-05-15 40 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