本文介绍了将信息从一个 jframe 传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道使用多个 jframe 是不受欢迎的,不幸的是,我让自己深入这个项目以重新开始.我的问题是我找不到将数据(用户输入)从一帧传输到另一帧的方法,我将提供需要从帧 1 传输到另一帧的代码

To begin with, I know that using multiple jframes is frowned upon, unfortunately i have gotten myself to deep into this project to start over. my issue is i cannot find a way to transfer data (user input) from one frame to another, i will provide the code i need to be transferred from frame 1 to another frame

这是他们必须输入的姓名和电子邮件的代码

this is my code for the name and email they have to input

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

        }

这是转到新框架的按钮代码

here is the button code to go to the new frame

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


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

我是 Swing 新手,我不需要有人来完成我的程序.我只需要知道如何将其转移到新框架上的新文本框.

i am new to swing and i dont need someone to complete my program. i just need to know how to carry it over to a new text box on a new frame.

推荐答案

这样做相当容易.您需要做的就是设置一个构造函数,在该构造函数中您将带有所需值的框架传递给新框架.

Doing this is fairly easy. All you need to do is to set a constructor in which you are passing your frame with the values you need over to your new frame.

例如,我有一个 LoginScreen.java 和 DoctorScreen.java.如果我的用户成功输入了他的详细信息并登录,我会将一个医生的 ArrayList 从一个 JFrame 传输到另一个 JFrame,或者更准确地说,通过创建该类的新实例,从一个 Java 类传输到另一个

For example, I have a LoginScreen.java and DoctorScreen.java. If my user successfully entered his details and logs in, I transfer an ArrayList of Doctors from one JFrame to another JFrame, or more precisely, from one java class to another by creating a new instance of that class

这里的例子

将数组列表从 LoginScreen.java 传递到 DoctorScreen.java

Passing an arraylist from LoginScreen.java to DoctorScreen.java

DoctorScreen dScreen = new DoctorScreen(frame, docList,d);

现在采用从 LoginScreen.java 传递的这些值并在 DoctorScreen.java 中设置它们

Now Taking those values passed from LoginScreen.java and setting them in DoctorScreen.java

public DoctorScreen(JFrame frame, ArrayList<Doctor> docList, Doctor d) {
    // TODO Auto-generated constructor stub
    dialog = new JFileChooser(System.getProperty("user.dir"));
    this.frame = frame;
    this.docList = docList;
    this.d = d;
    initialize();
}

现在,您可以更改 DoctorScreen 构造函数以适应您尝试执行的任何项目.

Now, you can change the DoctorScreen Constructor to fit into whatever project you are trying to do.

我建议您采取的步骤是创建一个 Java 文件来处理您的输入,第二个 Java 文件显示您在第一个文件中输入的内容

The steps I would advise you to take is to create a Java file for handling your input, and the second Java file to display what you entered in the first file

EG:

JButton btnContinue = new JButton("Continue");
btnContinue.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       String field1 = txtrEnterYourFull.getText();
       String name = nameL.getText();
       String field2 = txtroptionalEnterYour.getText();

       Display display = new Display(name, field1, field2);//using this as example
}

}

然后在你的 display.java 中,调用你的构造函数来获取这些字段并将它们显示在文本字段/文本区域或框架中的 JLabel 中

Then in your display.java, call your constructor taking in these fields and display them either in a textfield/textarea or in a JLabel in your frame

 String name, field1, field2;

 public Display(String name, String field1, String field2){

     this.name = name;
     this.field1 = field1;
     this.field2 - field2;
 }

请注意,这些变量已经被声明了,我只是为了演示目的使用它.

Please take note that these variables have already been declared and I am merely using this for demonstration purposes.

这篇关于将信息从一个 jframe 传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 15:52
查看更多