我想製作一個程序,但我有一個問題。我試圖做出下一個按鈕,但它顯示最後一個「soal」和最後一個「jawaban」。我想看到每個SOAL當我按下一個button.I上傳這張截圖和我的代碼嘗試在java中建立一個按鈕netbeans
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package guiandtests1;
import static guiandtests1.NewJFrame.answer1;
import static guiandtests1.NewJFrame.answer2;
import static guiandtests1.NewJFrame.answer3;
import static guiandtests1.NewJFrame.answer4;
import static guiandtests1.NewJFrame.question;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
*
* @author abc
*/
public class Guiandtests1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws ParseException, IOException {
NewJFrame form = new NewJFrame();
form.setVisible(true);
JSONParser parser = new JSONParser();
Object obj = null;
obj = parser.parse(new FileReader("c:\\Matematika.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("data"); // it should be any array name
Iterator<Object> iterator = array.iterator();
while (iterator.hasNext()) {
JSONObject element=(JSONObject)iterator.next();
System.out.println(element.get("soal"));
question=(String) element.get("soal");
JSONArray abc = (JSONArray) element.get("jawaban");
Iterator<Object> iterator1 = abc.iterator();
ArrayList<String> results = new ArrayList<>();
while (iterator1.hasNext()) {
results.add(iterator1.next().toString());
//System.out.println(iterator1.next());
}
System.out.println(results.get(0));
System.out.println(results.get(1));
answer1=results.get(0);
answer2=results.get(1);
answer3=results.get(2);
answer4=results.get(3);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package guiandtests1;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
*
* @author abc
*/
public class NewJFrame extends javax.swing.JFrame {
public static String question;
public static String answer1;
public static String answer2;
public static String answer3;
public static String answer4;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Microsoft YaHei", 0, 48)); // NOI18N
jLabel1.setText("jLabel1");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("jButton4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("Next");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(157, 157, 157)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton4)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(121, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(115, 115, 115))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton5)
.addGap(18, 18, 18))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jLabel1)
.addGap(49, 49, 49)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
.addComponent(jButton5)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//this.jButton1.setText(answer1);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText(question);
jButton1.setText(answer1);
jButton2.setText(answer2);
jButton3.setText(answer3);
jButton4.setText(answer4);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws IOException, ParseException {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
JSONParser parser = new JSONParser();
Object obj = null;
obj = parser.parse(new FileReader("c:\\Matematika.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("data"); // it should be any array name
Iterator<Object> iterator = array.iterator();
while (iterator.hasNext()) {
JSONObject element=(JSONObject)iterator.next();
System.out.println(element.get("soal"));
JSONArray abc = (JSONArray) element.get("jawaban");
Iterator<Object> iterator1 = abc.iterator();
ArrayList<String> results = new ArrayList<>();
while (iterator1.hasNext()) {
results.add(iterator1.next().toString());
//System.out.println(iterator1.next());
}
System.out.println(results.get(0));
answer1=results.get(0);
answer2=results.get(0);
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
JSON文件位置:
{
"data":[
{
"id":"1",
"soal":"3 * 3 + 2 =",
"jawaban":[
"11",
"15",
"17",
"19"
]
},
{
"id":"2",
"soal":"20/5 =",
"jawaban":[
"4",
"3",
"2",
"1"
]
},
{
"id":"3",
"soal":"16 + 6 = ",
"jawaban":[
"22",
"21",
"20",
"24"
]
},
{
"id":"4",
"soal":"2 * 4 =",
"jawaban":[
"8",
"6",
"10",
"12"
]
},
{
"id":"5",
"soal":"1+4*2",
"jawaban":[
"8",
"10",
"12",
"14"
]
},
{
"id":"6",
"soal":"Jika A+1=10 , B+A=11 C= ?",
"jawaban":[
"EGP",
"1",
"12",
"22"
]
},
{
"id":"7",
"soal":"44/11",
"jawaban":[
"4",
"2",
"5",
"8"
]
},
{
"id":"8",
"soal":"1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1 = ",
"jawaban":[
"16",
"15",
"17",
"10"
]
},
{
"id":"9",
"soal":"Berapakah jawaban sebelumnya ",
"jawaban":[
"16",
"17",
"18",
"19"
]
}
]
}
click to see the image after next button click