Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

java - How to add a JScrollPanel to previously added label components?

I'm having a little problem when trying to add JScrollPanel to previously added components with their bounds set specificaly in the for loop. The problem begins when I wanna add more than 15 components in my frame because I've explicitly set frame height and width so they won't be visible. Here is a code snippet:

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        setContentPane(contentPane);
        setResizable(false);
        contentPane.setLayout(null);
        for(int i=0,j=0;i<questions.length;i++) {

            JLabel quest = new JLabel((i+1)+") "+questions[i][0]);
            quest.setBounds(185, 10+j, 200, 200);
            contentPane.add(quest);

            JLabel ans = new JLabel("Ans) "+answers[i][0]);
            ans.setBounds(190, 30+j, 200, 200);
            contentPane.add(ans);
            j=j+40;

        }

        Exit = new JButton("Exit");
        Exit.setBounds(270, 530, 89, 23);
        contentPane.add(Exit);
        Exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {

                System.exit(0);
            }
        });

The code from above consists of for loop where I dynamically add components to their preffered x, y location using setBounds and simple exit button. How can I add JLabel components(quest, ans) from loop to JScrollPanel but to keep their location as specified in for loop ? Thanks guys for all the previous help :)

question from:https://stackoverflow.com/questions/65854689/how-to-add-a-jscrollpanel-to-previously-added-label-components

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...