英文:
Can I use a do-while loop with Vaadin? If I do the localhost:8080 shows a loading bar that almost fills but never does
问题
一个学校项目需要我们制作一个用于各种转换的Web应用程序。目前,我正在使用localhost:8080进行测试,同时构建应用程序。说明要求在初始菜单中使用do-while循环。我已经实现了逻辑,直到第一个温度转换(从华氏度到摄氏度),并且在不添加do-while循环时一切都正常工作。
到目前为止,我已尝试将整个代码包装在do-while循环中,以及仅将菜单放在do-while循环中(我还没有尝试每种可能的do-while循环位置)。在这两种情况下,页面都显示一个加载进度条,几乎填满,开始闪烁,然后一直停在那里,永远加载。我肯定是漏掉了什么简单的东西?
英文:
A project for school requires us to make a Web Application for various conversions. For the time being I am using localhost:8080 to test as I build. The instructions require a do-while loop for the initial menu. I have the logic up to the first temperature conversion (F to C) and it works just fine until I add a do-while.
So far I've tried wrapping the entire code in the do-while and just the menu in a do-while (I haven't tried every single possibility of do-while placement yet). In both cases the page shows a loading bar that almost fills, starts flashing, and stays there, forever loading. Surely I'm missing something simple?
add(new H1("Welcome to the Conversion Challenge!"));
IntegerField menuChoice = new IntegerField();
Button chooseConversionButton = new Button("Onward!");
AtomicInteger inputValue = new AtomicInteger(-1);
do
{
add(new H3("What method of conversion would you like to indulge in?"));
add(new Paragraph("1) A temperate one?"));
add(new Paragraph("2) Something heavier?"));
add(new Paragraph("3) Maybe you're money-minded?"));
add(new Paragraph("4) Or just give up!"));
menuChoice.setLabel("What is your choice?");
menuChoice.setHelperText("Don't be silly... go ahead.");
add(menuChoice, chooseConversionButton);
chooseConversionButton.addClickListener(eventChooseConversion -> {
inputValue.set(menuChoice.getValue());
removeAll();
switch(inputValue.get())
{
... code continues
} while(inputValue.intValue() != 4);
答案1
得分: 1
我认为你对Vaadin的概念有所误解。你的Java代码在服务器上运行,UI显示在浏览器上。这意味着有两台机器之间的网络连接。也就是说,这是一个分布式系统。因此,在Vaadin应用程序中,一切都基于事件和事件处理。
chooseConversionButton.addClickListener(eventChooseConversion -> { .. };
执行不会在此处停止等待输入。主线程的执行会继续。相反,在事件发生时会执行监听器中的回调函数。在任何类似Vaadin的分布式系统中都不可能进行阻塞操作。
因此,在你的代码中,do-while循环实际上是一个几乎永恒的循环。根据你的注释,在这里完全不必要。
所以如果你想显示一个菜单并接收输入,并且在接收到输入时有“某事”需要做,你需要在事件处理程序中调用那个“某事”。
我建议浏览Vaadin文档中的教程部分,并下载应用启动器来尝试这个概念。
英文:
I think you have misunderstood the concept of Vaadin. You Java code runs on the server and the UI is shown on the Browser. This means that there is two machines and a network connection between them. I.e. it is a distributed system. Thus in Vaadin application, everything is based on events and event handling.
chooseConversionButton.addClickListener(eventChooseConversion -> { .. };
The execution is not stopping here waiting for input. The main thread execution continues. Instead the callback in the listener is executed when the event happens. The blocking actions are not possible in any distributed system such as Vaadin.
Thus in your code do - while loop is practically eternal loop. Based on your comment it is completely un-necessary here.
So if you want to present a menu and take input, and have "something" that needs to be done when input is taken, you need to call that "something" in the event handler.
I recommend to browse thru the Tutorial section of the Vaadin documentation and download app starter to play with the concept.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论