将信息从一个Jframe传递到另一个Jframe

时间:2023-01-18 19:46:39

I have my original text field in my first frame and I need it to be displayed in my other jframe. The code is:

我的第一帧中有原始文本字段,我需要将它显示在我的其他jframe中。代码是:

JButton btnContinue = new JButton("Continue");
    btnContinue.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String msg = nameL.getText();

            frame2 fram = new frame2 ();
            fram.setVisible(true);
            frame.dispose();

            new order (msg) .setVisible(true);

'nameL' is the textbox the user enters their name in.

'nameL'是用户输入其姓名的文本框。

This code describe where it should be displayed:

此代码描述了它应显示的位置:

public order(String para){
        getComponents();


    JLabel lblNewLabel_1 = new JLabel("");
    lblNewLabel_1.setBounds(91, 27, 254, 84);
    contentPane.add(lblNewLabel_1);
    lblNewLabel_1.setText(para);

this is my first class where the user inputs their name

这是我用户输入姓名的第一堂课

public order(String para){
        getComponents();


    import java.awt.EventQueue;
  import javax.swing.JFrame;
 import javax.swing.JButton;
 import javax.swing.JTextField;
 import javax.swing.JTextArea;
 import java.awt.Color;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import java.awt.Font;

 public class frame1 {

public JFrame frame;
public JTextField nameL;
public JTextField textField_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame1 window = new frame1();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public frame1() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setEnabled(false);
    frame.setResizable(false);
    frame.getContentPane().setBackground(Color.GRAY);
    frame.setForeground(Color.WHITE);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnContinue = new JButton("Continue");
    btnContinue.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String msg = nameL.getText();

            frame2 fram = new frame2 ();
            fram.setVisible(true);
            frame.dispose();

            new order (msg) .setVisible(true);

        }
        }

    );
    btnContinue.setBounds(0, 249, 450, 29);
    frame.getContentPane().add(btnContinue);

    JTextArea txtrPleaseEnterYour = new JTextArea();
    txtrPleaseEnterYour.setEditable(false);
    txtrPleaseEnterYour.setBackground(Color.LIGHT_GRAY);
    txtrPleaseEnterYour.setBounds(0, 0, 450, 32);
    txtrPleaseEnterYour.setText("\tPlease enter your name and email below\n If you do not have or want to provide an email, Leave the space blank");
    frame.getContentPane().add(txtrPleaseEnterYour);

    JTextArea txtrEnterYourFull = new JTextArea();
    txtrEnterYourFull.setEditable(false);
    txtrEnterYourFull.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
    txtrEnterYourFull.setBackground(Color.GRAY);
    txtrEnterYourFull.setText("Enter your full name");
    txtrEnterYourFull.setBounds(52, 58, 166, 29);
    frame.getContentPane().add(txtrEnterYourFull);



    nameL = new JTextField();
    nameL.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    }
    );
    nameL.setBackground(Color.LIGHT_GRAY);
    nameL.setBounds(52, 93, 284, 26);
    frame.getContentPane().add(nameL);
    nameL.setColumns(10);


    JTextArea txtroptionalEnterYour = new JTextArea();
    txtroptionalEnterYour.setEditable(false);
    txtroptionalEnterYour.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
    txtroptionalEnterYour.setBackground(Color.GRAY);
    txtroptionalEnterYour.setText("(Optional) Enter your email");
    txtroptionalEnterYour.setBounds(52, 139, 193, 29);
    frame.getContentPane().add(txtroptionalEnterYour);

    textField_1 = new JTextField();
    textField_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    textField_1.setBackground(Color.LIGHT_GRAY);
    textField_1.setBounds(52, 180, 284, 26);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

 }
  }

my second class where the text field has to be set

我的第二课,必须设置文本字段

import java.awt.EventQueue;

 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.border.EmptyBorder;
 import java.awt.Color;
 import javax.swing.JButton;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import javax.swing.JLabel;

 public class order extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
public JPanel contentPane;
/**
 * Launch the application.
 */

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                order frame = new order();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public order() {
    setAlwaysOnTop(false);
    setTitle("Order Details // Finalization");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 451, 523);
    contentPane = new JPanel();
    contentPane.setBackground(Color.LIGHT_GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnNewButton = new JButton("Submit Order");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setAlwaysOnTop(true);
            JOptionPane.showMessageDialog(null, "Your Order Has Been Confirmed", "enjoy your meal", JOptionPane.YES_NO_OPTION);

        }
    });
    btnNewButton.setBounds(164, 466, 117, 29);
    contentPane.add(btnNewButton);

}

    public order(String para){
        getComponents();    

    JLabel lblNewLabel_1 = new JLabel("");
    lblNewLabel_1.setBounds(91, 27, 254, 84);
    contentPane.add(lblNewLabel_1);
    lblNewLabel_1.setText(para);


    }
    }

1 个解决方案

#1


1  

Open both JFrames, have them listen to the EventQueue for a custom "string display" event.

打开两个JFrame,让他们听EventQueue进行自定义“字符串显示”事件。

Wrap the string to be displayed in a custom event.

包装要在自定义事件中显示的字符串。

Throw that on the event queue, allowing both the JFrames to receive the event, which they will then display accordingly.

将其抛出到事件队列中,允许两个JFrame接收事件,然后它们将相应地显示。

---- Edited with an update ----

----编辑更新----

Ok, so you're a bit new to Swing, but hopefully not too new to Java. You'll need to understand sub-classing and the "Listener" design pattern.

好吧,所以你对Swing有点新意,但希望对Java不太新。您需要了解子类和“监听器”设计模式。

Events are things that Components listen for. They ask the dispatcher (the Swing dispatcher is fed by the EventQueue) to "tell them about events" and the dispatcher then sends the desired events to them.

事件是组件侦听的事物。他们要求调度员(Swing调度员由EventQueue提供)“告诉他们有关事件”,然后调度员将所需事件发送给他们。

Before you get too deep in solving your problem, it sounds like you need to get some familiarity with Swing and its event dispatch, so read up on it here.

在你深入解决问题之前,听起来你需要熟悉Swing及其事件发送,所以请在这里阅读。

#1


1  

Open both JFrames, have them listen to the EventQueue for a custom "string display" event.

打开两个JFrame,让他们听EventQueue进行自定义“字符串显示”事件。

Wrap the string to be displayed in a custom event.

包装要在自定义事件中显示的字符串。

Throw that on the event queue, allowing both the JFrames to receive the event, which they will then display accordingly.

将其抛出到事件队列中,允许两个JFrame接收事件,然后它们将相应地显示。

---- Edited with an update ----

----编辑更新----

Ok, so you're a bit new to Swing, but hopefully not too new to Java. You'll need to understand sub-classing and the "Listener" design pattern.

好吧,所以你对Swing有点新意,但希望对Java不太新。您需要了解子类和“监听器”设计模式。

Events are things that Components listen for. They ask the dispatcher (the Swing dispatcher is fed by the EventQueue) to "tell them about events" and the dispatcher then sends the desired events to them.

事件是组件侦听的事物。他们要求调度员(Swing调度员由EventQueue提供)“告诉他们有关事件”,然后调度员将所需事件发送给他们。

Before you get too deep in solving your problem, it sounds like you need to get some familiarity with Swing and its event dispatch, so read up on it here.

在你深入解决问题之前,听起来你需要熟悉Swing及其事件发送,所以请在这里阅读。