将输入从一帧传递到另一帧(JAVA) [英] Passing input from one frame to another(JAVA)

查看:51
本文介绍了将输入从一帧传递到另一帧(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事情是这样的...

我有 2 个 GUI 程序.
一个菜单程序,基本上是一个带有食物按钮的框架,点击时的按钮打开另一个程序,一个输入数量程序,它是一个带有文本字段、数字按钮、取消和确认按钮的框架.用户确认的数量将由输入数量程序的菜单程序访问,并存储在一个向量中,这样每次用户想要订购其他食品时,他只需单击另一个按钮并重复该过程.

I have 2 GUI programs.
A Menu Program,which is basically a frame with buttons of food items,the buttons when clicked opens this other program,an Input Quantity Program,which is a frame with a text field,buttons for numbers,buttons for Cancel and Confirm. The quantity that is confirmed by the user will be accessed by the menu program from the Input Quantity Program to be stored in a vector so that every time a user wants to order other food items he will just click another button and repeat the process.

现在我已经编写了大部分代码,除了一件事之外,一切都在工作,输入数量程序返回的值有这个延迟.

Now I've coded the most part and got everything working except one thing,the value returned by the Input Quantity Program has this delay thing.

这是我一步一步做的:

1)点击菜单中的食品项目,打开输入数量窗口.
2)我输入了我想要的数字,它在文本框中正确显示.
3)我按下确认,这将做 3 件事,首先它将文本字段的值存储到一个变量中,其次它将调用 dispose() 方法,第三个打印语句显示变量的值(用于测试目的).
4) 菜单程序然后检查用户是否已经按下了输入程序中的确认按钮,如果为真,它将调用输入程序中名为 getQuantity() 的方法,该方法返回变量数量"的值并将其存储在向量.
5)然后执行另一个print语句来检查传入的值是否正确,然后调用print()方法显示订购的商品名称和记录数量.

1)Click a food item in Menu,it opens the Input Quantity window.
2)I input the number I want,it displayed in the text box correctly.
3)I pressed confirm which will do 3 things,first it stores the value of the text field to a variable,second it will call the dispose() method and third a print statement showing the value of the variable(for testing purposes).
4)The menu program then checks if the user has already pressed the Confirm button in the Input program,if true it shall call a method in the Input program called getQuantity() which returns the value of the variable 'quantity' and store it in the vector.
5)After which executes another print statement to check if the passed value is correct and then calls the method print() to show the ordered item name and it's recorded quantity.

这是GUI的屏幕截图,代码将在其下方.

Here are the screenshots of the GUI and the code will be below it.


输入数量程序中CONFIRM BUTTON的ActionPerformed方法:

ActionPerformed method of the CONFIRM BUTTON in the Input Quantity Program:

private void ConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    confirmed = true;
    q= textField.getText().toString();
    quantity =Integer.parseInt(q) ;
    System.out.println("getQTY method inside Input Quantity Interface:" +getQuantity());
    System.out.println("Quantity from confirmButton in Input Quantity Interface actionPerformed: "+quantity);

    //getQuantity();
}                            

执行上述第 2 步的菜单程序中菜单项按钮的动作监听器类:

ACTION LISTENER CLASS of the MENU ITEM BUTTONS in MENU PROGRAM which does step 2 above:

class f implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) 
    {
         inputGUI.setVisible(true);
         int  q =0;

          q=inputGUI.getQuantity(); //call method to get value from Input Program

          System.out.println("Quantity inside Menu actionperformed from AskQuantity interface: "+q);

         orderedQuantity.add(q); //int vector
         textArea.append("\n"+e.getActionCommand()+"\t"+ q);
         orderedItems.add(e.getActionCommand());  //String vector
         print();
         /*
         System.out.println("Enter QTY: ");
         int qty = in.nextInt();
         orderedQuantity.add(qty);
         print();*/
   }

以下是控制台打印语句的截图:
在这里我第一次点了南瓜汤,我输入的数量是 1

Here are screenshots of the print statements in the console:
Here I first ordered Pumpkin Soup,I entered a quantity of 1

在这里我点了海鲜marinara并输入了2个

Here I ordered seafood marinara and entered a quantity of 2

这里我点了最后一项,煎三文鱼,输入的数量是 3

Here I ordered the last item,pan fried salmon and entered a quantity of 3

正如您所看到的,我订购的第一件商品的第一个记录数量为 0,然后当我添加另一件商品时,第一件商品的数量被记录,但第二件商品的数量没有被记录......第三件之后也是如此...即使程序终止,第三项的数量也不会被记录:(

As you can see the first recorded quantity is 0 for the first item I ordered then when I added another item,the quantity of the first item gets recorded but the 2nd item's quantity is not recorded..same goes after the third item... and the quantity of the 3rd item is not recorded even if the program terminates :(

我该如何解决这个问题?

How can I solve this problem?

推荐答案

我想我看到了你的问题,事实上它直接源于你没有使用模式对话框来获取你的输入.在用户有机会与之交互之前,您正在查询 inputGUI .等一下,我向你展示我的意思的一个小例子......

I think I see your problem, and in fact it stems directly from you're not using a modal dialog to get your input. You are querying the inputGUI before the user has had a chance to interact with it. Hang on while I show you a small example of what I mean...

编辑
这是我的示例代码,它有一个模态 JDialog 和一个 JFrame,它们都充当主 JFrame 的对话框,并且都使用相同的 JPanel 进行输入.不同之处在于,模态 JDialog 将在主 JFrame 的代码变为可见时冻结它,并且在它变为不可见之前不会恢复——因此代码将等待用户处理对话框之前它在进步,而一切都在其中.

Edit
Here's my example code that has a modal JDialog and a JFrame, both acting as a dialog to a main JFrame, and both using the very same JPanel for input. The difference being the modal JDialog will freeze the code of the main JFrame at the point that it has been made visible and won't resume until it has been made invisible -- thus the code will wait for the user to deal with the dialog before it progresses, and therein holds all the difference.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DialogExample {
   private static void createAndShowGui() {
      JFrame frame = new JFrame("Dialog Example");
      MainPanel mainPanel = new MainPanel(frame);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MainPanel extends JPanel {
   private InputPanel inputPanel = new InputPanel();
   private JTextField responseField = new JTextField(10);
   private JDialog inputDialog;
   private JFrame inputFrame;

   public MainPanel(final JFrame mainJFrame) {
      responseField.setEditable(false);
      responseField.setFocusable(false);

      add(responseField);
      add(new JButton(new AbstractAction("Open Input Modal Dialog") {

         @Override
         public void actionPerformed(ActionEvent e) {
            if (inputDialog == null) {
               inputDialog = new JDialog(mainJFrame, "Input Dialog", true);
            }
            inputDialog.getContentPane().add(inputPanel);
            inputDialog.pack();
            inputDialog.setLocationRelativeTo(mainJFrame);
            inputDialog.setVisible(true);  

            // all code is now suspended at this point until the dialog has been 
            // made invisible

            if (inputPanel.isConfirmed()) {
               responseField.setText(inputPanel.getInputFieldText());
               inputPanel.setConfirmed(false);
            }
         }
      }));
      add(new JButton(new AbstractAction("Open Input JFrame") {

         @Override
         public void actionPerformed(ActionEvent e) {
            if (inputFrame == null) {
               inputFrame = new JFrame("Input Frame");
            }

            inputFrame.getContentPane().add(inputPanel);
            inputFrame.pack();
            inputFrame.setLocationRelativeTo(mainJFrame);
            inputFrame.setVisible(true);  

            // all code continues whether or not the inputFrame has been
            // dealt with or not.

            if (inputPanel.isConfirmed()) {
               responseField.setText(inputPanel.getInputFieldText());
               inputPanel.setConfirmed(false);
            }

         }
      }));
   }
}

class InputPanel extends JPanel {
   private JTextField inputField = new JTextField(10);
   private JButton confirmBtn = new JButton("Confirm");
   private JButton cancelBtn = new JButton("Cancel");
   private boolean confirmed = false;

   public InputPanel() {
      add(inputField);
      add(confirmBtn);
      add(cancelBtn);

      confirmBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            confirmed = true;
            Window win = SwingUtilities.getWindowAncestor(InputPanel.this);
            win.setVisible(false);
         }
      });
      cancelBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            confirmed = false;
            Window win = SwingUtilities.getWindowAncestor(InputPanel.this);
            win.setVisible(false);
         }
      });
   }

   public boolean isConfirmed() {
      return confirmed;
   }

   public void setConfirmed(boolean confirmed) {
      this.confirmed = confirmed;
   }

   public String getInputFieldText() {
      return inputField.getText();
   }
}

所以解决方案:使用模态 JDialog.

So solution: use a modal JDialog.

这篇关于将输入从一帧传递到另一帧(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆