gpt4 book ai didi

java - 修复 JOptionsPane 循环

转载 作者:行者123 更新时间:2023-11-30 07:40:49 25 4
gpt4 key购买 nike

我正在尝试修复程序中的循环,但遇到了一些严重的问题。更准确地说,我正在尝试添加一个循环。该程序非常复杂,因此我将尽力解释。让我从问题区域开始:

public static void displayGUI(){//Method to display the GUI. 


//Creates a JOptionPane for the first GUI featuring 7 buttons and 2 lists..
int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null, new String[]{"Confirm","Create Return"}, "default");

if(result == 1){//If the user selects clicks the "Create Return" button..

initialScreenDecisions="NONE";//The user did not choose to add any entry details to the output list.
MainWriter.finishedCounter=true;//The boolean counter to trigger that the return is finished goes to true.
while(MainWriter.entryDetails.size()>0){//Removes all entry details from the input list.
MainWriter.entryDetails.remove(0);
}

while(output.size()>0){//Removes all entry details from the output list..
output.remove(0);
}

}

}

上面的类创建了一个带有两个列表的 GUI,其中的内容可以使用箭头按钮以及两个标题为“Confirm”和“Create Return”的标准 JOptionPane 按钮来回传输。当用户单击“创建返回”方法时,程序基本上结束并为他们生成一个文件。如果他们不单击“创建返回”,程序将照常继续,并出现其他对话框。我遇到的问题是,我被要求做到这一点,以便“创建返回”按钮不起作用,除非它们已达到程序中的某个点。 (该程序通过上述方法进行多次循环,具体取决于输入文件,因为它是缓冲的读取器/写入器。)

所以我真正需要的是简单地根据变量来确定,如果他们选择结果 1,则会出现错误消息,并且程序将不会前进。问题是,即使我在这个阶段向程序中添加一个带有错误消息的条件,程序仍然会前进。 JOptionsPane 上的“取消”按钮将关闭该 Pane ,我的程序将照常继续。

如何让该选项显示一条消息,但在程序中不再继续?将其视为具有 Activity 监听器的 JButton,而不是 JOptionsPane 按钮。

另一个按钮(“确认”)作为 JOptionsButton 工作得很好,因为它按照预期推进程序。此外,“创建返回”按钮在满足变量检查的情况下工作正常。它改变了一些变量并且程序照常继续。我只需要这个实例,其中按钮不会使程序向前推进,并且用户可以选择另一个按钮。

最佳答案

您可以传递一个按钮数组,每个按钮都有自己的 ActionListener,而不是将字符串数组传递给 showOptionDialog():

final JButton button = new JButton("Something");
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(final ActionEvent ae) {
// do something
}
});

final Object[] options = new Object[] { button };

int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, "default");

关于java - 修复 JOptionsPane 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34727000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com