我有一个主JFrame
,由3个小JPanel
组成,根据用户的选择进行切换。
问题是我必须将插入JPanel
的数据保存到数据库中,但我无法直接访问JPanel
的元素。那么我该如何接受呢。
在主框架中单击“接受”按钮时,将完成DB中的条目。 请帮忙
if(Selected==Credit Card){
Select Credit Card Panel;
}
if(Selected==Debit Card){
Select Debit Card Panel;
}
if(Selected==Cash on Delivery){
Select Cash on Delivery Panel;
}
onAccept(){
if(Selected==Credit Card){
get data from Credit Card panel and store in Database; (variables like cc number, cvv)
}
if(Selected==Debit Card){
get data from Debit Card panel and store in Database;
}
if(Selected==Cash on Delivery){
get data from Cash on Delivery Card panel and store in Database;
}
}
答案 0 :(得分:2)
让3个面板中的每个面板实现相同的界面:
public interface DataStorer {
public void storeDataInDatabase();
}
然后,当点击接受按钮时,请调用:
DataStorer selectedPanel = (DataStorer) theDisplayedPanel;
selectedPanel.storeDataInDatabase();