简体   繁体   English

将数据从jframe加载到新打开的jframe

[英]Load data from jframe to the newly opened jframe

I'm a little bit new here. 我在这里有点新。 I'm stuck figuring out what's wrong with my code. 我一直在弄清楚我的代码出了什么问题。 I have a two JFrames, one is my main frame and the other is like a "view detail" frame whenever you click a selected data in the main frame. 我有两个JFrame,一个是我的主框架,另一个当您单击主框架中的选定数据时,就像一个“查看详细信息”框架。 This is my main class looks like: 这是我的主要班级看起来像:

public class MainClass{
    private JFrame frame;
    private JButton btnNewButton;
    private String testData;
    private DetailClass detail;

    public JFrame getFrame() {
    return frame;
    }

    public String getTest() {
    return testData;
    }

    public void setTest(String test) {
    this.testData = test;
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                main window = new main();
                window.frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public MainClass() {
    initialize();
}
private void initialize() {
        //some codes label, button, etc.
        btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            setTest("Data retrieved from main");
            detail.getFrame().setVisible(true);
        }
    });
 detail = new DetailClass();
 detail.setMainClass(this);
}
}

And here is my DetailClass where I want to show data that I get from main. 这是我的DetailClass,我要在其中显示从main获得的数据。 Public class DetailClass(){ private MainClass main; 公共类DetailClass(){私有MainClass main; private JFrame frame; 私有JFrame框架;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DetailClass window = new DetailClass ();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public DetailClass() {
    initialize();
}
public void initialize(){
//some codes for frame

//this is where I test if the detail class receives the data
System.out.println(main.getTest());

}
public void setMainClass(MainClass m){
    this.main = m;
}
}

That's it, the main.getTest() doesn't seem to work in intialize() but I tried to put it in a mouse event whenever I click a button in frame 2 and it works fine. 就是这样,main.getTest()在intialize()中似乎不起作用,但是每当我单击第2帧中的按钮时,我都试图将其置于鼠标事件中,并且效果很好。 It seems that it only works when the frame is already visible/activated/finished rendering but it won't work in initialize and I need it to preload the data in frame 2. I hope you guys can help me with this. 看来,只有当框架已经可见/已激活/完成渲染时,它才起作用,但在初始化时将不起作用,我需要它在框架2中预加载数据。希望大家对此有所帮助。 :) :)

Without a valid Minimal, Complete, and Verifiable example we can only guess what you might be doing wrong, but I'm guessing that the other() method, the one where you display the main's text, is only called once, on creation of the second JFrame, and before the text is changed. 没有有效的“ 最小,完整和可验证”示例,我们只能猜测您可能在做错什么,但我猜测在创建主体时, other()方法other()用于显示主文本的方法)仅被调用一次。第二个JFrame,并且在更改文本之前。 The solution here is as mentioned in comments -- pass information from one class to another through method or constructor parameters, and pass this information when it is needed , not only on program start up. 如注释中所述,这里的解决方案-通过方法或构造函数参数将信息从一类传递到另一类, 需要传递此信息 ,而不仅仅是在程序启动传递

Also as noted in comments, the 2nd dialog window should be a JDialog, not a JFrame (application window). 另外,如注释中所述,第二对话框窗口应该是JDialog,而不是JFrame(应用程序窗口)。

For example: 例如:

import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class TwoWindows {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        MainGui mainPanel = new MainGui();
        mainPanel.setPreferredSize(new Dimension(400, 250));
        JFrame frame = new JFrame("Main GUI");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class MainGui extends JPanel {
    private SubWindow subWindow = new SubWindow();
    private JDialog dialog;
    private JTextField textField = new JTextField("Text in field", 10);

    public MainGui() {
        add(textField);
        add(new JButton(new ShowDetailAction("Show Detail")));
    }

    private class ShowDetailAction extends AbstractAction {
        public ShowDetailAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // if dialog not yet created -- create it
            if (dialog == null) {
                Window win = SwingUtilities.getWindowAncestor(MainGui.this);
                dialog = new JDialog(win, "Details Window", ModalityType.MODELESS);
                dialog.add(subWindow);
                dialog.pack();
                dialog.setLocationRelativeTo(win);
            }
            String text = textField.getText();
            subWindow.passText(text);
            dialog.setVisible(true);
        }
    }
}

class SubWindow extends JPanel {
    private JLabel textLabel = new JLabel(" ");

    public SubWindow() {
        setPreferredSize(new Dimension(300, 60));
        add(new JLabel("Details:"));
        add(textLabel);
    }

    public void passText(String text) {
        textLabel.setText(text);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM