Pobrane i przekazanie wartości zmiennej.

0

Witam czy istnieje możliwość wywołania frame (new frame2().setVisible(true)) po czym
w frame2, ustawic wartosc pola oraz je przekazać do frame który go wywołał?

Próbowałem cos takiego:

// frame 1

Button() // wyświetlany frame 2
{ new frame2().setVisible(true);
}

Void wys()
{ jLabel1.setText( new frame2().pobierz());
}

// frame 2
String Pobierz ()
{ return jTextField1.getText();
}

Button() // zamknij frame 2
{ this.dispose();
}

Bardzo proszę nie zwracać na styl oraz język pisowni.

// edit koziołek: używaj tagów < code >

0

Łopatologicznie można to zrobić normalnie, choć nie wiem w 100% o co ci chodziło:

// frame 1

class Frame1
{
Frame2 frame2;
String poleZFrane2;
public frame1(){
super();
frame2 = new Frame2(this);
}

Button() // wyświetlamy frame 2
{ frame2.setVisible(true);
}

Void wys()
{ jLabel1.setText(poleZFrame2);
}

public setPole(String value){
poleZFrame2 = value;
}
}


// frame 2
class Frame2{
Frame1 frame1;
String mojePole;

public Frame2(Frame1 f){
frame1 = f;
}

void Pobierz ()
{

mojePole = jTextField1.getText();
frame1.setPole(mojePole);
}

}

Na przyszłość napisz te kilka linijek tak, żeby kod miał poprawną składnię i wcięcia. Ułatwia to czytanie.

0

Witaj,

 
package test;

public class FirstForm extends javax.swing.JFrame {
    
    String poleZFrame2;
    
    public FirstForm() {
        initComponents();
   }
   
public String setPole(String value)
    {
        return poleZFrame2 = value;
    }        
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jLabel1.setText(poleZFrame2);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new SecForm().setVisible(true);
    }                                        

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FirstForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}
 
package test;

public class SecForm extends javax.swing.JFrame {

    String mojePole;
    
    public SecForm() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Pobierz();
        this.dispose();
    }                                        


    void Pobierz ()
    {

        mojePole = jTextField1.getText();
        new FirstForm().setPole(mojePole);
    }
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SecForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}

Ogólnie powinno działać chodź nie łapie czemu tak nie jest.

0

Kod jest niepoprawny (niekompilowalny), w klasie SecForm wywołujesz metodę initComponents, której brak.

1 użytkowników online, w tym zalogowanych: 0, gości: 1