Java - GUI:将值从帧传递到另一个 [英] Java - GUI: Passing values from frame to another

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

问题描述

我正在制作一个调用三个面板的面板项目.第一个面板包含所有信息,另外两个面板是当我按下按钮时打开的面板.它的工作原理是我调用第一个面板,按下一个按钮,第二个面板打开.然后在第二个面板中输入密码.如果正确,将打开第三个面板.我需要将第一个面板中的值调用到第三个面板中.我知道如何使用构造函数、访问器和修改器,但是我需要的值是在我按下按钮时在事件中生成的.我想弄清楚如何将这些值从事件中调用到第三个面板中.本质上,这些值是程序持续时间内所有交易的计数器、小计、税收和总计.这是我的代码.

I am making a panel project that calls three panels. Panel one holds all information and the other two panels are panels that open up when I press a button. How it works is I call the first panel, press a button, second panel opens. Then in the second panel I plug in a password. If it is correct, a third panel will open. I need to call values from the first panel into the third panel. I know how to use constructors, accessors and mutators, but the values I need are generated in an event when I press a button. I am trying to figure out how to call those values from the event into the third panel. Essentially, the values are counters, sub-total, tax, and total for all transactions of the duration of the program. Here is my code.

主类:

import javax.swing.*;

public class AppleRegister {

    public static void main(String[] args) 
    {
        double subTotalp2 = 0, taxP2 = 0, realTotalp2 = 0;

        JFrame frame = new JFrame("Apple Crazy Cola Cash (Apple IIC) Register");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        AppleRegisterPanel panel = new AppleRegisterPanel
                (subTotalp2, taxP2, realTotalp2);

        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }
}

第一面板:

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

public class AppleRegisterPanel extends JPanel
{
private JButton button1, button2, button3, button4, button5, button6;
private JLabel label1, label2, label3, label4, label5, label6, label7;
private JTextField text, text1, text2, text3, text4, text5, text6, text7, text8;
private JTextArea area ;
private JPanel panel, panel2, panel3, panel4;
private int counter, counter2, counter3, counter4, counterp21, counterp22, 
     counterp23, counterp24;
private String string;
private final double MD_TAX = 0.06;
private double subTotal, realTotal, tax, subTotalp2, realTotalp2, taxP2;
private double num100, num50, num20, num10, num5, num1, num25cents, num10cents,
      num5cents, num1cents;

public AppleRegisterPanel(double subTotalp2, double taxP2, double realTotalp2)
{
    this.subTotalp2 = subTotalp2;
    this.taxP2 = taxP2;
    this.realTotalp2 = realTotalp2;

    setLayout(new BorderLayout());
    text = new JTextField(10);
    text1 = new JTextField(5);
    text2 = new JTextField(5);
    text3 = new JTextField(5);
    text4 = new JTextField(5);
    text5 = new JTextField(10);
    text6 = new JTextField(10);
    text7 = new JTextField(10);
    text8 = new JTextField(10);
    area = new JTextArea();

    button1 = new JButton("Child");
    button2 = new JButton("Medium");
    button3 = new JButton("Large");
    button4 = new JButton("Ex Large");
    button5 = new JButton("Close Out Register");
    button6 = new JButton("Complete Transaction");

    panel = new JPanel();
    panel.setPreferredSize(new Dimension(240,250));
    panel.setBackground(Color.cyan);
    add(panel, BorderLayout.WEST);
    panel.add(button1);
    button1.addActionListener(new TempListener2());
    panel.add(text1);
    panel.add(Box.createRigidArea(new Dimension(0,20)));
    panel.add(button2);
    button2.addActionListener(new TempListener2());
    panel.add(text2);
    panel.add(Box.createRigidArea(new Dimension(0,20)));
    panel.add(button3);
    button3.addActionListener(new TempListener2());
    panel.add(text3);
    panel.add(Box.createRigidArea(new Dimension(0,20)));
    panel.add(button4);
    button4.addActionListener(new TempListener2());
    panel.add(text4);
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

    panel2 = new JPanel();
    label6 = new JLabel("Change");
    panel2.setPreferredSize(new Dimension(270,250));
    panel2.setBackground(Color.cyan);
    add(panel2, BorderLayout.EAST);
    panel2.add(button5);
    button5.addActionListener(new TempListener());
    panel2.add(label6);
    panel2.add(area);
    panel2.add(button6);
    button6.addActionListener(new TempListener1());
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));

    panel3 = new JPanel();
    label1 = new JLabel("Apple Crazy Cola Cash (Apple IIC) Register ");
    label7 = new JLabel(" By Robert Burns");
    panel3.setPreferredSize(new Dimension(50,50));
    panel3.setBackground(Color.cyan);
    add(panel3, BorderLayout.NORTH);
    panel3.add(label1);
    panel3.add(label7);

    panel4 = new JPanel();
    label1 = new JLabel("Sub-Total");
    label2 = new JLabel("Tax");
    label3 = new JLabel("Total");
    label4 = new JLabel("Payment");
    label5 = new JLabel("Change Owed");
    panel4.setPreferredSize(new Dimension(140,250));
    panel4.setBackground(Color.cyan);
    add(panel4, BorderLayout.CENTER);
    panel4.add(label1);
    panel4.add(text);
    panel4.add(label2);
    panel4.add(text5);
    panel4.add(label3);
    panel4.add(text6);
    panel4.add(label4);
    panel4.add(text7);
    text7.addActionListener(new TempListener3());
    panel4.add(label5);
    panel4.add(text8);

}

private class TempListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {

        if (event.getSource() == button5)
        {
            JFrame frame3 = new JFrame("Apple Crazy Cola Cash (Apple
                            IIC) Register");
            frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            AppleRegisterPanel3 panel = new AppleRegisterPanel3();
            frame3.getContentPane().add(panel);

            frame3.pack();
            frame3.setVisible(true);
        }
    }
}

private class TempListener1 implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {

        if (event.getSource() == button6)
        {
            counter = 0;
            text1.setText(Integer.toString(counter));
            counter2 = 0;
            text2.setText(Integer.toString(counter2));
            counter3 = 0;
            text3.setText(Integer.toString(counter3));
            counter4 = 0;
            text4.setText(Integer.toString(counter4));
            subTotal = 0;
            text.setText(Double.toString(subTotal));
            tax = 0;
            text5.setText(Double.toString(tax));
            realTotal = 0;
            text6.setText(Double.toString(realTotal));
            text7.setText("");
            text8.setText("");
        }
    }
}

private class TempListener2 implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        DecimalFormat df = new DecimalFormat("#.##");

        if (event.getSource() == button1)
        {
            counter++;
            string = text1.getText();
            text1.setText(Integer.toString(counter));

            subTotal += counter * 0.90;
            string = text.getText();
            text.setText(df.format(subTotal));

            tax += subTotal * MD_TAX;
            string = text5.getText();
            text5.setText(df.format(tax));

            realTotal += subTotal + tax;
            string = text6.getText();
            text6.setText(df.format(realTotal));

            counterp21++;
            subTotalp2 += counterp21 * 0.90;
            taxP2 += subTotalp2 * MD_TAX;
            realTotalp2 += subTotalp2 * taxP2;
        }

        if (event.getSource() == button2)
        {
            counter2++;
            string = text2.getText();
            text2.setText(Integer.toString(counter2));

            subTotal += counter2 * 1.40;
            string = text.getText();
            text.setText(df.format(subTotal));

            tax += subTotal * MD_TAX;
            string = text5.getText();
            text5.setText(df.format(tax));

            realTotal += subTotal + tax;
            string = text6.getText();
            text6.setText(df.format(realTotal));

            counterp22++;
            subTotalp2 += counterp22 * 1.40;
            taxP2 += subTotalp2 * MD_TAX;
            realTotalp2 += subTotalp2 * taxP2;
        }

        if (event.getSource() == button3)
        {
            counter3++;
            string = text3.getText();
            text3.setText(Integer.toString(counter3));

            subTotal += counter3 * 1.75;
            string = text.getText();
            text.setText(df.format(subTotal));

            tax += subTotal * MD_TAX;
            string = text5.getText();
            text5.setText(df.format(tax));

            realTotal += subTotal + tax;
            string = text6.getText();
            text6.setText(df.format(realTotal));

            counterp23++;
            subTotalp2 += counterp23 * 1.75;
            taxP2 += subTotalp2 * MD_TAX;
            realTotalp2 += subTotalp2 * taxP2;
        }

        if (event.getSource() == button4)
        {
            counter4++;
            string = text4.getText();
            text4.setText(Integer.toString(counter4));

            subTotal += counter4 * 2.00;
            string = text.getText();
            text.setText(df.format(subTotal));

            tax += subTotal * MD_TAX;
            string = text5.getText();
            text5.setText(df.format(tax));

            realTotal += subTotal + tax;
            string = text6.getText();
            text6.setText(df.format(realTotal));

            counterp24++;
            subTotalp2 += counterp24 * 2.00;
            taxP2 += subTotalp2 * MD_TAX;
            realTotalp2 += subTotalp2 * taxP2;
        }
    }
}

如果您向下滚动到 TempListner2 并看到变量 subTotalp2、taxP2、realTotalp2;这些是我需要继承的变量.你会看到我让构造函数包含这些变量,所以我可以在第三个面板中调用方法,但没有骰子.当我打开第三个面板时出现 0.0.不确定是不是因为它处于无效事件中.我将在这里发布第三个面板的一部分,我试图从第一个面板调用构造函数并将值插入到第三个面板中.

If you scroll down to TempListner2 and see the variables subTotalp2, taxP2, realTotalp2; those are the variables I need to carry over. You'll see that I made the constructor have those variables in it so I can call the methods over in the third panel, but no dice. Comes up 0.0 when I open the third panel. Not sure if it is because it is in a void event or not. I'll post part of the third panel here where I am trying to call the constructor from the first panel and inset the values into the third.

第三面板:

public AppleRegisterPanel2()
{

    AppleRegisterPanel p = new AppleRegisterPanel(subTotalp2, taxP2, 
            realTotalp2);
    this.subTotalp2 = subTotalp2;
    this.taxP2 = taxP2;
    this.realTotalp2 = realTotalp2;

    subTotalp2 = p.getSubTotal();
    taxP2 = p.getTax();
    realTotalp2 = p.getTotal();

你会在这里看到我正在尝试一种奇怪的方法来调用值.

You'll see here that I am trying a weird ass way to call the values.

推荐答案

一个问题是您从 JFrame 打开另一个 JFrame,而您应该显示一个模态 JDialog.这样做的原因是在用户输入适当的信息之前你不能推进你的程序,所以第二个窗口应该是模态的,这将阻止与底层窗口的交互,直到完全处理对话窗口.如果您这样做,并将您的 JPanel 放入模态 JDialog 中,那么您可以轻松地从第二个 JPanel 查询结果,因为您将在代码中确切地知道它何时被处理——您的主 GUI 代码完全从您设置的位置恢复模态对话框可见.

One problem is that you're opening another JFrame from a JFrame, when you should instead be displaying a modal JDialog. The reason for this is that you cannot advance your program until the user enters the appropriate information, and so the 2nd window should be modal which will prevent interaction with the underlying window until the dialog window has been completely dealt with. If you do this, and place your JPanel into a modal JDialog, then you can easily query the results from the 2nd JPanel because you will know in your code exactly when it's been dealt with -- your main GUI code resumes exactly from where you set the modal dialog visible.

例如:

import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class WindowCommunication {

   private static void createAndShowUI() {
      JFrame frame = new JFrame("WindowCommunication");
      frame.getContentPane().add(new MyFramePanel());
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   // let's be sure to start Swing on the Swing event thread
   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

class MyFramePanel extends JPanel {
   private JTextField field = new JTextField(10);
   private JButton openDialogeBtn = new JButton("Open Dialog");

   // here my main gui has a reference to the JDialog and to the
   // MyDialogPanel which is displayed in the JDialog
   private MyDialogPanel dialogPanel = new MyDialogPanel();
   private JDialog dialog;

   public MyFramePanel() {
      openDialogeBtn.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            openTableAction();
         }
      });
      field.setEditable(false);
      field.setFocusable(false);

      add(field);
      add(openDialogeBtn);
   }

   private void openTableAction() {
      // lazy creation of the JDialog
      if (dialog == null) {
         Window win = SwingUtilities.getWindowAncestor(this);
         if (win != null) {
            dialog = new JDialog(win, "My Dialog",
                     ModalityType.APPLICATION_MODAL);
            dialog.getContentPane().add(dialogPanel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
         }
      }
      dialog.setVisible(true); // here the modal dialog takes over

      // this line starts *after* the modal dialog has been disposed
      // **** here's the key where I get the String from JTextField in the GUI held
      // by the JDialog and put it into this GUI's JTextField.
      field.setText(dialogPanel.getFieldText());
   }
}

class MyDialogPanel extends JPanel {
   private JTextField field = new JTextField(10);
   private JButton okButton = new JButton("OK");

   public MyDialogPanel() {
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            okButtonAction();
         }
      });
      add(field);
      add(okButton);
   }

   // to allow outside classes to get the text held by the JTextField
   public String getFieldText() {
      return field.getText();
   }

   // This button's action is simply to dispose of the JDialog.
   private void okButtonAction() {
      // win is here the JDialog that holds this JPanel, but it could be a JFrame or 
      // any other top-level container that is holding this JPanel
      Window win = SwingUtilities.getWindowAncestor(this);
      if (win != null) {
         win.dispose();
      }
   }
}

这篇关于Java - GUI:将值从帧传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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