简体   繁体   English

如何将Text设置为jFrame内的jPanel内的jTextField到其他jFrame

[英]How to setText into a jTextField that is inside a jPanel that is inside a jFrame to from Other jFrame

This is the first Class, the thing is that i have to receive the values from the other class triggered by the event in the button (action performed), so in this class i want to display it! 这是第一个类,问题是我必须从按钮中的事件(执行的操作)触发的另一个类中接收值,所以在这个类中我要显示它!

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));
    }
}

This is the second Class, where i want to send the value that is in the jTextField, remember that i mentioned that in both jFrames, have jPanels and the jTextFields are inside of it. 这是第二个类,我要在其中发送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();
    }
}

So please don't look the sintaxis, if you can give me an idea to solve that problem, i think maybe dont display it in the jTextFields cause are private, Are there any way to display it or How can i update the jPanel components to display the update TextFields? 因此,请不要看单眼,如果您能给我一个解决该问题的想法,我想也许不显示它在jTextFields中是私有的,有没有办法显示它或如何将jPanel组件更新为显示更新的TextFields? Thanks a lot! 非常感谢!

Your example suffers from a reference issues. 您的示例遇到参考问题。 The instance of PanelCotizacion has nothing to do with what's on the screen (or at least, you've never added it to the screen - which could the solution to the problem I don't know) PanelCotizacion的实例与PanelCotizacion上的内容无关(或者至少您从未将其添加到屏幕上-这可以解决我不知道的问题)

The simplest solution would be to attach some kind of listener to the second class (source of the event), which provides notification that the value has changed and then provide some kind of accessor to extract the value from the class, like public String getText() {...} for example. 最简单的解决方案是将某种侦听器附加到第二个类(事件的源),该类将提供值已更改的通知,然后提供某种访问器以从类中提取值,例如public String getText() {...}

In BusquedaCotizacionGUI add... BusquedaCotizacionGUI添加...

public void addActionListener(ActionListener listener) {
    cmdOk.addActionListener(listener);
}

public void removeActionListener(ActionListener listener) {
    cmdOk.removeActionListener(listener);
}

public String getText() {
    return txtCotizacion.getText();
}

Either in PanelCotizacion or the container controlling the two instances of the classes, you need to attach an actionListener to BusquedaCotizacionGUI via the addActionListener method. PanelCotizacion或控制类的两个实例的容器中,都需要通过addActionListener方法将actionListener附加到BusquedaCotizacionGUI When actionPeformed is called, you need to set the text of instance of PanelCotizacion you already have 调用actionPeformed ,您需要设置已有的PanelCotizacion实例的文本

try to use a your jframe and text and panel as parameters in the other constructor of your jframe than use them inside it when you invoke your action button like this 尝试使用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.

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