[英]How to transfer data between panels of cardLayout
在我的軟件中,我使用卡片布局來創建“基於向導”的界面。 在面板中,用戶選擇一個文件,在另一個面板中獲取有關所選文件的信息。
問題是CardLayout將所有面板加載在一起。 因此,面板處理預定義數據。 但我想用當前面板中給出的信息更新下一個面板。 每個面板都有“下一個”和“后退”按鈕,所以我認為這是下一個面板可以以某種方式更新的點。 我認為使用setter和getters方法但無法正確實現它。
以下是帶有兩個子面板的示例代碼:BASE CLASS:
public Base(){
frame.setLayout(bl);
frame.setSize(800, 600);
frame.add(new MainPanel(), BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new Base();
}
}
MainPanel(子板持有人)
class MainPanel extends JPanel{
private CardLayout cl = new CardLayout();
private JPanel panelHolder = new JPanel(cl);
public MainPanel() {
ChooseFile chooseFile = new ChooseFile(this);
ShowResult showResult = new ShowResult(this);
panelHolder.add(showResult, "showResult");
panelHolder.add(chooseFile, "chooseFile");
cl.show(panelHolder, "chooseFile");
add(panelHolder);
}
public void showPanel(String panelIdentifier){
cl.show(panelHolder, panelIdentifier);
}
}
小組1:
class ChooseFile extends JPanel{
MainPanel ob2;
JPanel directoryChooserPanel, bottomPanel;
JButton btn, localSourceBack, localSourceNext;
JTextField field;
public ChooseFile(MainPanel mainPanel){
this.ob2 = mainPanel;
ShowResult showResult = new ShowResult();
setLayout(new BorderLayout());
directoryChooserPanel = new JPanel(new GridLayout(0,2));
btn = new JButton("Browse");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser("D:\\Desktop");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
File myFile = chooser.getSelectedFile();
String text = myFile + "";
field.setText(text);
}
}
});
directoryChooserPanel.add(btn);
field = new JTextField(20);
directoryChooserPanel.add(field);
localSourceNext = new JButton("Next");
localSourceNext.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
ob2.showPanel("showResult");
showResult.setRoot(getPath());
}
});
add(directoryChooserPanel, BorderLayout.NORTH);
add(localSourceNext, BorderLayout.EAST);
}
public String getPath(){
return field.getText();
}
}
小組2:
class ShowResult extends JPanel{
MainPanel ob2;
JPanel bottomPanel, labelsPanel;
JButton srcLocalBTN, srcFtpBTN, sourceLocationBack;
JLabel result;
File directory;
String root;
ArrayList<String> myFiles = new ArrayList<String>();
public ShowResult(MainPanel mainPanel){
this.ob2 = mainPanel;
setLayout(new BorderLayout());
result = new JLabel();
root = "No ADDRESS";
directory = new File(root);
listFiles(directory, myFiles);
String filesNumber = "It contains " + myFiles.size() + " files.";
result.setText(filesNumber);
add(result, BorderLayout.NORTH);
}
public void listFiles(File directory, ArrayList<String> list){
for(File file : directory.listFiles()){
list.add(file.getName());
if(file.isDirectory()){
listFiles(file.getAbsoluteFile(), list);
}
}
}
public ShowResult(){
}
public void setRoot(String chosenPath){
root = chosenPath;
}
}
所以它首先加載'子面板1',以便用戶通過jFileChooser
選擇一個目錄,然后我需要將這些數據傳輸到'子面板2'。 因此它可以計算它包含的文件數量。 我試圖通過獲取所選目錄並分配給第二個變量中的變量來傳輸數據。 但是不起作用。
任何想法?
您正在創建多個ShowResult對象,顯示一個但隨后將信息傳遞給另一個,即非顯示的對象,但這不是Java的工作方式(通過簡單地在此頁面中搜索new ShowResult()
數量,可以輕松發現這一點) new ShowResult()
匹配)。 您需要確保顯示的ShowResult對象與您傳遞信息的對象完全相同,這意味着您必須通過構造函數或方法參數將顯示的對象的引用傳遞到ChooseFile類。
更好的選擇:使用MVC設計模式,make,讓控件更改模型的狀態,並且視圖顯示模型的狀態。 這可能會降低代碼的圈復雜度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.