尝试在java netbeans中构建一个按钮
我正在尝试制作一个程序,但我有一个问题。我正在尝试制作“下一步”按钮,但它会显示最后一个“soal”和最后一个“jawaban”。我想在按下“下一步”按钮时看到每个soal。我上传了这个屏幕截图和我的代码尝试在java netbeans中构建一个按钮,java,json,user-interface,netbeans,Java,Json,User Interface,Netbeans,我正在尝试制作一个程序,但我有一个问题。我正在尝试制作“下一步”按钮,但它会显示最后一个“soal”和最后一个“jawaban”。我想在按下“下一步”按钮时看到每个soal。我上传了这个屏幕截图和我的代码 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates *
/*
* 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
}
您的代码组织得非常糟糕。一个明显的问题是,您试图在输入文件的迭代中创建UI。虽然这种方法可以奏效,但通常这是一个非常糟糕的主意,这就是为什么你只看到一个问题的原因。更好的方法是先将文件中的数据读入内存中的数据结构,然后再对其进行处理 另一个问题是,您需要了解,并非所有事情都应该(甚至可以)使用可视化所见即所得设计器来完成。一大堆分开的
jButton3ActionPerformed
绝对是地狱之路
为什么有两个几乎相同的main
方法
还有一点,我肯定你的org.simple.json
,我使用了aka的一个,但我想提醒你,这远远不是最流行的json Java库
话虽如此,这是对代码的一些改进,这些改进工作得更好:
import org.json.simple.JSONArray;
导入org.json.simple.JSONObject;
导入org.json.simple.parser.JSONParser;
导入org.json.simple.parser.ParseException;
导入javax.swing.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.*;
公共类NewJFrame扩展了javax.swing.JFrame{
公共静态类问题{
公共最终int id;
公开的最后一串问题;
公开最终字符串[]答案;
public int selectedAnswerIndex=-1;
公共问题(int-id、字符串问题、字符串[]答案){
this.id=id;
这个问题=问题;
这个。答案=答案;
}
@凌驾
公共字符串toString(){
返回“问题{”+
“id=”+id+
“,问题=”“+问题+”\”+
“,answers=“+Arrays.toString(answers)+
'}';
}
}
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
应答按钮。添加(jButton1);
应答按钮。添加(jButton2);
回答按钮。添加(jButton3);
应答按钮。添加(jButton4);
对于(int i=0;i{
"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"
]
}
]
}