gpt4 book ai didi

java - 从 ArrayList 到 JComboBox

转载 作者:行者123 更新时间:2023-11-30 09:40:36 24 4
gpt4 key购买 nike

我有一个赋值集,我需要将 Games 中的 JComboBox 填充到 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();
}
}

最佳答案

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();
}
}

关于java - 从 ArrayList 到 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9309449/

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