javascript 在面板中显示 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6241970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:01:31  来源:igfitidea点击:

Displaying an ArrayList in a Panel

javajavascriptswing

提问by Splitter

I am having this problem in below code as my ArrayList both name and values I wish to be displayed on the window is not appearing. It should be displayed at the bottom of the window but I set everything possible to setVisible true but still unable to display it. I think its a minor mistake but I cant see as it is my code. Sorry that the code is such a mess as I am writing this for my own understanding.

我在下面的代码中遇到了这个问题,因为我希望在窗口上显示的 ArrayList 名称和值都没有出现。它应该显示在窗口的底部,但我将所有可能的设置为 setVisible true 但仍然无法显示它。我认为这是一个小错误,但我看不到,因为这是我的代码。抱歉,代码是如此混乱,因为我正在编写此代码是为了我自己的理解。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;


public class SamplePaper5a {

    static final int width = 500;
    static final int hight = 600;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new JFrame("Exam");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(width,hight);
        Container contain = new Container();
        contain = frame.getContentPane();

        contain.setLayout(new GridLayout(2,2));

        final ArrayList<String>names = new ArrayList<String>();
        final ArrayList<String>values = new ArrayList<String>();

        names.add("dian");
        names.add("maze");
        names.add("carl");
        names.add("John");
        names.add("tan");
        names.add("james");

        values.add("11111");
        values.add("2222");
        values.add("3333");
        values.add("4444");
        values.add("5555");
        values.add("6666");

        final JTextArea txtArea = new JTextArea();
        JButton butt = new JButton(" Enter ");
        contain.add(txtArea);
        contain.add(butt);

        butt.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub                                  
                for(int i=0;i<values.size();i++)
                {
                    txtArea.setText( txtArea.getText()+" \n "+values.get(i) +"  "+ names.get(i));                       
                }
            }               
        });

        try
        {
            contain.add(new NamePanel2(names,values,1));

        }

        catch(Size2Exception e)
        {
            System.out.print(e);
        }

        frame.setVisible(true);
    }   
}

class NamePanel2 extends JPanel 
{
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> values = new ArrayList<String>();
    int cols;

    public NamePanel2(ArrayList<String> nam,ArrayList<String> val,int c) throws Size2Exception
    { 
        this.names = nam;
        this.values =val;
        this.cols = c;
        JPanel panel = new JPanel();

        if(names.size()!= values.size())
            throw new Size2Exception(" wrong sizes");

        panel.setLayout(new GridLayout(names.size(),cols));

        for(int i = 0; i<names.size();i++)
        {
            panel.add(new JLabel(names.get(i)));
            panel.add(new JTextField(values.get(i)));
        }
        panel.setVisible(true); 
    }       
}

class Size2Exception extends Exception
{
    public Size2Exception(String str)
    {
        super(str);
    }
}

回答by Andrew Thompson

See the comments for the nature of the fix + a few tips.

请参阅有关修复性质的评论 + 一些提示。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;


public class SamplePaper5a {

    static final int width = 500;
    static final int hight = 600;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new JFrame("Exam");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(width,hight);
        // don't mix Swing with AWT
        JPanel contain = new JPanel();
        System.out.println(contain.getLayout());
        frame.setContentPane(contain);

        contain.setLayout(new GridLayout(2,2));

        final ArrayList<String>names = new ArrayList<String>();
        final ArrayList<String>values = new ArrayList<String>();

        names.add("dian");
        names.add("maze");
        names.add("carl");
        names.add("John");
        names.add("tan");
        names.add("james");

        values.add("11111");
        values.add("2222");
        values.add("3333");
        values.add("4444");
        values.add("5555");
        values.add("6666");

        final JTextArea txtArea = new JTextArea();
        JButton butt = new JButton(" Enter ");
        contain.add(txtArea);
        contain.add(butt);

        butt.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                for(int i=0;i<values.size();i++)
                {
                    txtArea.setText( txtArea.getText()+" \n "+values.get(i) +"  "+ names.get(i));
                }
            }
        });

        try
        {
            contain.add(new NamePanel2(names,values,1));

        }

        catch(Size2Exception e)
        {
            System.out.print(e);
        }

        frame.setVisible(true);
    }
}

class NamePanel2 extends JPanel
{
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> values = new ArrayList<String>();
    int cols;

    public NamePanel2(ArrayList<String> nam,ArrayList<String> val,int c) throws Size2Exception
    {
        this.names = nam;
        this.values =val;
        this.cols = c;
        // unnecessary
        //JPanel panel = new JPanel();

        // it would be better to define an Object that contains a name/value pair
        if(names.size()!= values.size())
            throw new Size2Exception(" wrong sizes");

        setLayout(new GridLayout(names.size(),cols));

        for(int i = 0; i<names.size();i++)
        {
            add(new JLabel(names.get(i)));
            add(new JTextField(values.get(i)));
        }
        // unnecessary
        //panel.setVisible(true);
    }
}

class Size2Exception extends Exception
{
    public Size2Exception(String str)
    {
        super(str);
    }
}

Screen Shot

截屏

enter image description here

在此处输入图片说明

BTW - the GUI looks quite odd. I'll leave that for you to sort out.

顺便说一句 - GUI 看起来很奇怪。我会把它留给你解决。