Java Code greift nicht auf GUI-Form zu

Max Mustermann

Grünschnabel
Hi,

hab einen Code für ein Würfelspiel geschrieben, der soweit auch funktioniert. Aber dann hab ich die Buttons in der GUI verschoben und ein wenig verändert und wenn ich jetzt auf einen Button klicke passiert einfach gar nichts...

Hier mein Code:

Code:
package paschenoriginal;

/**
 *
 * @author Philipp
 */
public class Paschenoriginal2 extends javax.swing.JFrame {

    /**
     * Creates new form Paschenoriginal2
     */
    public Paschenoriginal2() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        spieler2 = new javax.swing.JButton();
        spieler3 = new javax.swing.JButton();
        spieler1 = new javax.swing.JButton();
        spieler4 = new javax.swing.JButton();
        spieler5 = new javax.swing.JButton();
        tfergebnis1 = new javax.swing.JTextField();
        tfsumme1 = new javax.swing.JTextField();
        spieleranzahl = new javax.swing.JButton();
        tfspieleranzahl = new javax.swing.JTextField();
        tfergebnis3 = new javax.swing.JTextField();
        tfergebnis2 = new javax.swing.JTextField();
        tfsumme2 = new javax.swing.JTextField();
        tfsumme3 = new javax.swing.JTextField();
        tfsumme4 = new javax.swing.JTextField();
        tfsumme5 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setPreferredSize(new java.awt.Dimension(650, 650));

        spieler2.setText("Spieler 2:  Würfeln");
        spieler2.setPreferredSize(new java.awt.Dimension(125, 50));
        spieler2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spieler2ActionPerformed(evt);
            }
        });

        spieler3.setText("Spieler 3:  Würfeln");
        spieler3.setPreferredSize(new java.awt.Dimension(125, 50));
        spieler3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spieler3ActionPerformed(evt);
            }
        });

        spieler1.setLabel("Spieler 1:  Würfeln");
        spieler1.setPreferredSize(new java.awt.Dimension(125, 50));
        spieler1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spieler1ActionPerformed(evt);
            }
        });

        spieler4.setText("Spieler 4:  Würfeln");
        spieler4.setPreferredSize(new java.awt.Dimension(125, 50));
        spieler4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spieler4ActionPerformed(evt);
            }
        });

        spieler5.setText("Spieler 5:  Würfeln");
        spieler5.setPreferredSize(new java.awt.Dimension(125, 50));
        spieler5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spieler5ActionPerformed(evt);
            }
        });

        tfergebnis1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfergebnis1ActionPerformed(evt);
            }
        });

        tfsumme1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfsumme1ActionPerformed(evt);
            }
        });

        spieleranzahl.setText("Anzahl der Spieler");
        spieleranzahl.setToolTipText("");
        spieleranzahl.setPreferredSize(new java.awt.Dimension(120, 50));

        tfspieleranzahl.setPreferredSize(new java.awt.Dimension(60, 30));

        tfsumme2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfsumme2ActionPerformed(evt);
            }
        });

        tfsumme3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfsumme3ActionPerformed(evt);
            }
        });

        tfsumme4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfsumme4ActionPerformed(evt);
            }
        });

        tfsumme5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfsumme5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(spieler1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addComponent(spieler2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(spieleranzahl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(tfsumme1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(87, 87, 87)
                                .addComponent(tfsumme2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spieler3, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(tfsumme3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tfergebnis2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tfergebnis3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(tfergebnis1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(58, 58, 58))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(spieler4, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addComponent(spieler5, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(tfsumme4, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(tfsumme5, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50))))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(tfspieleranzahl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spieler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spieler2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spieler3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spieler4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spieler5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfsumme1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfsumme2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfsumme3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfsumme4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfsumme5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(54, 54, 54)
                .addComponent(tfergebnis1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spieleranzahl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfergebnis2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfspieleranzahl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(tfergebnis3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(247, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void tfergebnis1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                          

    private void tfsumme5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void tfsumme4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void tfsumme3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void tfsumme2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void tfsumme1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void spieler5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void spieler4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void spieler3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void spieler2ActionPerformed(java.awt.event.ActionEvent evt) {                                        

    }                                        

    private void spieler1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    /**
     * @param args the command line arguments
     */
   
     //Methoden für einzelnen Fälle  

   
   
       
   
  //Kontrolliert, ob alle 3 Zufallszahlen den gleichen Wert haben
  //z.B. 1-1-1, 2-2-2
  private int gibtEsEinenDrilling(int[] anzahlDesEreignisses){    
    for(int i = 0 ; i < anzahlDesEreignisses.length;i++){
      if(anzahlDesEreignisses[i] == 3){
        return i+1;
      }
    }
    return -1;
  }
  //Kontrolliert, ob die Zufallszahlen eine Straße bilden, z.B 1-2-3, 2-3-4
  private boolean gibtEsEineStraße(int[] anzahlDesEreignisses){
   
   
    for(int i=0;i<anzahlDesEreignisses.length-2;i++){
      if(anzahlDesEreignisses[i] == 1 && anzahlDesEreignisses[i+1] == 1 && anzahlDesEreignisses[i+2] == 1){
        return true;
      }
     
    }
   
    return false;
   
   
  }
       
  private boolean gibtEsElfer(int[] anzahlDesEreignisses){
   
    return anzahlDesEreignisses[4]==1 && anzahlDesEreignisses[2]==2;
   
  }
  private boolean gibtEsGold(int[] anzahlDesEreignisses){
    return anzahlDesEreignisses[0]==1 && anzahlDesEreignisses[4]==1 && anzahlDesEreignisses[5]==1;
  }
    private void klickButton(int spieler) { 
        javax.swing.JTextField field;
        if(spieler == 1){
            field = tfsumme1;            
        }else if(spieler == 2){
            field = tfsumme2;
        }else if (spieler == 3){
            field = tfsumme3;
        }else if (spieler == 4){
            field = tfsumme4;
        }else {
            field = tfsumme5;
        }
       
       
        int zufallszahl1, zufallszahl2, zufallszahl3;
       
        // Würfel 1-3
        zufallszahl1 = (int) (Math.random()*6)+1;
        zufallszahl2 = (int) (Math.random()*6)+1;
        zufallszahl3 = (int) (Math.random()*6)+1;
       
        // Summe der 3 Würfel 
        tfergebnis1.setText(""+zufallszahl1);
        tfergebnis2.setText(""+zufallszahl2);
        tfergebnis3.setText(""+zufallszahl3);
       
       
       
          int[] anzahlDesEreignisses = new int[6];  //<-------------------------------------------------------------
     
          anzahlDesEreignisses[zufallszahl1-1]++;
          anzahlDesEreignisses[zufallszahl2-1]++;
        anzahlDesEreignisses[zufallszahl3-1]++;
     
        //durchläuft die einzelnen Kombinationsmöglichkeitn durch 
        //und gibt die entsprechende aus
     
          int drilling = gibtEsEinenDrilling(anzahlDesEreignisses);    
        if(drilling >= 0){
          field.setText(drilling +"er Posch");
        }else if(gibtEsEineStraße(anzahlDesEreignisses)){
          field.setText("Straße");
        }else if(gibtEsElfer(anzahlDesEreignisses)){
          field.setText("Elfer");
        }else if(gibtEsGold(anzahlDesEreignisses)){
          field.setText("Gold");
        }else{
         
          int summe = 100*anzahlDesEreignisses[0];
          summe = summe + 60*anzahlDesEreignisses[5];//<<--
         
         
         
        for(int i = 1 ; i < 5 ; i++){
            summe=summe+anzahlDesEreignisses[i]*(i+1);
          }
           
         field.setText(""+summe);
           
        }}                                        
                                       
// TODO add your handling code here:
       
       
                                       

                                           

    /**
     * @param arg the command line arguments
     */ public static void main(String args[]) {
         /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Paschenoriginal2().setVisible(true);
            }
        });
    }
       
       
   
   
   

    // End of variables declaration                  

       
   

    // Variables declaration - do not modify                    
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton spieler1;
    private javax.swing.JButton spieler2;
    private javax.swing.JButton spieler3;
    private javax.swing.JButton spieler4;
    private javax.swing.JButton spieler5;
    private javax.swing.JButton spieleranzahl;
    private javax.swing.JTextField tfergebnis1;
    private javax.swing.JTextField tfergebnis2;
    private javax.swing.JTextField tfergebnis3;
    private javax.swing.JTextField tfspieleranzahl;
    private javax.swing.JTextField tfsumme1;
    private javax.swing.JTextField tfsumme2;
    private javax.swing.JTextField tfsumme3;
    private javax.swing.JTextField tfsumme4;
    private javax.swing.JTextField tfsumme5;
    // End of variables declaration                  


}

Ich glaube ich müsste bei den initComponents() bei den einzelnen Spieler den "klickbutton" hinzufügen.

Aber ich kann ich diesem Code ja nichts verändern, bitte um dringende Hilfe .

Grüße
 
Hi

was waren diese Änderungen an den Buttons?
Habs mittlerweile gelöst :)

Hätte aber noch eine Frage und zwar, wie kann ich ein Bild auf der GUI-Form einblenden?

Hab bis jetzt ein Label erstellt und diesen Code geschrieben:

Code:
 javax.swing.ImageIcon icon = new javax.swing.ImageIcon("\"C:\\Users\\Philipp\\Desktop\\Poschen.jpg\"");
        iconlabel.setIcon(icon);
        iconlabel.validate();

Zwischen den Apostrophes wäre der Pfad fürs Bild, aber es funktioniert nicht.

Weißt du, wo das Problem liegt?
 
Hallo,

die Zeile sollte eher so lauten
Code:
 javax.swing.ImageIcon icon = new javax.swing.ImageIcon("C:\\Users\\Philipp\\Desktop\\Poschen.jpg");

Viele Grüße
Sascha
 
Zurück