Привет, мне нужна помощь с этим простым приложением Java Flashcard, которое я делаю.
Каждый раз, когда я нажимаю свой JButton
для добавления новой колоды, я создаю новый объект колоды и добавляю его в список колоды.Для каждой созданной колоды я добавил кнопку «Добавить карту», которая будет отображаться рядом с ней на панели jpane.Проблема в том, что всякий раз, когда я добавляю другую колоду, слушатель действия работает только для самого последнего созданного объекта колоды.Мне нужна помощь, чтобы найти способ заставить слушателя действия быть «назначенным» на конкретную колоду
вот мой код
public class FlashCardGUI2 extends JFrame{
private int width;
private int height;
private Container contentPane1;
//Deck title label//
public JLabel Decktitle;
//
//bottom panel//
private JPanel Namedeckpanel;
private JLabel DeckName;
private JTextField Deckinputname;
public JButton Addadeck;
//
//decks panel//
private JPanel wordsPanel;
//
//add a card panel//
private JPanel addcardspanel;
public JButton AddCardin,returntoDecks;
private JTextField front,back;
public JLabel frontl, backl;
//
//view cards panel//
private static int cardsize;
public static HashMap<Deck,JButton> hmap;
public JButton btn, AddCardsBtn, ViewCardsBtn, StudyBtn;
private JPanel viewcardspanel, viewcardspanel2,viewcardspanel3;
public static Deck deck;
public Card card;
public String inputTitle;
public JLabel csize, test,Decktitlein;
public static JLabel inputName;
public JTable table;
private ArrayList<JLabel> labels;
private ArrayList<String> words;
private static ArrayList<JLabel> csizelabels;
private ArrayList<String> csizewords;
private static ArrayList<JButton> addcardsbtn, viewcardsbtn, studybtn;
private static ArrayList<Deck> deckarray;
public JFrame AddCardFrame, ViewCardsFrame, StudyFrame;
public static String decktitleee;
public FlashCardGUI2(int w, int h) {
width = w;
height = h;
//label for add a deck window//
Decktitle = new JLabel ("Decks",SwingConstants.CENTER);
//
//bottom panel//
Namedeckpanel = new JPanel();
DeckName = new JLabel("Deck Name: ");
Deckinputname = new JTextField(20);
Addadeck = new JButton("Add a Deck");
//
//decks panel//
wordsPanel = new JPanel();
//
//Add A Card panel//
addcardspanel = new JPanel();
AddCardin = new JButton("Add Card");
returntoDecks = new JButton("Return to Decks");
frontl = new JLabel("Front: ");
backl = new JLabel ("Back: ");
front = new JTextField(15);
back = new JTextField(15);
//
//view cards panel//
viewcardspanel = new JPanel();
viewcardspanel2 = new JPanel();
viewcardspanel3 = new JPanel();
}
public void setUpGUI() {
setSize(width, height);
setTitle("Flash Cards");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane1 = getContentPane();
contentPane1.setBackground(Color.PINK);
contentPane1.add(Decktitle, BorderLayout.NORTH);
contentPane1.add(Namedeckpanel, BorderLayout.SOUTH);
Namedeckpanel.setLayout(new FlowLayout());
Namedeckpanel.add(DeckName);
Namedeckpanel.add(Deckinputname);
Namedeckpanel.add(Addadeck);
//actionlistener for add a new deck button//
Addadeck.addActionListener(new AddANewDeck());
//arraylists//
words = new ArrayList<>();
labels = new ArrayList<>();
csizewords = new ArrayList<>();
csizelabels = new ArrayList<>();
addcardsbtn = new ArrayList<>();
viewcardsbtn = new ArrayList<>();
studybtn = new ArrayList<>();
deckarray = new ArrayList<>();
HashMap<Deck,JButton> hmap = new HashMap<Deck,JButton>();
setVisible(true);
}
//Add a new deck action for Add a new deck button//
class AddANewDeck implements ActionListener{
public void actionPerformed (ActionEvent ae){
// taking in input//
String text = Deckinputname.getText();
inputName = new JLabel(text,SwingConstants.CENTER);
//adding to string for Add a card pane//
decktitleee = text;
//newpane and grid//
contentPane1.add(wordsPanel, BorderLayout.CENTER);
wordsPanel.setLayout(new GridLayout(csizewords.size()+1,5));
wordsPanel.setBackground(Color.PINK);
//adding the deck title
words.add(text);
labels.add(inputName);
//adding it to the panel
wordsPanel.add(inputName);
//new deck
deck = new Deck(text);
System.out.println("new deck is made");
System.out.println(deck.getTitle());
//loop for adding deck to array//
deckarray.add(deck);
//test = new JLabel("hi");
//getting deck size//
cardsize = deck.getSize();
String size = String.valueOf(cardsize);
csize = new JLabel(size,SwingConstants.CENTER);
//adding buttons
System.out.println(deckarray.size());
addcardsbtn.add( AddCardsBtn = new JButton("Add Cards") );
viewcardsbtn.add(ViewCardsBtn = new JButton ("View Cards"));
studybtn.add(StudyBtn = new JButton ("Study"));
//adding size
csizewords.add(size);
csizelabels.add(csize);
//adding size and buttons to panel
wordsPanel.add(csizelabels.get(csizelabels.size()-1));
wordsPanel.add(AddCardsBtn);
wordsPanel.add(ViewCardsBtn);
wordsPanel.add(StudyBtn);
returntoDecks.addActionListener(new ReturntoDeck());
//button to open add a new card//
AddCardin.addActionListener(new AddANewCard());
hmap.put(deck,AddCardsBtn);
contentPane1.revalidate();
contentPane1.repaint();
contentPane1.setVisible(true);
}
}
//action listener for add cards button in deck pane//
//this is within the action listener of add anew deck///
//to make an action listener per button//
class AddCardsAction extends AddANewDeck implements ActionListener
{
public void actionPerformed (ActionEvent ae) {
AddCardFrame = new JFrame();
AddCardFrame.setSize(600,300);
AddCardFrame.setTitle("Add A Card");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AddCardFrame.getContentPane();
//AddCardFrame.add(addcardspanel,BorderLayout.NORTH);
//test println//
System.out.println("addcardsaction passed");
System.out.println(deck.getTitle());
//addcardspanel.setLayout(new FlowLayout());
//code for card title//
AddCardFrame.setVisible(true);
Decktitlein = new JLabel (decktitleee,SwingConstants.CENTER);
AddCardFrame.add(Decktitlein,BorderLayout.NORTH);
//code for front and back panel and buttons//
AddCardFrame.add(addcardspanel,BorderLayout.CENTER);
addcardspanel.setLayout(new GridLayout(3,2));
addcardspanel.add(frontl);
addcardspanel.add(front);
addcardspanel.add(backl);
addcardspanel.add(back);
addcardspanel.add(AddCardin);
addcardspanel.add(returntoDecks);
System.out.println("index tester:");
System.out.println(deckarray.get(0).getTitle());
}
}
//button for addacard//
//AddCardsBtn.addActionListener(new AddCardsAction());
for (JButton buttons : addcardsbtn){
buttons.addActionListener(new AddCardsAction());
}
}