将输入从一帧传递到另一帧(JAVA)

这是事情……

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

现在我编写了大部分内容并且除了一件事之外一切都正常工作,输入数量计划返回的值有这个延迟的事情。

这就是我一步一步做的事情:

1)单击“菜单”中的食物项,打开“输入数量”窗口。
2)我输入了我想要的数字,它正确地显示在文本框中。
3)我按下确认哪个会做3件事,首先它将文本字段的值存储到变量中,第二个将调用dispose()方法,第三个是显示变量值的print语句(用于测试目的)。
4)菜单程序然后检查用户是否已按下Input程序中的Confirm按钮,如果为true,它将调用Input程序中名为getQuantity()的方法,该方法返回变量’quantity’的值并将其存储在矢量。
5)之后执行另一个print语句以检查传递的值是否正确,然后调用方法print()以显示已订购的项目名称及其记录的数量。

以下是GUI的屏幕截图,代码将在其下方。

菜单GUI
第一顺序最后的订单

输入数量计划中CONFIRM BUTTON的ActionPerformed方法:

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:

 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();*/ } 

以下是控制台中print语句的屏幕截图:
我在这里首先点了南瓜汤,我输入的数量为1
第一顺序

在这里,我点了海鲜marinara,输入量为2
第二顺序

在这里,我订购了最后一项,煎三文鱼,并输入3的数量

在此处输入图像描述

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

我怎么解决这个问题?

我想我看到了你的问题,事实上它直接源于你没有使用modal dialog来获取你的输入。 您在用户有机会与之交互之前查询inputGUI。 在我向您展示我的意思的一个小例子的时候继续……

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

 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。

假设我有两个GUI帧f1和f2。 现在通过单击f1上的按钮,我想调用帧f2并且还将一些数据从f1(帧类)发送到f2(帧类)。
一种可能的方法是在f2中声明一个构造函数,它接受与我想从f1发送给它的参数相同的数据。现在在帧f1的编码中只包括这些语句:
f1.setVisible(false); // f1不可见f2 newFrame = new f2(uname,pass); // uname和pass取自f1的文本字段f2.setVisible(true);

我想这会解决你的问题。