0

こんにちは私は2つのリストを定義しています:

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

私は次のような形式で質問と回答を読み込もうとしています。

1st Question 
  answer1
  answer2

2nd Question 
  answer1
  answer2
etc.

したがって、最初の質問はリストquestionsListから読み取られ、次に別のリストから読みたいanswersListその質問に対するすべての回答など。

質問リストで、mysqlからデータを読み取り、次のような形式でJLabelに保存されます
。1。質問12.
質問2
など

AnswersListで、mysqlからデータを読み取り、それらはJRadioButtonsに次のような形式で保存されます:
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);



        }

私が示した最初の形式のように表示されるように解決するにはどうすればよいですか?

4

1 に答える 1

4

Question質問とその質問に属するすべての回答の両方を含むクラスを作成してから、

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

あなたが使う

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

(補足として:インターフェースに対してプログラミングすることは良い習慣と考えられているので、質問をArrayList<Question>Iに保存する代わりに、に行くことをお勧めしますList<Question>。)


例えば:

class Question {

    JLabel questionLabel;
    List<JRadioButton> answers;

    public Question(String q, String... ans) {
        ...
    }
}
于 2012-05-25T15:43:21.627 に答える