gpt4 book ai didi

java - 使用 ActionListener 将 int 值从一个 jFrame 传递到另一个 jFrame

转载 作者:行者123 更新时间:2023-12-01 13:20:36 26 4
gpt4 key购买 nike

我有两个类,我正在使用 ActionListeners,问题是我想从第二个类接收第一个类中的 int 值...第一类是这个:

public class PanelCotizacion extends javax.swing.JPanel implements ActionListener {
private int numCotizacion = 0;
public PanelCotizacion() {
initComponents();
}
public void actionPerformed(ActionEvent e) {
System.out.println("HERE IS WHERE I WANT TO RECEIVE THE VALUE");
this numCotizacion = "";
//THE VALUE THAT I WANT TO RECEIVE FROM THE OTHER jFRAME
//TRIGGERED BY THE EVENT OF THE BUTTON (action performed)
}
}

这是第二个,我想在其中发送 int 值:

public class BusquedaCotizacionGUI extends javax.swing.JFrame {
private int numCotizacion = 0;
public BusquedaCotizacionGUI() {
initComponents();
this.setLocationRelativeTo(null);
PanelCotizacion pC = new PanelCotizacion();
this.cmdOK.addActionListener(pC);
}
private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {
this.numCotizacion = Integer.parseInt(this.txtNumCotizacion.getText());
//Here is where I WANT TO PASS THE VARIABLE "numCotizacion" tho the other class
//Can Somebody Help Me
this.dispose();
}
}

你们能帮我做到这一点吗,非常感谢!

最佳答案

根据您的代码,我认为 BusquedaCotizacionGUI JFrame 负责打开 PanelCotizacion Jpanel 并传递您的变量。

因此有很多方法可以将变量从 JFrame 传递到 JPanel

您可以创建一个采用 int 参数的构造函数,然后在构造函数中传递变量,例如:

public PanelCotizacion(int numCotizacion) {
initComponents();
this.numCotizacion = numCotizacion;
}

或者您可以将 JFrame 作为父组件传递给 JPanel 到构造函数,然后通过创建获取方法来获取值,例如,

private JFrame parent;
public PanelCotizacion(JFrame parent) {
initComponents();
this.parent= parent;
}

然后获取如下值:

parent.getNumCotizacion();

关于java - 使用 ActionListener 将 int 值从一个 jFrame 传递到另一个 jFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22045831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com