English 中文(简体)
我如何从名单中轮流读取内容?
原标题:How do I read elements from to lists alternately?
  • 时间:2012-05-25 15:40:44
  •  标签:
  • java

嗨,我有2个名单 定义:

ArrayList<JLabel> questionsList = new ArrayList<JLabel>();
ArrayList<JRadioButton> answersList = new ArrayList<JRadioButton>();

我试着读出这样的问答:

1st Question 
  answer1
  answer2

2nd Question 
  answer1
  answer2
etc.

因此,第一个问题是从列表中读取,“强度”问题列表 < /强度 > 然后我想从另一个列表中读取,“强度”答案列表

In questionsList I read data from mysql and they are saved in JLabels in format like:
1. Question one
2. Question two
etc

In answersList I read data from mysql and they are saved in JRadioButtons in format like:
answer1
answer2
etc

到目前为止,我的代码是:

    int height = 0;

        for(int i1 =0; i1<questionsList.size(); i1++)
        {   
              client.insideFillPollPanel.add(questionsList.get(i1)).setBounds(20, 20+150*i1, 480, 14);

            height = 20+150*i1;  

            for(int i2 =0; i2<answersList.size(); i2++)
            client.insideFillPollPanel.add(answersList.get(i2)).setBounds(20, 50+30*i2, 480, 14);



        }

我该如何解答它, 才能像第一个格式一样显示, 我展示了这样的问题, 然后是Asnwers问题和Asnwers?

最佳回答

我建议你建立一个 < code < question 类,该类既包括问题,也包括属于该问题的所有答案,然后,而不是

ArrayList<JLabel> questionsList = new ArrayList<JLabel>();
ArrayList<JRadioButton> answersList = new ArrayList<JRadioButton>();

您使用

ArrayList<Question> questions = new ArrayList<Question>();

(作为侧边注意:用程序与界面相对应被认为是良好做法,因此,不要将问题存储在 ArrayList< Question> 我建议您选择一个 List< Question> 。 )


例如:

class Question {

    JLabel questionLabel;
    List<JRadioButton> answers;

    public Question(String q, String... ans) {
        ...
    }
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签