JComboBox 在 运行 之后给出一个数组 3 次 [jdbc]
JComboBox gives an array after running for 3 times [jdbc]
我正在制作图书馆管理系统,
在这里,当你添加书籍时,你 select 来自 jcombobox 的类别,现在 jcombobox 从另一个 table 获取类别(项目),问题是它在它之后完美运行两到三次,它给出一个错误并且 ADD BOOK 框架没有加载,编码和错误消息和屏幕截图如下:)
类别Table
图书Table
图书类别是 FK 类别 table
现在这是从类别 table 中获取数据到 jcombobox
的代码
JComboBox category = new JComboBox();
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
Statement state = connect.createStatement();
ResultSet rs = state.executeQuery("select book_category from library_category");
while (rs.next()) {
String item = rs.getString("book_category");
category.addItem(item);
}
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
category.setBounds(300, 240, 150, 30);
category.setVisible(true);
panel.add(category);
这是另一半代码
(代码太长所以我只是发布关于这个问题的所有内容)
JButton btn_add = new JButton("ADD");
btn_add.setBounds(300, 350, 80, 30);
panel.add(btn_add);
btn_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String[] pattern = new String[4];
pattern[0] = "^[A-Za-z0-9]{3,10}$";
pattern[1] = "^[A-Za-z0-9 ]{3,40}$";
pattern[2] = "^[\p{L} .'-]{4,30}$";
pattern[3] = "^[1-9]\d*$";
//--------------------------------------
String[] field = new String[5];
field[0] = bkid.getText();
field[1] = txtid.getText();
field[2] = re.getText();
field[3] = (String) category.getSelectedItem();
field[4] = txtqun.getText();
if (field[0].isEmpty()) {
obj.info("Please enter book code!", "Error");
return;
}
if (field[1].isEmpty()) {
obj.info("Please enter book name!", "Error");
return;
}
if (field[2].isEmpty()) {
obj.info("Please enter author's name!", "Error");
return;
}
if (field[4].isEmpty()) {
obj.info("Please enter book's quantity!", "Error");
return;
}
Pattern pat1 = Pattern.compile(pattern[0]),
pat2 = Pattern.compile(pattern[1]),
pat3 = Pattern.compile(pattern[2]),
pat4 = Pattern.compile(pattern[3]);
Matcher mat1 = pat1.matcher(field[0]),
mat2 = pat2.matcher(field[1]),
mat3 = pat3.matcher(field[2]),
mat4 = pat4.matcher(field[4]);
if (mat1.matches()) {
if (mat2.matches()) {
if (mat3.matches()) {
if (mat4.matches()) {
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
PreparedStatement pst = connect.prepareStatement("insert into library_book values (?,?,?,?,?);");
pst.setString(1, field[0]);
pst.setString(2, field[1]);
pst.setString(3, field[2]);
pst.setString(4, field[3]);
pst.setString(5, field[4]);
pst.execute();
frame.dispose();
obj.info("You've successfully added " + field[1] + " book to Library System Management", "Congratulations!");
connect.close();
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
} else {
obj.info("Please use only numbers in book quantity!", "Error");
}
} else {
obj.info("Please use only alphabets in Author Name!", "Error");
}
} else {
obj.info("Please use only alphabets in Book Name!", "Error");
}
} else {
obj.info("Please use only numbers and alphabets in Book Code!", "Error");
}
}
});
JButton exit = new JButton("exit");
panel.add(exit);
exit.setBounds(400, 350, 80, 30);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
frame.dispose();
}
一切都运行完美,工作完美,但我打开添加书框三次后出现此问题
这是我运行的两次
最后当我第三次打开这个添加书架时(注意:我没有关闭父框架)
我明白了,它打不开,而且出现错误
现在,在阅读了这个长错误后,我发现它与指向行的某个指针有关,当它从 table 获取数据时,我试图重置它或关闭结果集,但没有任何反应,请帮助我,我明天必须提交这个项目:)
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: illegal component position at
java.awt.Container.addImpl(Container.java:1093) at
javax.swing.JLayeredPane.addImpl(JLayeredPane.java:230) at
javax.swing.JDesktopPane.addImpl(JDesktopPane.java:484) at
java.awt.Container.add(Container.java:410) at
libraraylms.LibrarayLMS.actionPerformed(LibrarayLMS.java:48) at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376) at
com.birosoft.liquid.LiquidMenuItemUI.doClick(LiquidMenuItemUI.java:492)
at
com.birosoft.liquid.LiquidMenuItemUI$MouseInputHandler.mouseReleased(LiquidMenuItemUI.java:1161)
at java.awt.Component.processMouseEvent(Component.java:6516) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at
java.awt.Component.processEvent(Component.java:6281) at
java.awt.Container.processEvent(Container.java:2229) at
java.awt.Component.dispatchEventImpl(Component.java:4872) at
java.awt.Container.dispatchEventImpl(Container.java:2287) at
java.awt.Component.dispatchEvent(Component.java:4698) at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273) at
java.awt.Window.dispatchEventImpl(Window.java:2719) at
java.awt.Component.dispatchEvent(Component.java:4698) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at
java.awt.EventQueue.access0(EventQueue.java:103) at
java.awt.EventQueue.run(EventQueue.java:694) at
java.awt.EventQueue.run(EventQueue.java:692) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at
java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue.run(EventQueue.java:708) at
java.awt.EventQueue.run(EventQueue.java:706) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 10 seconds)
@HovercraftFullOfEels 先生询问
第LibrarayLMS.java:48,LibrarayLMS.java的第48行class是哪一行?
这是代码块
JMenu books = new JMenu("Book's");
mb.add(books);
JMenuItem adbook = new JMenuItem("Add Books");
books.add(adbook);
adbook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Addbook objadd = new Addbook();
objadd.addframe();
objadd.frame.setVisible(true);
desktop.add(objadd.frame); // ******* - LINE #48 *********
try {
objadd.frame.setSelected(true);
} catch (PropertyVetoException exn) {
exn.printStackTrace();
}
}
});
@HovercraftFullOfEels 先生询问“您需要查看 Addbook class 可能包括其 addFrame() 方法及其框架字段。Post 如果可能,请在此处查看。”
这里是加书的完整代码
package libraraylms;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.regex.*;
import static libraraylms.Login.obj;
import static libraraylms.registerstd.frame;
public class Addbook {
public void info(String message, String title) {
JOptionPane.showMessageDialog(null, message, "NOTE:" + title, JOptionPane.INFORMATION_MESSAGE);
}
static JInternalFrame frame = new JInternalFrame("Add Book");
JComboBox category = new JComboBox();
public void addframe() {
try {
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
frame.setSize(700, 568);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
panel.setLayout(null);
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
Statement state = connect.createStatement();
ResultSet rs = state.executeQuery("select book_category from library_category");
while (rs.next()) {
String item = rs.getString("book_category");
category.addItem(item);
}
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
category.setBounds(300, 240, 150, 30);
category.setVisible(true);
panel.add(category);
JLabel heading = new JLabel("ADD BOOK");
heading.setBounds(320, 1, 200, 100);
heading.setFont(new Font("Serif", Font.BOLD, 30));
panel.add(heading);
JLabel b_id = new JLabel("Book code:");
b_id.setBounds(200, 65, 670, 150);
b_id.setForeground(Color.white);
panel.add(b_id);
final JTextField bkid = new JTextField(20);
bkid.setBounds(300, 130, 200, 20);
panel.add(bkid);
JLabel id = new JLabel("Book name:");
id.setBounds(200, 100, 670, 150);
id.setForeground(Color.white);
panel.add(id);
final JTextField txtid = new JTextField(20);
txtid.setBounds(300, 165, 200, 20);
panel.add(txtid);
JLabel author = new JLabel("author name:");
author.setBounds(200, 135, 670, 150);
author.setForeground(Color.white);
panel.add(author);
final JTextField re = new JTextField(20);
re.setBounds(300, 200, 200, 20);
panel.add(re);
JLabel catog = new JLabel("Category:");
catog.setBounds(200, 180, 670, 150);
catog.setForeground(Color.white);
panel.add(catog);
JLabel qun = new JLabel("Book Quantity:");
qun.setBounds(200, 240, 670, 150);
qun.setForeground(Color.white);
panel.add(qun);
final JTextField txtqun = new JTextField(20);
txtqun.setBounds(300, 300, 50, 20);
panel.add(txtqun);
JButton btn_add = new JButton("ADD");
btn_add.setBounds(300, 350, 80, 30);
panel.add(btn_add);
btn_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String[] pattern = new String[4];
pattern[0] = "^[A-Za-z0-9]{3,10}$";
pattern[1] = "^[A-Za-z0-9 ]{3,40}$";
pattern[2] = "^[\p{L} .'-]{4,30}$";
pattern[3] = "^[1-9]\d*$";
//--------------------------------------
String[] field = new String[5];
field[0] = bkid.getText();
field[1] = txtid.getText();
field[2] = re.getText();
field[3] = (String) category.getSelectedItem();
field[4] = txtqun.getText();
if (field[0].isEmpty()) {
obj.info("Please enter book code!", "Error");
return;
}
if (field[1].isEmpty()) {
obj.info("Please enter book name!", "Error");
return;
}
if (field[2].isEmpty()) {
obj.info("Please enter author's name!", "Error");
return;
}
if (field[4].isEmpty()) {
obj.info("Please enter book's quantity!", "Error");
return;
}
Pattern pat1 = Pattern.compile(pattern[0]),
pat2 = Pattern.compile(pattern[1]),
pat3 = Pattern.compile(pattern[2]),
pat4 = Pattern.compile(pattern[3]);
Matcher mat1 = pat1.matcher(field[0]),
mat2 = pat2.matcher(field[1]),
mat3 = pat3.matcher(field[2]),
mat4 = pat4.matcher(field[4]);
if (mat1.matches()) {
if (mat2.matches()) {
if (mat3.matches()) {
if (mat4.matches()) {
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
PreparedStatement pst = connect.prepareStatement("insert into library_book values (?,?,?,?,?);");
pst.setString(1, field[0]);
pst.setString(2, field[1]);
pst.setString(3, field[2]);
pst.setString(4, field[3]);
pst.setString(5, field[4]);
pst.execute();
frame.dispose();
obj.info("You've successfully added " + field[1] + " book to Library System Management", "Congratulations!");
connect.close();
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
} else {
obj.info("Please use only numbers in book quantity!", "Error");
}
} else {
obj.info("Please use only alphabets in Author Name!", "Error");
}
} else {
obj.info("Please use only alphabets in Book Name!", "Error");
}
} else {
obj.info("Please use only numbers and alphabets in Book Code!", "Error");
}
}
});
JButton exit = new JButton("exit");
panel.add(exit);
exit.setBounds(400, 350, 80, 30);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
frame.dispose();
}
});
JLabel logo = new JLabel(new ImageIcon("C:\Users\ramb0\Documents\NetBeansProjects\LibrarayLMS\book.jpg"));
panel.add(logo);
logo.setSize(700, 568);
frame.setVisible(true);
}
}
这里是父class、主机、KING、
的代码
package libraraylms;
import javax.swing.JDesktopPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.*;
public class LibrarayLMS {
public void info(String message, String title) {
JOptionPane.showMessageDialog(null, message, "NOTE:" + title, JOptionPane.INFORMATION_MESSAGE);
}
JDesktopPane desktop;
JFrame f = new JFrame("A.M Library");
public void framelaunch() {
try {
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
} catch (Exception ec) {
ec.printStackTrace();
}
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
mb.add(menu);
JMenuItem exit = new JMenuItem("Quit");
menu.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenu books = new JMenu("Book's");
mb.add(books);
JMenuItem adbook = new JMenuItem("Add Books");
books.add(adbook);
adbook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Addbook objadd = new Addbook();
objadd.addframe();
objadd.frame.setVisible(true);
desktop.add(objadd.frame);
try {
objadd.frame.setSelected(true);
} catch (PropertyVetoException exn) {
exn.printStackTrace();
}
}
});
JMenuItem issuebook = new JMenuItem("Issue Book");
books.add(issuebook);
issuebook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
issue frame = new issue();
frame.LaunchInternalFrame();
frame.interFrame.setVisible(true);
desktop.add(frame.interFrame);
try {
frame.interFrame.setSelected(true);
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
}
}
);
JMenuItem rebook = new JMenuItem("Return Book");
books.add(rebook);
rebook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
returns obj = new returns();
obj.returnframe();
obj.frame.setVisible(true);
desktop.add(obj.frame);
try {
obj.frame.setSelected(true);
} catch (PropertyVetoException exx) {
exx.printStackTrace();
}
}
});
JMenuItem bookstatus = new JMenuItem("Book Status");
books.add(bookstatus);
bookstatus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookstatus objst = new bookstatus();
objst.status();
objst.framest.setVisible(true);
desktop.add(objst.framest);
try {
objst.framest.setSelected(true);
} catch (PropertyVetoException exxn) {
exxn.printStackTrace();
}
}
});
JMenuItem bookscat = new JMenuItem("Add Category");
books.add(bookscat);
bookscat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addCategory objcat = new addCategory();
objcat.category();
objcat.catframe.setVisible(true);
desktop.add(objcat.catframe);
try {
objcat.catframe.setSelected(true);
} catch (PropertyVetoException n) {
n.printStackTrace();
}
}
});
JMenu reg = new JMenu("Register");
mb.add(reg);
JMenuItem adm = new JMenuItem("Admin");
reg.add(adm);
adm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
registeradmin amframe= new registeradmin();
amframe.register();
amframe.adminframe.setVisible(true);
desktop.add(amframe.adminframe);
try{
amframe.adminframe.setSelected(true);
}catch (PropertyVetoException eee) {
eee.printStackTrace();
}
}
});
JMenuItem remem = new JMenuItem("Member");
reg.add(remem);
remem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent en) {
registerstd f = new registerstd();
f.register();
f.frame.setVisible(true);
desktop.add(f.frame);
try {
f.frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
});
JMenuItem report = new JMenuItem("Report");
books.add(report);
report.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
report frm = new report();
frm.reports();
frm.reportframe.setVisible(true);
desktop.add(frm.reportframe);
try {
frm.reportframe.setSelected(true);
} catch (PropertyVetoException el) {
el.printStackTrace();
}
}
});
JMenu Help = new JMenu("Help");
mb.add(Help);
JMenuItem help = new JMenuItem("Help");
Help.add(help);
JMenuItem About = new JMenuItem("About");
Help.add(About);
About.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
about frme = new about();
frme.aboutframe();
frme.abframe.setVisible(true);
desktop.add(frme.abframe);
try {
frme.abframe.setSelected(true);
} catch (PropertyVetoException el) {
el.printStackTrace();
}
}
});
desktop = new JDesktopPane(); //a specialized layered pane
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
JLabel logo = new JLabel(new ImageIcon("C:\Users\ramb0\Documents\NetBeansProjects\LibrarayLMS\download.jpg"));
desktop.add(logo);
logo.setSize(1000, 625);
f.getContentPane().add(desktop);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setJMenuBar(mb);
f.setSize(1000, 625);
f.setResizable(false);
f.setContentPane(desktop);
}
public static void main(String[] args) {
LibrarayLMS myframe = new LibrarayLMS();
myframe.framelaunch();
}
}
是的,您的问题是您正在使用静态 JInternalFrame 并将相同的内部框架一遍又一遍地添加到 JDesktopPane。不要这样做。相反,如果您必须使用 JInternalFrames,则根据需要创建一个新的。我自己,我会通过 CardLayout 交换 JPanels。
这是我的 true-MCVE,一个可重现您的问题的小型可编译程序。请注意,我通过连续注释您的代码块来创建它,然后删除注释块(如果它们不需要编译和重现问题),直到我只剩下显示问题的基本代码。
请评论和取消评论指定的行。
import java.awt.event.*;
import java.beans.PropertyVetoException;
import javax.swing.*;
public class Mcve1 {
private static void createAndShowGui() {
LibrarayLMS.main(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class Addbook {
static JInternalFrame frame = new JInternalFrame("Add Book");
public void addframe() {
JPanel panel = new JPanel();
// ****** comment and un-comment the line below
// frame = new JInternalFrame("Add Book");
frame.setSize(700, 568);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
JButton btn_add = new JButton("ADD");
panel.add(btn_add);
btn_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
frame.dispose();
}
});
JButton exit = new JButton("exit");
panel.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
frame.setVisible(true);
}
}
class LibrarayLMS {
JDesktopPane desktop;
JFrame f = new JFrame("A.M Library");
public void framelaunch() {
JMenuBar mb = new JMenuBar();
JMenu books = new JMenu("Book's");
mb.add(books);
JMenuItem adbook = new JMenuItem("Add Books");
books.add(adbook);
adbook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Addbook objadd = new Addbook();
objadd.addframe();
objadd.frame.setVisible(true);
desktop.add(objadd.frame); // !! ***** here *****
try {
objadd.frame.setSelected(true);
} catch (PropertyVetoException exn) {
exn.printStackTrace();
}
}
});
desktop = new JDesktopPane();
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
f.getContentPane().add(desktop);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setJMenuBar(mb);
f.setSize(1000, 625);
f.setResizable(false);
f.setContentPane(desktop);
}
public static void main(String[] args) {
LibrarayLMS myframe = new LibrarayLMS();
myframe.framelaunch();
}
}
同样,您的代码中存在对 static 的误用,我尚未更改。同样根据我的评论,您应该避免使用空布局和使用 setBounds(...) 来放置组件,因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕而且很难更新和维护。此外,您的代码不遵守 Swing 线程规则,因为您在 Swing 事件线程上有很多长 运行 代码 运行。考虑在后台线程(例如 SwingWorker 派生线程)上调用 long-运行 代码。
请记住这个隔离错误的技巧,以后会派上用场的。
我正在制作图书馆管理系统, 在这里,当你添加书籍时,你 select 来自 jcombobox 的类别,现在 jcombobox 从另一个 table 获取类别(项目),问题是它在它之后完美运行两到三次,它给出一个错误并且 ADD BOOK 框架没有加载,编码和错误消息和屏幕截图如下:)
类别Table
图书Table
图书类别是 FK 类别 table
现在这是从类别 table 中获取数据到 jcombobox
的代码JComboBox category = new JComboBox();
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
Statement state = connect.createStatement();
ResultSet rs = state.executeQuery("select book_category from library_category");
while (rs.next()) {
String item = rs.getString("book_category");
category.addItem(item);
}
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
category.setBounds(300, 240, 150, 30);
category.setVisible(true);
panel.add(category);
这是另一半代码 (代码太长所以我只是发布关于这个问题的所有内容)
JButton btn_add = new JButton("ADD");
btn_add.setBounds(300, 350, 80, 30);
panel.add(btn_add);
btn_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String[] pattern = new String[4];
pattern[0] = "^[A-Za-z0-9]{3,10}$";
pattern[1] = "^[A-Za-z0-9 ]{3,40}$";
pattern[2] = "^[\p{L} .'-]{4,30}$";
pattern[3] = "^[1-9]\d*$";
//--------------------------------------
String[] field = new String[5];
field[0] = bkid.getText();
field[1] = txtid.getText();
field[2] = re.getText();
field[3] = (String) category.getSelectedItem();
field[4] = txtqun.getText();
if (field[0].isEmpty()) {
obj.info("Please enter book code!", "Error");
return;
}
if (field[1].isEmpty()) {
obj.info("Please enter book name!", "Error");
return;
}
if (field[2].isEmpty()) {
obj.info("Please enter author's name!", "Error");
return;
}
if (field[4].isEmpty()) {
obj.info("Please enter book's quantity!", "Error");
return;
}
Pattern pat1 = Pattern.compile(pattern[0]),
pat2 = Pattern.compile(pattern[1]),
pat3 = Pattern.compile(pattern[2]),
pat4 = Pattern.compile(pattern[3]);
Matcher mat1 = pat1.matcher(field[0]),
mat2 = pat2.matcher(field[1]),
mat3 = pat3.matcher(field[2]),
mat4 = pat4.matcher(field[4]);
if (mat1.matches()) {
if (mat2.matches()) {
if (mat3.matches()) {
if (mat4.matches()) {
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
PreparedStatement pst = connect.prepareStatement("insert into library_book values (?,?,?,?,?);");
pst.setString(1, field[0]);
pst.setString(2, field[1]);
pst.setString(3, field[2]);
pst.setString(4, field[3]);
pst.setString(5, field[4]);
pst.execute();
frame.dispose();
obj.info("You've successfully added " + field[1] + " book to Library System Management", "Congratulations!");
connect.close();
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
} else {
obj.info("Please use only numbers in book quantity!", "Error");
}
} else {
obj.info("Please use only alphabets in Author Name!", "Error");
}
} else {
obj.info("Please use only alphabets in Book Name!", "Error");
}
} else {
obj.info("Please use only numbers and alphabets in Book Code!", "Error");
}
}
});
JButton exit = new JButton("exit");
panel.add(exit);
exit.setBounds(400, 350, 80, 30);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
frame.dispose();
}
一切都运行完美,工作完美,但我打开添加书框三次后出现此问题
这是我运行的两次
最后当我第三次打开这个添加书架时(注意:我没有关闭父框架)
我明白了,它打不开,而且出现错误
现在,在阅读了这个长错误后,我发现它与指向行的某个指针有关,当它从 table 获取数据时,我试图重置它或关闭结果集,但没有任何反应,请帮助我,我明天必须提交这个项目:)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position at java.awt.Container.addImpl(Container.java:1093) at javax.swing.JLayeredPane.addImpl(JLayeredPane.java:230) at javax.swing.JDesktopPane.addImpl(JDesktopPane.java:484) at java.awt.Container.add(Container.java:410) at libraraylms.LibrarayLMS.actionPerformed(LibrarayLMS.java:48) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.AbstractButton.doClick(AbstractButton.java:376) at com.birosoft.liquid.LiquidMenuItemUI.doClick(LiquidMenuItemUI.java:492) at com.birosoft.liquid.LiquidMenuItemUI$MouseInputHandler.mouseReleased(LiquidMenuItemUI.java:1161) at java.awt.Component.processMouseEvent(Component.java:6516) at javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at java.awt.Component.processEvent(Component.java:6281) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4872) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access0(EventQueue.java:103) at java.awt.EventQueue.run(EventQueue.java:694) at java.awt.EventQueue.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue.run(EventQueue.java:708) at java.awt.EventQueue.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 10 seconds)
@HovercraftFullOfEels 先生询问
第LibrarayLMS.java:48,LibrarayLMS.java的第48行class是哪一行?
这是代码块
JMenu books = new JMenu("Book's");
mb.add(books);
JMenuItem adbook = new JMenuItem("Add Books");
books.add(adbook);
adbook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Addbook objadd = new Addbook();
objadd.addframe();
objadd.frame.setVisible(true);
desktop.add(objadd.frame); // ******* - LINE #48 *********
try {
objadd.frame.setSelected(true);
} catch (PropertyVetoException exn) {
exn.printStackTrace();
}
}
});
@HovercraftFullOfEels 先生询问“您需要查看 Addbook class 可能包括其 addFrame() 方法及其框架字段。Post 如果可能,请在此处查看。”
这里是加书的完整代码
package libraraylms;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.regex.*;
import static libraraylms.Login.obj;
import static libraraylms.registerstd.frame;
public class Addbook {
public void info(String message, String title) {
JOptionPane.showMessageDialog(null, message, "NOTE:" + title, JOptionPane.INFORMATION_MESSAGE);
}
static JInternalFrame frame = new JInternalFrame("Add Book");
JComboBox category = new JComboBox();
public void addframe() {
try {
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
frame.setSize(700, 568);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
panel.setLayout(null);
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
Statement state = connect.createStatement();
ResultSet rs = state.executeQuery("select book_category from library_category");
while (rs.next()) {
String item = rs.getString("book_category");
category.addItem(item);
}
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
category.setBounds(300, 240, 150, 30);
category.setVisible(true);
panel.add(category);
JLabel heading = new JLabel("ADD BOOK");
heading.setBounds(320, 1, 200, 100);
heading.setFont(new Font("Serif", Font.BOLD, 30));
panel.add(heading);
JLabel b_id = new JLabel("Book code:");
b_id.setBounds(200, 65, 670, 150);
b_id.setForeground(Color.white);
panel.add(b_id);
final JTextField bkid = new JTextField(20);
bkid.setBounds(300, 130, 200, 20);
panel.add(bkid);
JLabel id = new JLabel("Book name:");
id.setBounds(200, 100, 670, 150);
id.setForeground(Color.white);
panel.add(id);
final JTextField txtid = new JTextField(20);
txtid.setBounds(300, 165, 200, 20);
panel.add(txtid);
JLabel author = new JLabel("author name:");
author.setBounds(200, 135, 670, 150);
author.setForeground(Color.white);
panel.add(author);
final JTextField re = new JTextField(20);
re.setBounds(300, 200, 200, 20);
panel.add(re);
JLabel catog = new JLabel("Category:");
catog.setBounds(200, 180, 670, 150);
catog.setForeground(Color.white);
panel.add(catog);
JLabel qun = new JLabel("Book Quantity:");
qun.setBounds(200, 240, 670, 150);
qun.setForeground(Color.white);
panel.add(qun);
final JTextField txtqun = new JTextField(20);
txtqun.setBounds(300, 300, 50, 20);
panel.add(txtqun);
JButton btn_add = new JButton("ADD");
btn_add.setBounds(300, 350, 80, 30);
panel.add(btn_add);
btn_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String[] pattern = new String[4];
pattern[0] = "^[A-Za-z0-9]{3,10}$";
pattern[1] = "^[A-Za-z0-9 ]{3,40}$";
pattern[2] = "^[\p{L} .'-]{4,30}$";
pattern[3] = "^[1-9]\d*$";
//--------------------------------------
String[] field = new String[5];
field[0] = bkid.getText();
field[1] = txtid.getText();
field[2] = re.getText();
field[3] = (String) category.getSelectedItem();
field[4] = txtqun.getText();
if (field[0].isEmpty()) {
obj.info("Please enter book code!", "Error");
return;
}
if (field[1].isEmpty()) {
obj.info("Please enter book name!", "Error");
return;
}
if (field[2].isEmpty()) {
obj.info("Please enter author's name!", "Error");
return;
}
if (field[4].isEmpty()) {
obj.info("Please enter book's quantity!", "Error");
return;
}
Pattern pat1 = Pattern.compile(pattern[0]),
pat2 = Pattern.compile(pattern[1]),
pat3 = Pattern.compile(pattern[2]),
pat4 = Pattern.compile(pattern[3]);
Matcher mat1 = pat1.matcher(field[0]),
mat2 = pat2.matcher(field[1]),
mat3 = pat3.matcher(field[2]),
mat4 = pat4.matcher(field[4]);
if (mat1.matches()) {
if (mat2.matches()) {
if (mat3.matches()) {
if (mat4.matches()) {
try {
MyConnection mc = new MyConnection();
Class.forName(mc.driver);
Connection connect = DriverManager.getConnection(mc.login);
PreparedStatement pst = connect.prepareStatement("insert into library_book values (?,?,?,?,?);");
pst.setString(1, field[0]);
pst.setString(2, field[1]);
pst.setString(3, field[2]);
pst.setString(4, field[3]);
pst.setString(5, field[4]);
pst.execute();
frame.dispose();
obj.info("You've successfully added " + field[1] + " book to Library System Management", "Congratulations!");
connect.close();
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
} catch (SQLException ex) {
System.out.println("Error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found");
ex.printStackTrace();
}
} else {
obj.info("Please use only numbers in book quantity!", "Error");
}
} else {
obj.info("Please use only alphabets in Author Name!", "Error");
}
} else {
obj.info("Please use only alphabets in Book Name!", "Error");
}
} else {
obj.info("Please use only numbers and alphabets in Book Code!", "Error");
}
}
});
JButton exit = new JButton("exit");
panel.add(exit);
exit.setBounds(400, 350, 80, 30);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bkid.setText("");
txtid.setText("");
re.setText("");
txtqun.setText("");
frame.dispose();
}
});
JLabel logo = new JLabel(new ImageIcon("C:\Users\ramb0\Documents\NetBeansProjects\LibrarayLMS\book.jpg"));
panel.add(logo);
logo.setSize(700, 568);
frame.setVisible(true);
}
}
这里是父class、主机、KING、
的代码package libraraylms;
import javax.swing.JDesktopPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.*;
public class LibrarayLMS {
public void info(String message, String title) {
JOptionPane.showMessageDialog(null, message, "NOTE:" + title, JOptionPane.INFORMATION_MESSAGE);
}
JDesktopPane desktop;
JFrame f = new JFrame("A.M Library");
public void framelaunch() {
try {
UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
} catch (Exception ec) {
ec.printStackTrace();
}
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
mb.add(menu);
JMenuItem exit = new JMenuItem("Quit");
menu.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenu books = new JMenu("Book's");
mb.add(books);
JMenuItem adbook = new JMenuItem("Add Books");
books.add(adbook);
adbook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Addbook objadd = new Addbook();
objadd.addframe();
objadd.frame.setVisible(true);
desktop.add(objadd.frame);
try {
objadd.frame.setSelected(true);
} catch (PropertyVetoException exn) {
exn.printStackTrace();
}
}
});
JMenuItem issuebook = new JMenuItem("Issue Book");
books.add(issuebook);
issuebook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
issue frame = new issue();
frame.LaunchInternalFrame();
frame.interFrame.setVisible(true);
desktop.add(frame.interFrame);
try {
frame.interFrame.setSelected(true);
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
}
}
);
JMenuItem rebook = new JMenuItem("Return Book");
books.add(rebook);
rebook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
returns obj = new returns();
obj.returnframe();
obj.frame.setVisible(true);
desktop.add(obj.frame);
try {
obj.frame.setSelected(true);
} catch (PropertyVetoException exx) {
exx.printStackTrace();
}
}
});
JMenuItem bookstatus = new JMenuItem("Book Status");
books.add(bookstatus);
bookstatus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookstatus objst = new bookstatus();
objst.status();
objst.framest.setVisible(true);
desktop.add(objst.framest);
try {
objst.framest.setSelected(true);
} catch (PropertyVetoException exxn) {
exxn.printStackTrace();
}
}
});
JMenuItem bookscat = new JMenuItem("Add Category");
books.add(bookscat);
bookscat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addCategory objcat = new addCategory();
objcat.category();
objcat.catframe.setVisible(true);
desktop.add(objcat.catframe);
try {
objcat.catframe.setSelected(true);
} catch (PropertyVetoException n) {
n.printStackTrace();
}
}
});
JMenu reg = new JMenu("Register");
mb.add(reg);
JMenuItem adm = new JMenuItem("Admin");
reg.add(adm);
adm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
registeradmin amframe= new registeradmin();
amframe.register();
amframe.adminframe.setVisible(true);
desktop.add(amframe.adminframe);
try{
amframe.adminframe.setSelected(true);
}catch (PropertyVetoException eee) {
eee.printStackTrace();
}
}
});
JMenuItem remem = new JMenuItem("Member");
reg.add(remem);
remem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent en) {
registerstd f = new registerstd();
f.register();
f.frame.setVisible(true);
desktop.add(f.frame);
try {
f.frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
});
JMenuItem report = new JMenuItem("Report");
books.add(report);
report.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
report frm = new report();
frm.reports();
frm.reportframe.setVisible(true);
desktop.add(frm.reportframe);
try {
frm.reportframe.setSelected(true);
} catch (PropertyVetoException el) {
el.printStackTrace();
}
}
});
JMenu Help = new JMenu("Help");
mb.add(Help);
JMenuItem help = new JMenuItem("Help");
Help.add(help);
JMenuItem About = new JMenuItem("About");
Help.add(About);
About.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
about frme = new about();
frme.aboutframe();
frme.abframe.setVisible(true);
desktop.add(frme.abframe);
try {
frme.abframe.setSelected(true);
} catch (PropertyVetoException el) {
el.printStackTrace();
}
}
});
desktop = new JDesktopPane(); //a specialized layered pane
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
JLabel logo = new JLabel(new ImageIcon("C:\Users\ramb0\Documents\NetBeansProjects\LibrarayLMS\download.jpg"));
desktop.add(logo);
logo.setSize(1000, 625);
f.getContentPane().add(desktop);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setJMenuBar(mb);
f.setSize(1000, 625);
f.setResizable(false);
f.setContentPane(desktop);
}
public static void main(String[] args) {
LibrarayLMS myframe = new LibrarayLMS();
myframe.framelaunch();
}
}
是的,您的问题是您正在使用静态 JInternalFrame 并将相同的内部框架一遍又一遍地添加到 JDesktopPane。不要这样做。相反,如果您必须使用 JInternalFrames,则根据需要创建一个新的。我自己,我会通过 CardLayout 交换 JPanels。
这是我的 true-MCVE,一个可重现您的问题的小型可编译程序。请注意,我通过连续注释您的代码块来创建它,然后删除注释块(如果它们不需要编译和重现问题),直到我只剩下显示问题的基本代码。
请评论和取消评论指定的行。
import java.awt.event.*;
import java.beans.PropertyVetoException;
import javax.swing.*;
public class Mcve1 {
private static void createAndShowGui() {
LibrarayLMS.main(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class Addbook {
static JInternalFrame frame = new JInternalFrame("Add Book");
public void addframe() {
JPanel panel = new JPanel();
// ****** comment and un-comment the line below
// frame = new JInternalFrame("Add Book");
frame.setSize(700, 568);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
JButton btn_add = new JButton("ADD");
panel.add(btn_add);
btn_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
frame.dispose();
}
});
JButton exit = new JButton("exit");
panel.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
frame.setVisible(true);
}
}
class LibrarayLMS {
JDesktopPane desktop;
JFrame f = new JFrame("A.M Library");
public void framelaunch() {
JMenuBar mb = new JMenuBar();
JMenu books = new JMenu("Book's");
mb.add(books);
JMenuItem adbook = new JMenuItem("Add Books");
books.add(adbook);
adbook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Addbook objadd = new Addbook();
objadd.addframe();
objadd.frame.setVisible(true);
desktop.add(objadd.frame); // !! ***** here *****
try {
objadd.frame.setSelected(true);
} catch (PropertyVetoException exn) {
exn.printStackTrace();
}
}
});
desktop = new JDesktopPane();
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
f.getContentPane().add(desktop);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setJMenuBar(mb);
f.setSize(1000, 625);
f.setResizable(false);
f.setContentPane(desktop);
}
public static void main(String[] args) {
LibrarayLMS myframe = new LibrarayLMS();
myframe.framelaunch();
}
}
同样,您的代码中存在对 static 的误用,我尚未更改。同样根据我的评论,您应该避免使用空布局和使用 setBounds(...) 来放置组件,因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕而且很难更新和维护。此外,您的代码不遵守 Swing 线程规则,因为您在 Swing 事件线程上有很多长 运行 代码 运行。考虑在后台线程(例如 SwingWorker 派生线程)上调用 long-运行 代码。
请记住这个隔离错误的技巧,以后会派上用场的。