简体   繁体   English

JOptionPane MessageDialog在Java中显示数组的数据

[英]JOptionPane MessageDialog to display data of an array in java

I'm working on a program that displays a MessageDialog which shows data of an array I created. 我正在开发一个显示MessageDialog的程序,该MessageDialog显示了我创建的数组的数据。 Each line for example: 每行例如:

11327|933393|2 is inside element 0 of an array. 11327 | 933393 | 2在数组的元素0内部。
11833|938393|1 is inside element 1 of an array. 11833 | 938393 | 1在数组的元素1内部。

For example pretend the numbers below are inside the MessageDialog : 例如,假设下面的数字在MessageDialog内部:

11327|933393|2 11327 | 933393 | 2
11833|938393|1 11833 | 938393 | 1
11934|483393|7 11934 | 483393 | 7

My only problem is that I can only display each element of the array one by one per MessageDialog. 我唯一的问题是每个MessageDialog只能一一显示数组的每个元素。 but I want to display all 3 elements inside one single MessageDialog. 但我想在一个MessageDialog中显示所有3个元素。

Any hints or tips of how I can display my entire array inside one MessageDialog? 关于如何在一个MessageDialog中显示整个数组的任何提示或技巧? :) :)

You can place arbitrary components in your dialog, as shown in this example . 您可以在对话框中放置任意组件,如本示例所示。 A JList or JTable would seem to be a good choice. JListJTable似乎是一个不错的选择。

Addendum: Here's a simple example using JList . 附录:这是一个使用JList的简单示例。

在此处输入图片说明

import java.awt.EventQueue;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/questions/7781781 */
public class OptionList {

    private void display() {
        String[] items = {
            "11327|933393|2", "11833|938393|1", "11934|483393|7"
        };
        JList list = new JList(items);
        JPanel panel = new JPanel();
        panel.add(list);
        JOptionPane.showMessageDialog(null, panel);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new OptionList().display();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM