从jframe加载数据到新打开的jframe
Load data from jframe to the newly opened jframe
我有点陌生。我一直在弄清楚我的代码有什么问题。我有两个 JFrame,一个是我的主框架,另一个就像 "view detail" 框架,每当您单击主框架中的选定数据时。
这是我的主要 class 样子:
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);
}
}
这是我的 DetailClass,我想在其中显示从 main 获得的数据。
Public class DetailClass(){
私有主类主;
私有 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;
}
}
就是这样,main.getTest() 似乎在 intialize() 中不起作用,但每当我在第 2 帧中单击一个按钮时,我都尝试将其放入鼠标事件中,并且它工作正常。它似乎只有在帧已经 visible/activated/finished 渲染时才有效,但它在初始化时不起作用,我需要它来预加载帧 2 中的数据。我希望你们能帮助我解决这个问题。 :)
如果没有有效的 Minimal, Complete, and Verifiable example,我们只能猜测您可能做错了什么,但我猜测 other()
方法,即显示主体文本的方法,仅被调用一次,在创建第二个 JFrame 时,在文本更改之前。这里的解决方案如评论中所述——通过方法或构造函数参数将信息从一个 class 传递到另一个 并在需要 时传递此信息 ,不仅在程序启动时。
另外如评论中所述,第二个对话框 window 应该是 JDialog,而不是 JFrame(应用程序 window)。
例如:
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);
}
}
我有点陌生。我一直在弄清楚我的代码有什么问题。我有两个 JFrame,一个是我的主框架,另一个就像 "view detail" 框架,每当您单击主框架中的选定数据时。 这是我的主要 class 样子:
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);
}
}
这是我的 DetailClass,我想在其中显示从 main 获得的数据。 Public class DetailClass(){ 私有主类主; 私有 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;
}
}
就是这样,main.getTest() 似乎在 intialize() 中不起作用,但每当我在第 2 帧中单击一个按钮时,我都尝试将其放入鼠标事件中,并且它工作正常。它似乎只有在帧已经 visible/activated/finished 渲染时才有效,但它在初始化时不起作用,我需要它来预加载帧 2 中的数据。我希望你们能帮助我解决这个问题。 :)
如果没有有效的 Minimal, Complete, and Verifiable example,我们只能猜测您可能做错了什么,但我猜测 other()
方法,即显示主体文本的方法,仅被调用一次,在创建第二个 JFrame 时,在文本更改之前。这里的解决方案如评论中所述——通过方法或构造函数参数将信息从一个 class 传递到另一个 并在需要 时传递此信息 ,不仅在程序启动时。
另外如评论中所述,第二个对话框 window 应该是 JDialog,而不是 JFrame(应用程序 window)。
例如:
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);
}
}