我有一个赋值集,我需要将一个JComboBox从Games输入到ArrayList中填充,现在我已经完成了所有设置,除了整个添加到arraylist之外,我只想让输入正常工作。它加载,但它打印数组地址,而不是字符串,有没有办法解决这个问题?我的代码如下。请注意,这不是全部,这是导致我麻烦的文件,我的其他文件用于显示文件。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class GameCombo extends JPanel {
ArrayList<Game> gamesList = new ArrayList<Game>();
//String[] games = {"", "[Halo: Reach] Xbox 360; Action; $108.00; 2;", "[Dead Space] Xbox 360; Horror; $65.00; 1;"};
public GameCombo() {
setLayout(new GridLayout(1,1,1,1));
gamesList.add(new Game());
gamesList.add(new Game("[Halo: Reach]", 3, 1, 108.00, 2));
gamesList.add(new Game("[Dead Space]", 3, 2, 80.00, 1));
JComboBox _gameBox = new JComboBox(getGames());
add(_gameBox);
}
public Object[] getGames() {
return gamesList.toArray();
}
}
答案 0 :(得分:2)
AKJ是对的。
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ComboTest extends JFrame {
private ArrayList<Game> gamesList = new ArrayList<Game>();
private class Game {
private String title = "";
public Game() {}
public Game(String title, int x, int y, double z, int a) {
this.title = title;
}
/********************************************************/
/********************************************************/
@Override
public String toString() {
return this.title;
}
/********************************************************/
/********************************************************/
}
public ComboTest() {
gamesList.add(new Game());
gamesList.add(new Game("[Halo: Reach]", 3, 1, 108.00d, 2));
gamesList.add(new Game("[Dead Space]", 3, 2, 80.00d, 1));
add(getCombo());
setSize(320, 80);
setVisible(true);
}
private JComboBox getCombo() {
JComboBox box = new JComboBox(getGames());
return box;
}
private Object[] getGames() {
return gamesList.toArray();
}
public static void main(String[] args) {
new ComboTest();
}
}
答案 1 :(得分:0)
您需要将toString()方法添加到Game类。或者在getGames中创建一个新数组并用游戏名称填充它。