【问题标题】:How to add array to a JComboBox如何将数组添加到 JComboBox
【发布时间】:2015-11-26 10:26:35
【问题描述】:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

import javax.swing.ComboBoxModel;

public class test {
    public static String [][] myA;
    public static String i1;
    public static String i2;
    public static void main(String[] args) throws Exception {

        String thisLine;
        String[] temp;
        String delimiter="\t";
        String [][] myA = new String[15][3];
            BufferedReader gg= new BufferedReader(new FileReader(filepath));
            for (int j=0; j<15; j++) {
                    thisLine=gg.readLine();
                    temp = thisLine.split(delimiter);
            for (int i = 0; i <3; i++) {
                myA[j][i]=temp[i];
            }}
            String i1 = myA[0][1];
            String i2 = myA[1][1];

            /*System.out.println(i1);
            System.out.println(i2);*/
          }

        }

我想通过数组将menu.txt添加到JComboBox;但是当我 在另一个框架类中使用此代码,它在组合框中不显示任何内容。 有人可以帮忙吗?

    String[] items = new String[] {test.i1,test.i2};
    JComboBox comboBox = new JComboBox(items);
    comboBox.setFont(new Font("Segoe Print", Font.BOLD, 20));
    comboBox.setBounds(147, 70, 419, 29);
    getContentPane().add(comboBox);

这些是 Menu.txt 的文本:

Drink   Coffee  35
Drink   Latte   37
Drink   Mocha   37
Drink   Cappuccino  34
Drink   Hot Chocolate   34
Drink   Espresso    37
Drink   Hojicha Tea Latte   34
Drink   Java Chip   40
Drink   Ristretto Bianco    37
Salad   Salmon Salad    48
Salad   Chicken Garden Salad    42
Cake    Hazelnut&Chestnut Cheesecake    34
Cake    Chestnut Cheesecake 34
Cake    Chocolate Marquise  33
Cake    Lemon Tart  21

【问题讨论】:

    标签: java arrays swing combobox jframe


    【解决方案1】:

    试试这个: - 初始化后用数组设置JComboBox的项目

    comboBox.setModel(new DefaultComboBoxModel(items));
    

    【讨论】:

    • 谢谢,但组合框中仍然没有显示任何内容
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多