简体   繁体   中英

Problem in Java swing code

I intend to take the elements of an integer list and add it to a label and print them in downward fashion one after another. I wrote the following code for it.

public static JFrame ListDraw(JFrame frame , ArrayList<Integer> e)
{
    for(int i= 0;i<e.size();i++)
    {
        JLabel j = new JLabel(e.get(i).toString(),JLabel.CENTER); 
        frame.add(j);
    }
    return frame;
}

But it just prints the last array element in the label. What am I doing wrong here?

---------------------(update)

This is just a query that I have regarding the same thing. Therefore I am going to ask it here only. Is there any way to print the label items in a stack as in vertical alignment. Right now I get all the values printed in the horizontal fashion.

I guess you need to set layout for your frame, f.ex: frame.setLayout(new FlowLayout()); .

Your frame isn't adapting to the new group of elements- the LayoutManager isn't getting a chance to resize the window. At the end of your function, add frame.pack() .

您应该使用setBounds()定义框架的大小,并为其选择所需的LayoutManager。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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