1

我创建了两个 JFrame。主 JFrame 包含文本区域。我的子 JFrame 包含一个下拉列表。任务是传递我在下拉列表中选择的值并显示在主 JFrame 的文本区域中。

子 JFrame 中的代码:

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
    close();      
    room=cmbRoom.getSelectedItem().toString();
}

主 JFrame 中的代码:

private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {
    roomNo r=new roomNo();
    txtArea2.append("\nRoom Number: " + r.getroom());
}                                           
4

3 回答 3

0
class NextPage extends JFrame
{
  NextPage(String st)
  {
     setLayout(null);
     setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE);
     setTitle("Welcome");
     JLabel lab=new JLabel("Welcome  "+st);
     lab.setBounds(10,10,500,20);
     add(lab);
     setSize(300, 100);
  }
}
于 2013-05-04T20:58:56.393 回答
0

这可能不是完全正确的答案,但这将完成这项工作。

假设您有 2 个 Jframe,即 Home.java 和 Second.java

Second.java 的代码如下,

public static String selection = "";//static variable to store seletced value from combobox
Home h = new Home();//instance of Home Jframe

/**
* return selected value (called from Home Jframe)
*/
public static String getSeletced() {
    return selection;
}

/**
* get selected value from comboBox event
*/
private void cmbLapActionPerformed(java.awt.event.ActionEvent evt) {                                       
    selection = cmbLap.getSelectedItem().toString();
    h.isSelected = true;//this is to control data duplication
}  

现在对于 Home.java 文件,我们可以使用 formWindowGainedFocus事件来更新jTextArea. Home .java 文件包含以下代码,

public static boolean isSelected = false;//flag to check combo box is selected

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {                                       
    System.out.println(isSelected);
    if (isSelected) {
        String text = new Second().getSeletced();
        System.out.println(text);
        txaData.append("Your Laptop: " + text + "\n");//appending data
        isSelected = false;//to prevent duplication
    }
}  

此方法可用于使用来自另一个 jFrame 的数据更新 jTextArea。

于 2018-03-03T16:32:33.417 回答
-1
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class PassData extends JFrame
{
    JTextField text;
    PassData(){
    JLabel l=new JLabel("Name: ");
    text=new JTextField(20);
    JButton b=new JButton("Send");
    setLayout(null);
    l.setBounds(10,10,100,20);
    text.setBounds(120,10,150,20);
    b.setBounds(120,40,80,20);
    add(l);
    add(text);
    add(b);
    setVisible(true);
    setSize(300,100);
      b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        String value=text.getText();
        NextPage page=new NextPage(value);
        page.setVisible(true);
        }
    });
   }
    public static void main(String[] args) 
  {
    new PassData();
  }
}
于 2013-05-04T20:52:29.943 回答