0

好的,所以我现在正在为大学做一个项目。基本上是一个纸牌游戏,在这种情况下是一个编辑器来创建你自己的套牌。当用户尝试保存卡片组时,程序会检查是否已存在具有该名称的文件。如果是这样,将打开一个通知。用户可以确认或取消(按下通知中的按钮将布尔值设置为真或假)

我现在需要一种暂停 saveDeck 方法的方法,直到用户按下通知窗口中的按钮。到目前为止,我在互联网上搜索到的所有内容要么与计时器有关(这对我的问题没有帮助),要么以我一点也不了解的方式进行了解释。

无论如何,这是我到目前为止的代码,关于我的问题:Deck Class:

public class Deck{
[...]
private String name = "";
private boolean overwrite = false;
[...]
public void saveDeck(){
    if (this.name.equals("")){
        ErrorWindow error = new ErrorWindow(3); 
        return;
    }else{
    }
    String deckpath = "Decks\\" + this.name +".txt";
    File deck = new File(deckpath);
    if (deck.exists() == true){
        Notification note = new Notification(1, this);
            if (overwrite == true){
            try{
                deck.createNewFile(); //recreates the file
                writeDeckToFile(deck); //writes the decks cardlist into the file
                updateDecklist(this.name); //updates the decklist
            }catch (IOException e){
                ErrorWindow error = new ErrorWindow(2);
            }
        }else{
            return;  
        }
    }
}

通知类:

public class Notification extends JFrame {
[...]
public Notification(int key, Deck deck) {
[...]
JButton okayButton = new JButton("Okay");
    okayButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
            deck.setOverwrite(true); //sets the decks overwrite boolean true    
        }
    });
    okayButton.setBounds(10, 86, 144, 23);
    contentPane.add(okayButton);

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
            deck.setOverwrite(false); //sets the decks overwrite boolean false
        }
    });

Sooo...是的...如果有人可以在这里给我一些建议会很好。

4

0 回答 0