第二种情况下的对话框为空

问题描述 投票:0回答:0

我有以下代码,问题出在最后一个开关上,在第一种情况下,我创建了对话框,我在屏幕上看到了它,到目前为止一切都很好。在对话框中有一个名为“btnAddWord2”的按钮对应于开关的第二种情况,我按下它并识别它,问题是当我尝试在这种情况下引用“对话框”时,它需要它为空,这可能是一个可能的解决方案?

public class App implements ActionListener{
    
    private Frame frame;
    private Dialog dialog;
    private JsonFileManager file;
    private Dictionary dictionary;
    
    public App() {
        
        dialog = null;
        frame = new Frame(this);
        
    }

    public static void main(String[] args) {
        
        new App();
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        dictionary = new Dictionary();
        int op = 0;
        
        if(frame.getRbtEng().isSelected() == true) {
            
            op = 1;
            
            try {
                
                file = new JsonFileManager(op);
                
            } catch (IOException e1) {
                
                e1.printStackTrace();
            }
            
            dictionary.setDictionary(file.getDictionary().getDictionary());
            
        }else if(frame.getRbtFra().isSelected() == true) {
            
            op = 2;
            try {
                
                file = new JsonFileManager(op);
                
            } catch (IOException e1) {
                
                e1.printStackTrace();
            }
            
            dictionary.setDictionary(file.getDictionary().getDictionary());
            
        }
        
        if(e.getActionCommand().equals("btnTranslate")) {
            
            int pos = dictionary.searchWord(frame.getTextFieldEsp().getText());
            String translation = dictionary.getDictionary().get(pos).getTranslation();
            frame.getTextFieldTrans().setText(translation);
            
        }
        
        switch(e.getActionCommand()) {
        
        case "btnAddWord":
            
            dialog = new Dialog(this);
            dialog.setVisible(true);
            
            break;
            
        case "btnAddWord2":
            
            if (dialog != null && !dialog.isVisible()) {
                
                System.out.println("Enter");
                Definition definition = new Definition(dialog.getTxtField1().getText(), dialog.getTxtField2().getText());
                dictionary.getDictionary().add(definition);
                dialog.dispose();
            }
          
            break;
            
        default:
            break;
        }
            
    }
}

首先,我尝试在该案例中创建一个条件,以便它检查对话框是否为空,确实是因为它从未执行过 if 中的代码。 我唯一希望的是对话框不再为空,根据代码,信息设置在对话框的文本字段中

java user-interface null dialog switch-statement
© www.soinside.com 2019 - 2024. All rights reserved.