![](/img/trans.png)
[英]How to access to the content of JTextField inside of Jpanel inside of JFrame?
[英]How to setText into a jTextField that is inside a jPanel that is inside a jFrame to from Other jFrame
这是第一个类,问题是我必须从按钮中的事件(执行的操作)触发的另一个类中接收值,所以在这个类中我要显示它!
public class PanelCotizacion extends javax.swing.JPanel {
private int numCotizacion = 0;
public int getNumCotizacion() {
return numCotizacion;
}
public void setNumCotizacion(int numCotizacion) {
this.numCotizacion = numCotizacion;
}
public PanelCotizacion() {
initComponents();
showTextFields();
}
show textFields(){
this.txtCosTra.setText(String.valueOf(cosTra));
}
}
这是第二个类,我要在其中发送jTextField中的值,请记住我提到在两个jFrame中都具有jPanels,并且jTextFields在其中。
public class BusquedaCotizacionGUI extends javax.swing.JFrame {
public BusquedaCotizacionGUI() {
initComponents();
this.setLocationRelativeTo(null);
}
private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {
PanelCotizacion p = new PanelCotizacion();
p.setNumCotizacion(Integer.parseInt(this.txtCotizacion.getText()));
p.setVisible(true);
p.revalidate();
p.updateUI();
this.dispose();
}
}
因此,请不要看单眼,如果您能给我一个解决该问题的想法,我想也许不显示它在jTextFields中是私有的,有没有办法显示它或如何将jPanel组件更新为显示更新的TextFields? 非常感谢!
您的示例遇到参考问题。 PanelCotizacion
的实例与PanelCotizacion
上的内容无关(或者至少您从未将其添加到屏幕上-这可以解决我不知道的问题)
最简单的解决方案是将某种侦听器附加到第二个类(事件的源),该类将提供值已更改的通知,然后提供某种访问器以从类中提取值,例如public String getText() {...}
。
在BusquedaCotizacionGUI
添加...
public void addActionListener(ActionListener listener) {
cmdOk.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
cmdOk.removeActionListener(listener);
}
public String getText() {
return txtCotizacion.getText();
}
在PanelCotizacion
或控制类的两个实例的容器中,都需要通过addActionListener
方法将actionListener
附加到BusquedaCotizacionGUI
。 调用actionPeformed
,您需要设置已有的PanelCotizacion
实例的文本
尝试使用jframe以及文本和面板作为jframe的另一个构造函数中的参数,而不是像这样调用动作按钮时在其中使用它们
public constructoroftheotherJFrame (firstJFrame frame , String yourtext){
this.frame=frame;
this.text=text;
// then type your code there
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.