2014-02-24 103 views
0

这是第一个类,事情是我必须接收由按钮中的事件触发的其他类的值(行动表演),所以在这个班我想展示它!如何将文本设置为一个jTextField,它位于jFrame内部的jPanel中,以供其他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中值,记得我提到,在这两种jFrames,有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(); 
    } 
} 

所以,请不要看sintaxis,如果你可以给我一个想法来解决这个问题,我想也许不中JTextFields将导致显示它是私有的,有没有什么办法来显示它或如何我可以更新jPanel组件以显示更新TextFields吗?非常感谢!

+0

参见[多个JFrames,好/坏习惯的用?(http://stackoverflow.com/a/9554657/418556) –

+0

你只是创建PanelCotization'的'实例除非你在'initComponent()'中做了,但你没有显示,并且我猜是netbeans mattise生成的代码。 – nachokk

回答

1

您的示例患有参考问题。 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方法附加一个actionListenerBusquedaCotizacionGUI。当调用actionPeformed时,您需要设置PanelCotizacion实例的文本

+0

嗨,谢谢你的答案,你只能给我一个线索如何将一个actionListener附加到BusquedaCotizacionGUI? – Mario

+0

当然,请参阅*“In'BusquedaCotizacionGUI'下的大代码块add ...”*?然后给出一个'BusquedaCotizacionGUI'的实例,只需使用'instanceOfBusquedaCotizacionGUI.addActionListener(instanceOfAnActionListener)' – MadProgrammer

0

尝试在您的jframe的其他构造函数中使用您的jframe,文本和面板作为参数,而不是在调用您的jframe时使用它们操作按钮这样

public constructoroftheotherJFrame (firstJFrame frame , String yourtext){ 
this.frame=frame; 
this.text=text; 
// then type your code there 
} 
相关问题