简体   繁体   English

从一个类到另一个的ArrayList

[英]ArrayList from one class to another

I have read quite a few pages of stackoverflow but I wasn't able to get my ArrayList to get copied unto another class. 我已经阅读了很多关于stackoverflow的页面,但是我无法获取ArrayList来复制到另一个类。 Here's the scenario, I'm building a quick book saver app, similar to what you would have in a library but simpler (for school). 在这种情况下,我正在构建一个快速的图书保护器应用程序,类似于您在图书馆中可以使用的应用程序,但更简单(对于学校而言)。

I have my main library class (with the main) that has the swing set up for the main menu/options. 我的主库类(包含主库)具有为主菜单/选项设置的秋千。

I have the book class with the constructor for new books as follows: 我有带有新书构造函数的book类,如下所示:

public class Livre {

private String titre;
private String soustitre;
private String auteur;
private String editeur;
private String collection;
private String isbn;
private long cup;
private double prixDeVenteSuggere;
private double prixVente;
private int nbPages;
private boolean disponible;

public Livre(String titre, String soustitre, String auteur, String editeur, String collection, String isbn, long cup, double prixDeVenteSuggere, double prixVente, int nbPages, boolean disponible){
    this.titre = titre;
    this.soustitre = soustitre;
    this.auteur = auteur;
    this.editeur = editeur;
    this.collection = collection;
    this.isbn = isbn;
    this.cup = cup;
    this.prixDeVenteSuggere = prixDeVenteSuggere;
    this.prixVente = prixVente;
    this.nbPages = nbPages;
    disponible = true;
}

public Livre() {

}

public String getTitre() {
    return titre;
}

public void setTitre(String titre) {
    this.titre = titre;
}

public String getSoustitre() {
    return soustitre;
}

public void setSoustitre(String soustitre) {
    this.soustitre = soustitre;
}

public String getAuteur() {
    return auteur;
}

public void setAuteur(String auteur) {
    this.auteur = auteur;
}

public String getEditeur() {
    return editeur;
}

public void setEditeur(String editeur) {
    this.editeur = editeur;
}

public String getCollection() {
    return collection;
}

public void setCollection(String collection) {
    this.collection = collection;
}

public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

public long getCup() {
    return cup;
}

public void setCup(long cup) {
    this.cup = cup;
}

public double getPrixDeVenteSuggere() {
    return prixDeVenteSuggere;
}

public void setPrixDeVenteSuggere(double prixDeVenteSuggere) {
    this.prixDeVenteSuggere = prixDeVenteSuggere;
}

public double getPrixVente() {
    return prixVente;
}

public void setPrixVente(double prixVente) {
    this.prixVente = prixVente;
}

public int getNbPages() {
    return nbPages;
}

public void setNbPages(int nbPages) {
    this.nbPages = nbPages;
}

public boolean isDisponible() {
    return disponible;
}

public void setDisponible(boolean disponible) {
    this.disponible = disponible;
}

} }

Option #1 on the Library class (built with WindowBuilder) has the "New" button which opens a second JFrame to input all the info in regards to the book. Library类(使用WindowBuilder构建)上的选项#1具有“新建”按钮,该按钮将打开第二个JFrame以输入有关该书的所有信息。

in this JFrame class, I've added an actionListener on the confirm button to confirm the input on the JTextFields to be added as an object as follows: 在此JFrame类中,我在确认按钮上添加了一个actionListener ,以确认要添加为对象的JTextFields上的输入,如下所示:

public void confirmerLivre(){
    l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(), 
                    txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(), 
                    Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()), 
                    Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);

    confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList
}

What I can't wrap my head around is being able to take the ArrayList confirmerLivre from the 2nd JFrame class and push it unto my main JFrame class to be manipulated further with other options. 我无法确定的是能够从第二个JFrame类中获取ArrayList JFrame并将其推送到我的主JFrame类中,以便通过其他选项进行进一步操作。

Any help will be greatly appreciated. 任何帮助将不胜感激。 Thank you 谢谢

Probably the quickest fix is to create/expose these methods in your main JFrame class: 最快的解决方法可能是在主JFrame类中创建/公开这些方法:

  • getBookList()
  • setBookList()

When you create your popup JFrame, you need to pass an instance of your main JFrame class to it in its constructor: 创建弹出JFrame时,需要在其构造函数中将主JFrame类的实例传递给它:

public PopupFrame extends JFrame {
  private MainFrame main;
  public PopupFrame(MainFrame main) {
    this.main = main;
  }
}

Now that you have access to your main JFrame class from your popup, you can just go main.getBookList() to get the list (I'd recommend reading this question also) 现在您可以从弹出窗口访问主JFrame类,您只需转到main.getBookList()即可获取列表(我建议您也阅读此问题

If you create your ArrayList as a public variable in the second JFrame class (outside any of the methods) then it can be used in the first class such as: 如果您在第二个JFrame类(任何方法之外)中将ArrayList创建为公共变量,则可以在第一类中使用它,例如:

SecondJFramesName.confirmerLivre()

In this code SecondJFramesName is the name of your second JFrame class. 在此代码中, SecondJFramesName是第二个JFrame类的名称。 Now that your ArrayList is a public variable it can be accessed outside the class. 现在,您的ArrayList是一个公共变量,可以在类外部访问它。

Note: your second JFrame 's name is the one you use to create it in a way such as this: 注意:第二个JFrame的名称就是您用来创建它的名称,例如:

JFrame SecondJFramesName = new JFrame("My title");

If you need any more specific details please comment! 如果您需要更多具体细节,请发表评论!

Hopefully this helps! 希望这会有所帮助!

Maybe observer pattern could help you: 也许观察者模式可以帮助您:

public interface ConfirmerLivreMonitor{
    void onConfirmerLivreChange(List<...> confirmerLivre);
}

then 然后

//...
private ConfirmerLivreMonitor confirmerLivreMonitor;
public void setConfirmerLivreMonitor(ConfirmerLivreMonitor confirmerLivreMonitor ){
    this.confirmerLivreMonitor = confirmerLivreMonitor 
}
//....
public void confirmerLivre(){
    l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(), 
                    txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(), 
                    Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()), 
                    Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);

    confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList

    if(confirmerLivreMonitor != null){   //notify confirmerLivre change
       confirmerLivreMonitor.onConfirmerLivreChange(confirmerLivre);
    }
}

make the Main JFrame implemnents ConfirmerLivreMonitor,so you can: 使Main JFrame实现ConfirmerLivreMonitor,因此您可以:

sencondJFrame.setConfirmerLivreMonitor(this);

or just pass a anonymous class: 或只是通过匿名课程:

sencondJFrame.setConfirmerLivreMonitor(new ConfirmerLivreMonitor(){

        public void onConfirmerLivreChange(List<...> confirmerLivre){
            //display in Main JFrame,maybe
        }
});

once the confirmerLivre change, the main frame can display(or something else) the first time,very cool 一旦confirmerLivre更改,主框架就可以第一次显示(或其他),非常酷

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

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