[英]Passing int value from one jFrame to another jFrame using ActionListener
I Have two classes, I am using ActionListeners, the thing is that i want to receive an int value in the First class from the Second One... THE FIRST CLASS is this one: 我有两个类,我正在使用ActionListeners,问题是我想从第二个类的First类中接收一个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)
}
}
This is the Second One, where i want to send the int value: 这是第二个,我要在其中发送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();
}
}
Can you help me guys to do this, Thanks a lot! 您能帮我做到这一点吗,非常感谢!
From your code i think the BusquedaCotizacionGUI JFrame
responsible to open the PanelCotizacion Jpanel
and pass your variable. 从您的代码中,我认为BusquedaCotizacionGUI JFrame
负责打开PanelCotizacion Jpanel
并传递您的变量。
So there is many ways to pass a variable from JFrame
to JPanel
. 因此,有很多方法可以将变量从JFrame
传递到JPanel
。
You can create a constructor taking an int parameter, then pass the variable in the constructor, like: 您可以创建一个带有int参数的构造函数,然后在该构造函数中传递变量,例如:
public PanelCotizacion(int numCotizacion) {
initComponents();
this.numCotizacion = numCotizacion;
}
or you could pass the JFrame
as a parent component to JPanel
to the constructor, then get the value by creating a getting method, like, 或者您可以将JFrame
作为父组件传递给JPanel
到构造函数,然后通过创建一个geting方法来获取值,例如,
private JFrame parent;
public PanelCotizacion(JFrame parent) {
initComponents();
this.parent= parent;
}
then get the value like: 然后得到像这样的值:
parent.getNumCotizacion();
There is Object getSource()
method in ActionEvent
class, so in your ActionListener you can get the source and cast it to PanelCotizacion
. ActionEvent
类中有Object getSource()
方法,因此在ActionListener中,您可以获取源并将其转换为PanelCotizacion
。 Other possibility is to add reference to your BusquedaCotizacionGUI
into PanelCotizacion
's constructor. 另一种可能性是BusquedaCotizacionGUI
PanelCotizacion
的引用添加到PanelCotizacion
的构造函数中。
in your second JFrame (BusquedaCotizacionGUI) add this line of code (sth ike this) : 在您的第二个JFrame(BusquedaCotizacionGUI)中,添加以下代码行(例如):
PanelCotizacion.setParam(int parameterToPass)
and in the first JFrame (PanelCotizacion) add a method (say setParam) and an integer field (say myField): 在第一个JFrame(PanelCotizacion)中添加一个方法(例如setParam)和一个整数字段(例如myField):
public void setParam (int param) {
this.myField = param;
}
You should decide about how to implement this idea (static method vs creating an instance of the first JFrame,...); 您应该决定如何实现这个想法(静态方法与创建第一个JFrame的实例,...); this is just the general idea, customize it so as to fit your desire. 这只是一般性的想法,请对其进行自定义以满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.