Java 带有 Arraylist 错误的组合框
Java Combobox with Arraylist error
我对使用 swing 和 Jcombo 框还很陌生,我试图创建一个包含火车线路、车站的下拉列表,但我收到错误消息,我感觉我可能正在导入数据进入阵列列表错误,但程序将火车线阵列吐出到控制台,但我已经注释掉了 println 命令,因为我现在正在使用 GUI。
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.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 相当困难。
(ComboBoxModel<ArrayList>) stations
不应该是 stations
吗?好像没有演员的必要
请注意...
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 了解更多详情
我对使用 swing 和 Jcombo 框还很陌生,我试图创建一个包含火车线路、车站的下拉列表,但我收到错误消息,我感觉我可能正在导入数据进入阵列列表错误,但程序将火车线阵列吐出到控制台,但我已经注释掉了 println 命令,因为我现在正在使用 GUI。
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.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 相当困难。
(ComboBoxModel<ArrayList>) stations
不应该是 stations
吗?好像没有演员的必要
请注意...
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 了解更多详情