Lỗi khi sử dụng JSON

ThanhPC

New Member
22/10/21
1
0
1
Hà Nội
Em có làm thử 1 bt trên lớp nhưng bị lỗi.
code như sau ạ:
package BTLT;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.json.simple.JSONObject;

public class Demo1 extends JFrame {

JLabel lblBook_id = new JLabel("Book_id: ");
JLabel lblTitle = new JLabel("Title: ");
JLabel lblPrice = new JLabel("Price: ");

JTextField txtBook_id = new JTextField();
JTextField txtTitle = new JTextField();
JTextField txtPrice = new JTextField();

JButton btnJSON = new JButton("JSON");
JButton btnAdd = new JButton("Add");

JTextArea txtKetqua = new JTextArea();

public Demo1() {
setTitle("Mô tả đối tướng bằng JSON");
setSize(500,300);
setLayout(null);
setLocationRelativeTo(null);

add(lblBook_id);
add(lblTitle);
add(lblPrice);
lblBook_id.setBounds(10, 20, 100, 25);
lblTitle.setBounds(10, 50, 100, 25);
lblPrice.setBounds(10, 80, 100, 25);

add(txtBook_id);
add(txtTitle);
add(txtPrice);
txtBook_id.setBounds(115, 20, 200, 25);
txtTitle.setBounds(115, 50, 200, 25);
txtPrice.setBounds(115, 80, 200, 25);

add(btnJSON);
btnJSON.setBounds(10, 110, 100, 25);
add(btnAdd);
btnAdd.setBounds(115, 110, 100, 25);

add(txtKetqua);
txtKetqua.setBounds(10, 140, 450, 75);

btnJSON.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JSONObject obj = new JSONObject();
String vId = txtBook_id.getText().trim();
obj.put("book_Id", vId);
String vtitle = txtTitle.getText().trim();
obj.put("title", vtitle);
String vprice = txtPrice.getText().trim();
obj.put("price", vprice);
txtKetqua.setText(obj.toJSONString());
}
});


}

public static void main(String[] args) {
// TODO Auto-generated method stub
Demo1 wMain = new Demo1();
wMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
wMain.setVisible(true);
}

}
Khi em bấm vào nutton json thì nó hiện như này:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
at BTLT.Demo1$1.actionPerformed(Demo1.java:60)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6382)
at java.desktop/java.awt.Container.processEvent(Container.java:2264)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 36 more
Mọi người cho em xin cách giải với, em làm đúng với gợi ý nhưng vẫn bị lỗi ạ