如何从第一帧文本字段输入文本输出到第二帧textArea

时间:2010-04-24 08:56:20

标签: java user-interface swing

这是我的第一帧 - 我想要在textfield示例名称中输入文本然后单击按钮报告将使用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
    }
}

2 个答案:

答案 0 :(得分:2)

只需在第二帧中添加对第一帧的引用,或者在显示之前将您感兴趣的值传递给第二帧。

至于你要求的代码示例:

public class SecondFrame extends JFrame {
    private JFrame firstFrame;

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

现在,您可以通过内部引用获取从firstFrame获取的所有内容。

答案 1 :(得分:2)

如果您只有一个String要传递,请将其添加到第二个JFrame的构造函数中:

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

并在创建第二个JFrame时传递它:

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

如果要传递多个属性,请考虑将它们放在另一个类中并传递此类的实例。这样可以避免在每次需要传递其他变量时更改构造函数。