简体   繁体   English

无法从其他类调用方法

[英]Can't call method from another class

I have two classes. 我有两节课。 The main one opens the second one in a jframe in which the user will press a button and trigger a method from the main class/jframe editare(String value) that will automatically add some data to some jtextfields in the main jframe.The problem is that it won't trigger the method.I tried calling other methods from the main class,it doesn't call them either.I tried a lot of stuff for like the past 1-2 hours,can't figure it out. 主菜单在jframe中打开第二个菜单,用户将在其中按下按钮并从主类/ jframe editare(String value)触发一个方法,该方法将自动向主jframe中的某些jtextfield添加一些数据。我尝试从主类中调用其他方法,但也没有调用它们。我在过去的1-2个小时中尝试了很多方法,无法弄清楚。

Here is some code : From the second jframe : 这是一些代码:从第二个jframe开始:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
Test2 test2=new Test2();
test2.citireser(list.getSelectedValue().toString()); //won't work.works if i call it from the same method,the main one
test2.restart(); //won't work either
this.dispose(); }   

From the first jframe,the main one : 从第一个jframe开始,主要的是:

public void citireser(String cur) {
    try {
        serializedPath = "C:/Inter/" + cur;
        InputStream file = new FileInputStream(serializedPath);
        InputStream buffer = new BufferedInputStream(file);
        ObjectInput input = new ObjectInputStream(buffer);

        String[] storeAllArraysREAD[] = (String[][]) input.readObject();
        prodr = storeAllArraysREAD[0];
        cantr = storeAllArraysREAD[1];
        pretr = storeAllArraysREAD[2];
        input.close();
        buffer.close();
        file.close();
       System.err.println("prodr[1]= "+prodr[1].toString());
        for (int m = 0; m < prodr.length - 1; m++) {

        allprod.get(m).setText(prodr[m]);
        allcant.get(m).setText(cantr[m]);
        allpret.get(m).setText(pretr[m]);
        produsnou();

    }

    } catch (ClassNotFoundException ex) {
        System.err.println("EROARE");
    } catch (IOException ex) {
       System.err.println("EROARE");
    }

}

EDIT : Ok,after trying different stuff for a couple of hours i've got it. 编辑:好的,尝试了几个小时的其他东西后,我知道了。

public class Opt extends javax.swing.JFrame implements Printable {

private final Test2 main; 

public Opt(Test2 aMain) {
     main = aMain;
    try {

    } catch (Exception e) {
        e.printStackTrace();
    }
    initComponents();
    jScrollPane2.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
    jScrollPane2.getVerticalScrollBar().setUnitIncrement(16);

    citirel();

    if (list.getModel().getSize() == 0) {
        jButton1.setEnabled(false);
        jButton2.setEnabled(false);
    }

}

Thanks for your help,i don't know who i should pick as the right answer :( Sorry to the other guy 感谢您的帮助,我不知道该选谁给我:(对不起,另一个人

The problem here is that you're working with a new instance of Test2 . 这里的问题是您正在使用Test2的新实例。 In the action performed (first block of code), you're creating a new Test2 (which would be the first frame). 在执行的操作(代码的第一块)中,您正在创建一个新的Test2 (这将是第一帧)。 You have to keep somewhere (usually a field) the reference to the first Test2 created. 您必须将对创建的第一个Test2的引用保留在某个地方(通常是一个字段)。

If you're having further issues, consider editing your question and posting the full code (the two frames entirely, at least). 如果您还有其他问题,请考虑编辑您的问题并发布完整代码(至少两个框架)。 My spider senses are telling me that there's some context missing. 我的蜘蛛感觉告诉我,缺少某些上下文。

Also, we have similar family names. 另外,我们有相似的姓氏。 :-) :-)

Please correct me if any of this is wrong, as I'm trying to understand your program from incomplete code: 如果这有任何错误,请更正我,因为我正在尝试从不完整的代码中了解您的程序:

  1. Test2 (a JFrame containing your program entry point main(string[]) ) at some point creates a second class (also a JFrame ) and opens it. Test2 (一个JFrame包含了程序入口点main(string[])在某一时刻)创建了一个第二类(也是JFrame ),并打开它。
  2. When you click a certain button in your second window, you wish to modify some elements of the Test2 window, and close the secondary window. 当您单击第二个窗口中的某个按钮时,您希望修改Test2窗口的某些元素,并关闭第二个窗口。

Assuming the above is correct, there is one obvious problem I can see in the code snippets you've posted. 假设以上内容正确无误,那么在您发布的代码段中我可以看到一个明显的问题。

In jButton3ActionPerformed , you're creating a new Test2 object, and modifying that. jButton3ActionPerformed ,您将创建一个新的 Test2对象,并对其进行修改。 If you want to modify the original window, you need to be storing a reference to it. 如果要修改原始窗口,则需要存储对其的引用。 For example, require a Test2 object as a parameter to your second class, and store that parameter as a field in the class. 例如,要求将Test2对象作为第二个类的参数,并将该参数存储为类中的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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