所以我搜索了互联网,javaDocs和堆栈溢出,但似乎我的问题太模糊了。很明显我错过了一些简单的东西,但我只是没有看到它。
这个程序应该显示一个简单的gui,带有问题的框和下面的相应答案。我还没有完成编码,因为我遇到了这个问题......
我将其中一个QuizCard构造函数设置为清除arrayList,然后将值添加到位置0.这些添加的值是文本区域中显示的默认问题。我制作了一些方法,使我能够将新问题从QuizCardTestDrive类bu传递到程序中,因为某些原因值不会通过。那个和arrayList.size()方法不返回列表的正确值。任何帮助将不胜感激!
public class QuizCard implements Serializable{
private ArrayList<String> mQuestionList = new ArrayList<String>();
private ArrayList<String> mAnswerList = new ArrayList<String>();
public QuizCard(String question, String answer) {
mQuestionList.add(question);
mAnswerList.add(answer);
}
public QuizCard() {
mQuestionList.clear();
mQuestionList.clear();
mQuestionList.add(0, "What class is used for writing to a text file?");
mAnswerList.add(0,"class FileWriter");
}
public void setLists(String q, String a) {
mQuestionList.add(q);
mAnswerList.add(a);
}
public String getQuestion(int q) {
if ((q > mQuestionList.size()) ) {
q = 0;
}
return mQuestionList.get(q);
}
public String getAnswer(int a) {
if ((a > mAnswerList.size()) ) {
a = 0;
}
return mAnswerList.get(a);
}
public int getQuestionListSize() {
return mQuestionList.size();
}
public int getAnswerListSize() {
return mAnswerList.size();
}
}
public class QuizCardTestDrive extends QuizCard{
public static void main(String [] args) {
QuizCard b5 = new QuizCard();
QuizCardBuilder qb = new QuizCardBuilder();
System.out.println("The Answer List Size = " + b5.getAnswerListSize());
System.out.println("The Question List Size = " +
b5.getQuestionListSize());
b5.setLists("is This a test?", "DUHH!!!");
b5.setLists("adfadfad", "DUHH!!!");
b5.setLists("adfadfad", "DUHH!!!");
b5.setLists("adfadfad", "DUHH!!!");
System.out.println("The Answer List Size = " + b5.getAnswerListSize());
System.out.println("The Question List Size = " +
b5.getQuestionListSize());
qb.setupGui();
}
}
public class QuizCardBuilder implements Serializable, ActionListener{
QuizCard qz = new QuizCard();
int list;
JFrame frame;
JPanel background;
JTextArea areaN;
JTextArea areaS;
JButton buttonNext;
JLabel labelN;
JLabel labelS;
JScrollPane scrollN;
JScrollPane scrollS;
public void setupGui() {
frame = new JFrame("Gray Garrett LLC Presents: QUIZ CARD FLASH 1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
background = new JPanel();
background.setBackground(Color.magenta);
background.setLayout(new BoxLayout(background, BoxLayout.Y_AXIS));
areaN = new JTextArea(10,20);
areaS = new JTextArea(10,20);
areaN.setText(qz.getQuestion(0));
areaS.setText(qz.getAnswer(0));
areaN.setLineWrap(true);
areaS.setLineWrap(true);
scrollN = new JScrollPane(areaN);
scrollN.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollN.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollS = new JScrollPane(areaS);
scrollS.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollS.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
labelN = new JLabel("QUESTION");
labelS = new JLabel("ANSWER");
buttonNext = new JButton("NEXT");
buttonNext.addActionListener(this);
background.add(labelN);
background.add(scrollN);
background.add(labelS);
background.add(scrollS);
background.add(buttonNext);
frame.getContentPane().add(BorderLayout.CENTER, background);
frame.setSize(500,500);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
areaN.setText("");
areaS.setText("");
list++;
if (list > qz.getQuestionListSize()) {
list = 0;
}
//areaN.setText("");
areaN.setText(qz.getQuestion(list));
}
class AnswerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
for (int z = 0; z < qz.getQuestionListSize(); z++) {
if (!areaN.equals(qz.getQuestion(z))) {
areaS.setText(" ");
areaS.setText("CUSTOM QUESTION HAS NOT BEEN SAVED!");
}
}
areaS.setText("");
areaS.setText(qz.getAnswer(list));
}
}
}
答案 0 :(得分:0)
在main方法中执行swing应用程序时,您第一次有五个答案和问题。 您还可以在控制台上查看输出的第一个和最后一个
The Answer List Size = 1
The Question List Size = 1
The Answer List Size = 5
The Question List Size = 5
我认为你必须将QuizCard对象引用传递给已有4个项目的QuizCardBuilder类。
所以,我用一个QuizCard对象作为参数更改了setupGui方法。
我认为您不需要清除和识别列表的索引。 也不需要检查索引排列,因为在QuizCardBuilder类中已经有一个列表作为边界检查常量。
您可以使用另一个应该验证与AnswerListener类相对应的用户问题的按钮,该按钮不会在任何地方使用。
您可以看到代码与我的代码之间存在差异。
这不是一个好的代码,但希望这可以帮助你。
以下是名为QuizCardTestDrive类的完整修改代码:
package justtest;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class QuizCardTestDrive /*extends QuizCard*/ {
public static void main(String[] args) {
QuizCardBuilder qb = new QuizCardBuilder();
QuizCard b5 = new QuizCard();
System.out.println("The Answer List Size = " + b5.getAnswerListSize());
System.out.println("The Question List Size = " + b5.getQuestionListSize());
b5.setLists("is This a test?", "DUHH!!!");
b5.setLists("adfadfad", "DUHH!!!");
b5.setLists("adfadfad", "DUHH!!!");
b5.setLists("adfadfad", "DUHH!!!");
System.out.println("The Answer List Size = " + b5.getAnswerListSize());
System.out.println("The Question List Size = " + b5.getQuestionListSize());
qb.setupGui(b5);
}
}
class QuizCard implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<String> mQuestionList = new ArrayList<String>();
private ArrayList<String> mAnswerList = new ArrayList<String>();
//public QuizCard(String question, String answer) {
// mQuestionList.add(question);
// mAnswerList.add(answer);
//}
final static String firstQuestion = "What class is used for writing to a text file?";
final static String firstAnswer = "class FileWriter";
public QuizCard() {
//mQuestionList.clear();
//mQuestionList.clear();
//mQuestionList.add(0, "What class is used for writing to a text file?");
//mAnswerList.add(0,"class FileWriter");
mQuestionList.add(firstQuestion);
mAnswerList.add(firstAnswer);
}
public void clearAnswers()
{
mAnswerList.clear();
}
public void clearQuestions()
{
mQuestionList.clear();
}
public void setLists(String q, String a) {
mQuestionList.add(q);
mAnswerList.add(a);
}
public String getQuestion(int q) {
//if ((q > mQuestionList.size())) {
// q = 0;
//}
//return mQuestionList.get(q);
return mQuestionList.get(q);
}
public String getAnswer(int a)
{
//if ((a > mAnswerList.size())) {
// a = 0;
//}
//return mAnswerList.get(a);
return mAnswerList.get(a);
}
public int getQuestionListSize() {
return mQuestionList.size();
}
public int getAnswerListSize() {
return mAnswerList.size();
}
}
class QuizCardBuilder implements Serializable, ActionListener {
private static final long serialVersionUID = 1L;
//QuizCard b5 = new QuizCard();
QuizCard qz;
int list;
JFrame frame;
JPanel background;
JTextArea areaN;
JTextArea areaS;
JButton buttonNext;
JLabel labelN;
JLabel labelS;
JScrollPane scrollN;
JScrollPane scrollS;
public void setupGui(QuizCard qz) {
this.qz = qz;
frame = new JFrame("Gray Garrett LLC Presents: QUIZ CARD FLASH 1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
background = new JPanel();
background.setBackground(Color.magenta);
background.setLayout(new BoxLayout(background, BoxLayout.Y_AXIS));
areaN = new JTextArea(10, 20);
areaS = new JTextArea(10, 20);
areaN.setText(qz.getQuestion(0));
areaS.setText(qz.getAnswer(0));
areaN.setLineWrap(true);
areaS.setLineWrap(true);
scrollN = new JScrollPane(areaN);
scrollN.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollN.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollS = new JScrollPane(areaS);
scrollS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
labelN = new JLabel("QUESTION");
labelS = new JLabel("ANSWER");
buttonNext = new JButton("NEXT");
buttonNext.addActionListener(this);
background.add(labelN);
background.add(scrollN);
background.add(labelS);
background.add(scrollS);
background.add(buttonNext);
frame.getContentPane().add(BorderLayout.CENTER, background);
frame.setSize(500, 500);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
areaN.setText("");
areaS.setText("");
list++;
if (list >= qz.getQuestionListSize()) {
list = 0;
}
// areaN.setText("");
areaN.setText(qz.getQuestion(list));
areaS.setText(qz.getAnswer(list));
}
class AnswerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
for (int z = 0; z < qz.getQuestionListSize(); z++) {
if (!areaN.equals(qz.getQuestion(z))) {
areaS.setText(" ");
areaS.setText("CUSTOM QUESTION HAS NOT BEEN SAVED!");
}
}
areaS.setText("");
areaS.setText(qz.getAnswer(list));
}
}
}