2017-05-06 3 views
0

Ich mache ein Kartenspiel für Klasse, das ein dealButton benötigt, um eine Kartenhand auszugeben und sie im Rahmen anzuzeigen. Ich habe eine Schleife, die durch die gegebene Hand geht und die Karten richtig erstellt und anzeigt, aber wenn dieser Code vom Konstruktor in den ActionListener des DealButtons verschoben wird, wird nichts angezeigt.Hinzufügen von JButtons außerhalb des JFrame-Konstruktors

Gibt es eine Möglichkeit, dem Rahmen außerhalb des Konstruktors JButtons hinzuzufügen?

Hier ist der Code, der die Hände werden die Anzeige sollte:

deck.shuffle();

 Hand hands[] = deck.deal(4, 13); 

     GridBagConstraints handCon1 = new GridBagConstraints(); 
     handCon1.insets = new Insets(0, 0, 0, 0); 
     handCon1.gridy = 10; 

     int handSize = hands[0].getCards().length; 
     Card cards[] = hands[0].getCards(); 
     for(int i = 0 ; i < handSize ; i++){ 
      JButton card = new JButton(); 
      PlayCardListener playCard = new PlayCardListener(deck, cards[i], card); 
      card.addActionListener(playCard); 
      card.setIcon(new ImageIcon(cards[i].getImg())); 
      card.setBorder(null); 
      handCon1.gridx = i; 
      add(hand1); 
      hand1.add(card, handCon1); 
+0

Willkommen bei Stack-Überlauf aufgerufen werden soll! Fragen, die Debugging-Hilfe suchen ("Warum funktioniert dieser Code nicht?") Müssen das gewünschte Verhalten, ein spezifisches Problem oder einen Fehler und den kürzesten Code enthalten, der für die Reproduktion in der Frage erforderlich ist. Fragen ohne eine klare Problemstellung sind für andere Leser nicht nützlich. Siehe: Erstellen eines [mcve]. Du sagst nur "wenn ich es außerhalb des Konstruktors versuche, funktioniert es nicht". Dann poste ** diesen Code, der nicht funktioniert. Und die Antwort auf Ihre Frage lautet: JA, das ist möglich. – GhostCat

Antwort

0

Es ist sicher möglich:

public class Deal { 

    public static void main(String[] args) { 
     new Deal(); 
    } 

    private JFrame frame; 
    private JTextField statusField; 
    private JButton dealButton; 
    private JPanel buttonsPanel; 

    private Deal() { 
     statusField = new JTextField("starting - press DEAL"); 
     statusField.setEditable(false); 

     dealButton = new JButton("DEAL"); 
     dealButton.addActionListener(this::doDeal); 

     buttonsPanel = new JPanel(); 

     frame = new JFrame(); 
     frame.add(dealButton, BorderLayout.PAGE_START); 
     frame.add(buttonsPanel, BorderLayout.CENTER); 
     frame.add(statusField, BorderLayout.PAGE_END); 
     frame.setSize(600, 200); 
     frame.validate(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private void doDeal(ActionEvent ev) { 
     buttonsPanel.removeAll(); 
     List<String> cards = Arrays.asList("A", "2", "3", "4", "5"); 
     Collections.shuffle(cards); 
     for (String text : cards) { 
      JButton button = new JButton(text); 
      button.addActionListener(this::doCard); 
      button.setActionCommand(text); 
      buttonsPanel.add(button); 
     } 
     statusField.setText("new buttons"); 
     frame.validate(); 
    } 

    private void doCard(ActionEvent ev) { 
     String text = ev.getActionCommand(); 
     statusField.setText(text); // just demo 
    } 
} 

ich das folgende Segment des Codes vermuten:

for (...) { 
    ... 
    add(hand1); 
    hand1.add(card, handCon1); 

überprüfen, ob es wirklich nötig ist hand1 in der Schleife hinzuzufügen (mehrmals) - aber nur eine Vermutung, ohne zu wissen, was es ist.

einige Punkte zu beachten:

  • Swing-Komponenten (JPanel, JButton, ...) sollte nur auf der Event Dispatch Thread (EDT) geändert werden - dies ist der Fall für Code in der Action ausgeführt
  • nach Änderungen Layout aus der validate Methode (Wieder-) Layout, um die Komponente
+0

Danke für den Hinweis. Ich habe das Problem gelöst, indem ich einfach hand1.validate() und hand1.repaint() am Ende der Schleife hinzugefügt habe. Jetzt funktioniert es genau wie beabsichtigt! – JustABoy

Verwandte Themen