我正在用awt框架中的Java开发图书馆系统。 如果用户单击在不同组件的同一窗口中打开添加书籍界面。例如,单击后,添加书籍菜单组件删除并添加添加到同一窗口的书籍组件。我的问题是用户在部分添加书后,如果用户点击菜单部分,虽然添加了组件,但菜单组件未显示。主要活动: public class LibrarySystemInterface扩展了javax.swing.JFrame实现
PanelWorker {
private FileReaderWriter writer;
private LibraryIterator forCode ;
private Library kutuphane;
private Collection collection;
private int count=1;
private PanelWorker getToWork;
private Library library = null;
private static LibrarySystemInterface instance;
public LibrarySystemInterface() {
initComponents();
writer=new FileReaderWriter("prevent");
kutuphane=Library.getInstance();
}
private void initComponents() {
panel1 = new java.awt.Panel();
label1 = new java.awt.Label();
list1 = new java.awt.List();
label2 = new java.awt.Label();
list2 = new java.awt.List();
ekle = new java.awt.Button();
sil = new java.awt.Button();
ara = new java.awt.Button();
iade = new java.awt.Button();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
label1.setText("Butun Kitaplarin Listesi");
list1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
list1ActionPerformed(evt);
}
});
label2.setText("Odunc Verilcek Kitaplarin Listesi");
ekle.setLabel("Kitap Ekle");
ekle.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ekleActionPerformed(evt);
}
});
sil.setLabel("Kitap Sil");
sil.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
silActionPerformed(evt);
}
});
ara.setActionCommand("Kitap Ara");
ara.setLabel("Kitap Ara");
ara.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
araActionPerformed(evt);
}
});
iade.setLabel("Kitap Iade");
iade.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
iadeActionPerformed(evt);
}
});
javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
panel1.setLayout(panel1Layout);
panel1Layout.setHorizontalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(ara, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sil, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ekle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
.addComponent(iade, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(panel1Layout.createSequentialGroup()
.addGap(67, 67, 67)
.addComponent(label2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(list2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 8, Short.MAX_VALUE))
);
panel1Layout.setVerticalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
.addComponent(ekle, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sil, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(panel1Layout.createSequentialGroup()
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(list1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)))
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(panel1Layout.createSequentialGroup()
.addComponent(ara, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(iade, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(list2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void ekleActionPerformed(java.awt.event.ActionEvent evt) {
panel1.removeAll();
getToWork=PanelFactory.getWork("add");
this.add(getToWork.builder());
this.repaint();
this.validate();
}
public static void main(String args[]) {
instance=LibrarySystemInterface.getInstance();
}
public static LibrarySystemInterface getInstance() {
if (instance==null)
return instance=new LibrarySystemInterface();
else return instance;
}
private java.awt.Button ara;
private java.awt.Button ekle;
private java.awt.Button iade;
private java.awt.Label label1;
private java.awt.Label label2;
private java.awt.List list1;
private java.awt.List list2;
private java.awt.Panel panel1;
private java.awt.Button sil;
// End of variables declaration
@Override
public Panel builder() {
this.initComponents();
updateNList();
return panel1;
}
public void updateNList(){
list1.removeAll();
ArrayList<Collection> item=new ArrayList<Collection>();
kutuphane=library.getInstance();
forCode= kutuphane.iterator();
while(!forCode.hasNext()){
collection=(Collection) forCode.next();
String itemStr=collection.type + ", " + collection.name + ", " + collection.auther + ", " + collection.date
+ ", " + collection.code;
list2.add(itemStr);
}
}
}
添加书籍部分
public class EkleInterface extends javax.swing.JFrame implements PanelWorker{
private FileReaderWriter writer;
private LibraryIterator forCode ;
private Library kutuphane;
private Collection collection;
private int count=1;
private PanelWorker getToWork;
private Library library = null;
public EkleInterface() {
initComponents();
writer=new FileReaderWriter("prevent");
kutuphane=Library.getInstance();
private void initComponents() {
panel2 = new java.awt.Panel();
label1 = new java.awt.Label();
textField1 = new java.awt.TextField();
label2 = new java.awt.Label();
textField2 = new java.awt.TextField();
label3 = new java.awt.Label();
textField3 = new java.awt.TextField();
label4 = new java.awt.Label();
textField4 = new java.awt.TextField();
ekle = new java.awt.Button();
Menu = new java.awt.Button();
label5 = new java.awt.Label();
list2 = new java.awt.List();
label6 = new java.awt.Label();
list1 = new java.awt.List();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
panel2.setName("");
label1.setText("Tur:");
textField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField1ActionPerformed(evt);
}
});
label2.setText("Ismi");
textField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField2ActionPerformed(evt);
}
});
label3.setText("Yazari");
textField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField3ActionPerformed(evt);
}
});
label4.setText("Yayınlanma Tarihi");
textField4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField4ActionPerformed(evt);
}
});
ekle.setLabel("Ekle");
ekle.setName(""); // NOI18N
ekle.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ekleActionPerformed(evt);
}
});
Menu.setLabel("Menu");
Menu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuActionPerformed(evt);
}
});
label5.setText("Butun Kitaplarin Listesi");
label6.setText("Odunc Verilcek Kitaplarin Listesi");
list1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
list1ActionPerformed(evt);
}
});
javax.swing.GroupLayout panel2Layout = new javax.swing.GroupLayout(panel2);
panel2.setLayout(panel2Layout);
panel2Layout.setHorizontalGroup(
panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel2Layout.createSequentialGroup()
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(panel2Layout.createSequentialGroup()
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(53, 53, 53)
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(panel2Layout.createSequentialGroup()
.addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(panel2Layout.createSequentialGroup()
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel2Layout.createSequentialGroup()
.addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(list2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(panel2Layout.createSequentialGroup()
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(panel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(ekle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6)
.addComponent(Menu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addComponent(textField4, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel2Layout.createSequentialGroup()
.addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addComponent(list1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
panel2Layout.setVerticalGroup(
panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel2Layout.createSequentialGroup()
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(panel2Layout.createSequentialGroup()
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(panel2Layout.createSequentialGroup()
.addComponent(list2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)))
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel2Layout.createSequentialGroup()
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(label4, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(textField4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ekle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Menu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(47, Short.MAX_VALUE))
.addGroup(panel2Layout.createSequentialGroup()
.addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(list1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 14, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void ekleActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String type=textField1.getText();
String name=textField2.getText();
String date=textField4.getText();
String auther=textField3.getText();
forCode= kutuphane.iterator();
while(!forCode.hasNext()){
collection=(Collection) forCode.next();
count=collection.code+1;
}
String url = "jdbc:mysql://127.0.0.1:3306/";
String dbName = "kutuphane";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "123456";
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
Statement st = conn.createStatement();
String query = " insert into kitaplar (tur,yazar,tarih, ismi)"
+ " values ( ?, ?, ?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, type);
preparedStmt.setString (2, auther);
preparedStmt.setString (3, date);
preparedStmt.setString(4, name);
preparedStmt.execute();
conn.close();
}
catch (ClassNotFoundException k) {
} catch (InstantiationException k) {
} catch (IllegalAccessException k) {
} catch (SQLException k) {
}
kutuphane.addAggregate(collection=new Collection(type,name,auther,
date,count));
updateNList();
}
private void MenuActionPerformed(java.awt.event.ActionEvent evt) {
panel2.removeAll();
getToWork=PanelFactory.getWork("menu");
this.add(getToWork.builder());
this.repaint();
this.validate();
}
private java.awt.Button Menu;
private java.awt.Button ekle;
private java.awt.Label label1;
private java.awt.Label label2;
private java.awt.Label label3;
private java.awt.Label label4;
private java.awt.Label label5;
private java.awt.Label label6;
private java.awt.List list1;
private java.awt.List list2;
private java.awt.Panel panel2;
private java.awt.TextField textField1;
private java.awt.TextField textField2;
private java.awt.TextField textField3;
private java.awt.TextField textField4;
public Panel builder() {
initComponents();
updateNList();
return panel2;
}
public void updateNList(){
list1.removeAll();
ArrayList<Collection> item=new ArrayList<Collection>();
kutuphane=library.getInstance();
forCode= kutuphane.iterator();
while(!forCode.hasNext()){
collection=(Collection) forCode.next();
String itemStr=collection.type + ", " + collection.name + ", " + collection.auther + ", " + collection.date
+ ", " + collection.code;
list2.add(itemStr);
}
}
}
接口:
public interface PanelWorker {
public Panel builder();
}
public class PanelFactory {
public static PanelWorker getWork(String whereTo){
if(whereTo.equals("add")){
return new EkleInterface();
}
else if (whereTo.equals("delete")){
return new SilInterci();
}
else if (whereTo.equals("menu"))
return new LibrarySystemInterface();
else if(whereTo.equals("search"))
return new AraInterface();
else if(whereTo.equals("rentFinish"))
return new IadeInterface();
return null;
}
}