2016-11-02 156 views
-1

我有我的原始文本字段在我的第一幀,我需要它顯示在我的其他jframe。該代碼是:從一個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」是用戶在輸入自己的名字文本框中

此代碼描述它應顯示:

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); 

這是我的第一課堂,用戶輸入他們的名字

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); 

} 
    } 

我的第二課,其中的文本字段必須設置

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); 


    } 
    } 
+2

你的問題確切的是什麼? – Berger

+0

用戶在文本字段中輸入他們的名字,它將它們帶到一個訂單屏幕,當他們去「收據」jframe時,他們應該有他們的名字出現,我無法找到一種方法來轉移它,嘗試了很多方法,沒有什麼作品。 @Berger –

+0

看來你的代碼已經在做這件事了,什麼都行不通?也許增加整個類,以便我們可以更好地看到問題。 – Berger

回答

1

打開這兩個JFrames,讓他們監聽EventQueue以獲得自定義「字符串顯示」事件。

將字符串換行顯示在自定義事件中。

在事件隊列中拋出它,允許兩個JFrames接收事件,然後他們將相應顯示。

----與更新編輯----

好了,你是一個有點新的擺動,但是希望不是太新到Java。您需要了解子分類和「聽衆」設計模式。

事件是組件傾聽的事情。他們要求調度員(Swing調度員由EventQueue提供)「告訴他們關於事件」,然後調度員將所需事件發送給他們。

在解決您的問題之前,您聽起來像需要熟悉Swing及其事件調度,所以read up on it here

+0

新手在這裏,我不知道如何寫出這個代碼我自己 –

+0

@ e.john增加了一點,以幫助你開始。這並不難,並且當您從Swing EventQueue之外的事件開始時,如果您願意,最終可以將它們發佈到EventQueue。 –

相關問題