简体   繁体   English

从另一个JPanel中的数组获取信息

[英]Getting info from an array that is in another JPanel

I am storing some info in an ArrayList that is in a JPanel. 我将一些信息存储在JPanel中的ArrayList中。 I want to access this info from a JFrame so that I can print the contents of the ArrayList . 我想从JFrame访问此信息,以便可以打印ArrayList的内容。

How do I do this? 我该怎么做呢?

This is what i have tried so far: 到目前为止,这是我尝试过的:

    package projektarbete;

import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

public class Spelet extends javax.swing.JPanel {
ArrayList<String> Resultat = new ArrayList<>();


.....

        if(gameOver == true || vinst == true){
        btnTillbakaTillMeny.setVisible(true);
        int klick = antalKlick;
        String namn = txfNamn.getText();
        //Integer.toString(klick);
        String klickString = klick+"";
        String score = namn+"\t"+klickString;
        Resultat.add(score);

That was the JPanel and the info is stored in the ArrayList called Restultat. 那就是JPanel,信息存储在名为Restultat的ArrayList中。

This is how I am trying to retrieve the info from the JFrame: 这就是我试图从JFrame检索信息的方式:

    package projektarbete;

import javax.swing.JFrame;

public class Instruktioner extends javax.swing.JFrame {
//private final Meny Meny = new projektarbete.Meny();
    private static void close() {
      //  throw new UnsupportedOperationException("Not supported yet."); 
    }


    public Instruktioner() {
        initComponents();

    Spelet Resultat = new Spelet(); 
        jTextArea1.setText(Resultat);
    }

The thing is that NetBeans is underlining Resultat in jTextArea1.setText(Resultat); 事实是,NetBeans在jTextArea1.setText(Resultat);强调了Resultat jTextArea1.setText(Resultat);

Any ideas? 有任何想法吗?

You cannot put a Resultat object as a parameter to setText(), because that method does not Accept a parameter of that type. 您不能将Resultat对象作为setText()的参数,因为该方法不接受该类型的参数。 If you look at the javadoc for the method, you will see what type(s) it takes (there may be more than one 'signature' for the method, each signature taking a different combination of types). 如果查看该方法的javadoc,您将看到它需要什么类型(该方法可能有多个“签名”,每个签名采用不同的类型组合)。

I think what you want to do is have a class and then an object that holds the data for your program. 我认为您想要做的是拥有一个类,然后是一个保存程序数据的对象。 It will have the necessary methods for setting and obtaining data, and for calculating things that need calculation. 它将具有设置和获取数据以及计算需要计算的事物的必要方法。 Then, any object that is going to present information to the user (panel, frame, whatever) will need to have a reference to the class holding the data, and can call methods to get what it needs. 然后,任何要向用户呈现信息的对象(面板,框架等)都需要引用保存数据的类,并且可以调用方法来获取所需的信息。

This is the very fundamental idea behind "model-view-controller" -- a *separation of concerns", where the logic for handling data is separated from the logic for displaying that data. It helps in the common cases where you need to change the presentation but the data handling itself is ok. 这是“模型-视图-控制器”(“关注点分离”)背后的最基本思想,其中处理数据的逻辑与显示数据的逻辑分离,在需要更改的常见情况下有帮助演示文稿,但数据处理本身还可以。

setText()正在等待字符串,但是您给了它一个ArrayList

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

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