简体   繁体   English

我如何获得从第一帧文本字段输入文本到第二帧文本的输出

[英]How I can get output from 1st frame textfield input text to 2nd frame textArea

Here is my 1st frame - I want went I input text in textfield example name then click button report will display output to 2nd frame using textArea... please help me 这是我的第一帧-我想在文本字段示例名称中输入文本,然后单击按钮报告将使用textArea将输出显示到第二帧...请帮助我

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


public class Order extends JFrame implements ActionListener
{

    private JPanel pInfo,pN, pIC, pDate,Blank,pBlank, button, pTotal;

    private JLabel nameL,icL,DateL;

    private JTextField nameTF, icTF;

    private JFormattedTextField DateTF;

    private JButton calB,clearB,exitB,reportB;


    public Order()
    {
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.setBackground(Color.gray);


          pInfo             = new JPanel();     

          pN                = new JPanel();
          pIC               = new JPanel();
          pDate             = new JPanel();


            nameTF      = new JTextField(30);
            icTF        = new JTextField(30);    
            DateTF      = new JFormattedTextField(
                                   java.util.Calendar.getInstance().getTime());
            DateTF.setEditable (false);
            DateTF.addActionListener(this);


        nameL      = new JLabel(" NAME : ",SwingConstants.RIGHT);
        icL        = new JLabel(" IC   :   ",SwingConstants.RIGHT);
        DateL      = new JLabel(" DATE :",SwingConstants.RIGHT);


        pInfo.setLayout(new GridLayout(10,2,5,5));

        pInfo.setBorder(BorderFactory.createTitledBorder
        (BorderFactory.createEtchedBorder(),"ORDER"));


        pN.add(nameL); 
        pN.add(nameTF);

        pIC.add(icL); 
        pIC.add(icTF);

        pDate.add(DateL);
        pDate.add(DateTF);


      pInfo.add(pN);
        pInfo.add(pIC);
        pInfo.add(pDate);


        pInfo.setBackground(Color.GRAY);

        pN.setBackground(Color.gray);    
        pIC.setBackground(Color.gray);
        pDate.setBackground(Color.gray);

        nameL.setForeground(Color.black); 
        icL.setForeground(Color.black); 
        DateL.setForeground(Color.black); 

        nameTF.setBackground(Color.pink);
        icTF.setBackground(Color.pink);
        DateTF.setBackground(Color.pink);


          contentPane.add(pInfo,BorderLayout.CENTER);



        Blank   = new JPanel();
        pBlank  = new JPanel();
        button  = new JPanel();

        calB = new JButton("CALCULATE");
        calB.setToolTipText("Click to calculate");

        clearB  = new JButton("RESET");
        clearB.setToolTipText("Click to clear");

            reportB = new JButton ("REPORT");
            reportB.setToolTipText ("Click to print");

        exitB   = new JButton("EXIT");
        exitB.setToolTipText("Click to exit");

        Blank.setLayout(new GridLayout(2,2));

        Blank.setBorder(BorderFactory.createTitledBorder
        (BorderFactory.createEtchedBorder(),""));

            button.setLayout(new GridLayout(1,4));

            button.add(calB,BorderLayout.WEST);
            button.add(clearB,BorderLayout.CENTER);
            button.add(reportB,BorderLayout.CENTER);
            button.add(exitB,BorderLayout.EAST);

            Blank.add(pBlank); 
            Blank.add(button);   

      contentPane.add(Blank,BorderLayout.SOUTH);    


        Blank.setBackground(Color.gray);
        pBlank.setBackground(Color.gray);

        calB.setForeground(Color.black);
        clearB.setForeground(Color.black);
        reportB.setForeground(Color.black);
        exitB.setForeground(Color.black);
        calB.setBackground(Color.pink);
        clearB.setBackground(Color.pink);
        reportB.setBackground(Color.pink);        
        exitB.setBackground(Color.pink);          


            calB.addActionListener(this);
            clearB.addActionListener(this);
            reportB.addActionListener(this);
            exitB.addActionListener(this);

        }


        public void actionPerformed(ActionEvent p)
        {

        if (p.getSource() == calB)
        {
        }   

    else if (p.getSource() == clearB)
    {
    }

    else if (p.getSource () == reportB)
    {

   }

    else if (p.getSource() == exitB)
        {
        }
}

    public static void main (String [] args)
        {

        Order frame = new Order();
        frame.setTitle("Order");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);//center the frame
    }
}

Simply add some reference to the first frame in the second or pass the value you're interested in to the second frame before you display it. 只需在第二帧中添加对第一帧的引用,或在显示第二帧之前将您感兴趣的值传递给第二帧。

As for the code example you requested: 至于您要求的代码示例:

public class SecondFrame extends JFrame {
    private JFrame firstFrame;

    public SecondFrame(JFrame firstFrame) {
        this.firstFrame = firstFrame;
    }
}

Now you can obtain everything there is to obtained from the firstFrame through the internal reference to it. 现在,您可以通过对其的内部引用来获取从firstFrame获得的所有内容。

If you only have one String to pass, add it to the constructor of your second JFrame : 如果只有一个String要传递,请将其添加到第二个JFrame的构造函数中:

public class SecondFrame extends JFrame {
    public SecondFrame(String someValueFromFirstFrame) {
        someTextField.setText(someValueFromFirstFrame);
    }
}

and pass it when creating the second JFrame : 并在创建第二个JFrame时传递它:

SecondFrame secondFrame = new SecondFrame(firstTextField.getText());

If there is more than one attribute to pass, consider putting them together in another class and pass the instance of this class. 如果要传递多个属性,请考虑将它们放在一起放在另一个类中,然后传递该类的实例。 This saves you from changing the constructor every time you need to pass an additional variable. 这使您不必在每次需要传递附加变量时都更改构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个框架到第二个框架在文本字段中获取输入的文本? - How can i get the inputted text in a textfield from another frame to the 2nd frame? 如果我将提供第一栏的文字,我该如何获取第二栏的文字 - How can i get text of 2nd column if i will provide the text of 1st column 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 在Java swing中如何在当前窗口(第一帧)中单击按钮时如何打开新窗口(第二帧) - how to open a new window(2nd frame) when a button is clicked in present window(1st frame) in java swing 如何将代码从第二类添加到第一类 - How to add code from 2nd class to 1st class 如何在Java Swing中从第一个面板获取价值并插入第二个面板 - How to get value from 1st panel and insert in 2nd panel in Java Swing 如何将数组值添加到第二个组合框模型项中,从第一个组合框所选项目中获取数组名称? - How can I add array value into 2nd Combo Box model item getting the array name from 1st Combo Box selected item? 比较两个文本文件,看看第二个文件中的单词在第一个文件中出现了多少次 - Compare two text files and see how many times the words in the 2nd file occurs in the 1st file 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 我从第一个活动中获取数据。 在第二个活动中,我尝试输入一个值,以便y = beta0 + beta1 * x。 但是当我输入x时,结果y为0 - i get the data from 1st activity. in this 2nd activity, i try to input a value, so that y=beta0 +beta1*x. but when i input x, the result y is 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM