简体   繁体   English

JFrames 之间的数据传输

[英]Data transfer between JFrames

I have JFrame 1 which shows JLabel "Balance" - my bank account balance and 2 JButton components (Add income; Add expenses).我有JFrame 1,它显示JLabel “余额”——我的银行账户余额和 2 个JButton组件(添加收入;添加支出)。 By clicking one of these buttons I hide main frame and open income of expenses frame where I add the data.通过单击这些按钮之一,我会隐藏主框架并打开我添加数据的费用收入框架。

After I input amounts into JTextField components and click "Save" button, in dialog field I can see that my record was saved, but when I click "Back" button, the "Balance" label stays 0 as if nothing was entered.在我将金额输入JTextField组件并单击“保存”按钮后,在对话框字段中我可以看到我的记录已保存,但是当我单击“返回”按钮时,“余额”标签保持为 0,就好像没有输入任何内容一样。

Could somebody help me?有人可以帮助我吗? My code is a mess now so I doubt it would be helpful.我的代码现在一团糟,所以我怀疑它会有所帮助。

Here is an easy example where I tried to rebuild your program:这是一个简单的例子,我试图重建你的程序:

public class Class {
    public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
        Frame2 frame2 = new Frame2();

        frame1.setChildWindow(frame2);
        frame2.setParentWindow(frame1);
    }
}

Frame1:第一帧:

import javax.swing.*;

class Frame1 extends JFrame {
    private int balance = 0;
    private JLabel balanceLabel = new JLabel(String.valueOf(balance));
    private Frame2 childWindow;

    Frame1() {
        JPanel panel = new JPanel();
        panel.add(new JLabel("Balance:"));
        panel.add(balanceLabel);
        JButton balanceButton = new JButton("Balance");
        balanceButton.addActionListener(e -> {
            childWindow.setVisible(true);
            setVisible(false);
        });
        panel.add(balanceButton);
        getContentPane().add(panel);
        pack();
        setVisible(true);
    }

    void setChildWindow(Frame2 childWindow) {
        this.childWindow = childWindow;
    }

    void addBalance(int balance) {
        this.balance+=balance;
        balanceLabel.setText(String.valueOf(this.balance));
    }
 }

Frame2:帧 2:

import javax.swing.*;

class Frame2 extends JFrame {
    private Frame1 parentWindow;

    Frame2() {
        JComboBox<Integer> comboBox = new JComboBox<>(new Integer[] {1,2,3,4,5,6,7,8,9});
        JButton addButton = new JButton("add");

        addButton.addActionListener(e -> {
            parentWindow.addBalance((Integer)comboBox.getSelectedItem());
            parentWindow.setVisible(true);
            setVisible(false);
        });

        JPanel panel = new JPanel();
        panel.add(comboBox);
        panel.add(addButton);
        getContentPane().add(panel);
        pack();
    }

    void setParentWindow(Frame1 parentWindow) {
        this.parentWindow = parentWindow;
    }
}

If you have any further questions, feel free to ask!如果您还有其他问题,请随时提问!

(But btw, in your next questions, post some code, so that other people can help you better. Even if it is a mess, other people could help you with that as well and give you hints what you could to better or how you could write your code cleaner ^^) (但是顺便说一句,在你的下一个问题中,发布一些代码,以便其他人可以更好地帮助你。即使它是一团糟,其他人也可以帮助你解决这个问题,并给你提示你可以做得更好或你如何可以写你的代码更干净^^)

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

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