在基于swing的项目中,我使用向导创建一个主细节类并填充J表并在另一个类中创建一个按钮Record并将主细节类的对象放在Record按钮的动作监听器中。但是当我单击Record按钮主细节类时运行对接时不显示GUI窗口当我将其作为主类运行或直接运行时它运行成功并显示GUI窗口并显示来自数据库的记录。我很困惑,因为其他类运行单击记录按钮如果我替换主客户端类的对象与任何其他类,但只有主细节类不显示GUI窗口。这是两个类的代码是主要细节,名称为BrRecord和其他类,处理它的名称为Balance.Thanks。
package my.soft;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BrRecord extends javax.swing.JPanel {
public BrRecord() {
initComponents();
if (!Beans.isDesignTime()) {
entityManager.getTransaction().begin();
}
}
private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
entityManager.getTransaction().rollback();
entityManager.getTransaction().begin();
java.util.Collection data = query.getResultList();
for (Object entity : data) {
entityManager.refresh(entity);
}
list.clear();
list.addAll(data);
}
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
int[] selected = masterTable.getSelectedRows();
List<my.soft.Expance1> toRemove = new ArrayList<my.soft.Expance1>(selected.length);
for (int idx = 0; idx < selected.length; idx++) {
my.soft.Expance1 e = list.get(masterTable.convertRowIndexToModel(selected[idx]));
toRemove.add(e);
entityManager.remove(e);
}
list.removeAll(toRemove);
}
private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {
my.soft.Expance1 e = new my.soft.Expance1();
entityManager.persist(e);
list.add(e);
int row = list.size() - 1;
masterTable.setRowSelectionInterval(row, row);
masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
}
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
} catch (RollbackException rex) {
rex.printStackTrace();
entityManager.getTransaction().begin();
List<my.soft.Expance1> merged = new ArrayList<my.soft.Expance1>(list.size());
for (my.soft.Expance1 e : list) {
merged.add(entityManager.merge(e));
}
list.clear();
list.addAll(merged);
}
}
private javax.swing.JTextField breakfastField;
private javax.swing.JLabel breakfastLabel;
private javax.swing.JTextField dateField;
private javax.swing.JLabel dateLabel;
private javax.swing.JButton deleteButton;
private javax.persistence.EntityManager entityManager;
private javax.swing.JTextField expance1Field;
private javax.swing.JLabel expance1Label;
private java.util.List<my.soft.Expance1> list;
private javax.swing.JScrollPane masterScrollPane;
private javax.swing.JTable masterTable;
private javax.swing.JButton newButton;
private javax.persistence.Query query;
private javax.swing.JButton refreshButton;
private javax.swing.JButton saveButton;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() {
public void run() {
new BrRecord().setVisible(true);
JFrame frame = new JFrame();
frame.setContentPane(new BrRecord());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
Balance
来源 package my.soft;
//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import static com.sun.corba.se.spi.presentation.rmi.StubAdapter.request;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import static java.text.DateFormat.Field.DAY_OF_MONTH;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Balance extends javax.swing.JFrame { public Balance() {
initComponents();
CurrentDate();
conn = MySql.connectToDB();
}
private void bRecordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BrRecord Br = new BrRecord();
Br.setVisible(true);
// DISPOSE_ON_CLOSE();
Close();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Balance().setVisible(true);
}
});
}