AWT Werte aus NumberField vergleichen

rgamer

Mitglied
Guten Tag alle zusammen,

Ich habe schon das halbe Internet durchforstet, aber nichts gefunden, was mir irgendwie weiterhelfen könnte.
Ich schreibe ein Lottoprogramm, wo ich in NumberField (oder auch TextField, funktioniert jedoch beides nicht), Werte eingeben kann.

Mit einem Button bestätige ich dann die Eingabe.
Wenn man auf diesen button drückt sollen die Werte aus dem NumberField nun verglichen werden, ob sie größer 50 und größer 15 sind und wenn das der Fall ist soll ein dialogfeld 1 aufgerufen werden.
In allen anderen Fällen das dialogfeld 2.

Mein Problem ist, dass er immer dialogfeld 2 aufruft, egal was ich eintippe, obwohl ich die If-Anfrage gesetzt hab.

Code:
Java:
import java.awt.*;
import java.awt.event.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 14.02.2014
  * @author 
  */

public class LottoautomatDLGJL extends Dialog {
  
  // Anfang Attribute
  private FlowLayout flo = new FlowLayout();
  
  public Frame f = new Frame();
  public Frame f2 = new Frame();
  
  private Label l1 = new Label ("Julian's special Lotto");
  private Label l2 = new Label ("Wählen Sie hier ihre Zahlen");
  private Label l3 = new Label ("Wählen Sie hier ihre Superzahlen");
  
  private NumberField tf1 = new NumberField();
  private NumberField tf2 = new NumberField();
  private NumberField tf3 = new NumberField();
  private NumberField tf4 = new NumberField();
  private NumberField tf5 = new NumberField();
  private NumberField tf6 = new NumberField();
  private NumberField tf7 = new NumberField();
  
  private Button b1 = new Button("Bestätigen und Spielen");
  
  public int zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahl7;
  
  
  
  

  // Ende Attribute
  
  public LottoautomatDLGJL(Frame owner, String title, boolean modal) { 
    
    // Dialog-Initialisierung
    super(owner, title, modal);
    setBounds(100,100,800,600);
    setLayout(null);
    setBackground(Color.green);
    
    //Fenster schließen
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { dispose(); }
    });
    
    
    Font font1 = new Font ("Arial", 2,24);     
    add(l1);
    l1.setBounds(50,25,400,50);
    l1.setFont(font1);
    
    Font font2 = new Font ("Arial", 1,20);     
    add(l2);
    l2.setBounds(50,100,400,30);
    l2.setFont(font2);
    
    
    
    add(tf1);
    tf1.setBounds(50,150,20,20);
    add(tf2);
    tf2.setBounds(75,150,20,20);
    add(tf3);
    tf3.setBounds(100,150,20,20);
    add(tf4);
    tf4.setBounds(125,150,20,20);
    
    
    
    add(l3);
    l3.setBounds(50,225,400,30);
    l3.setFont(font2);
    
    add(tf5);
    tf5.setBounds(50,275,20,20);
    add(tf6);
    tf6.setBounds(50,325,20,20);
    add(tf7);
    tf7.setBounds(50,375,20,20);    
    
    
    add(b1);
    b1.setBounds(50,350,250,50);
    b1.addActionListener (new ActionListener(){;
      public void actionPerformed (ActionEvent e)
      {
        tf1.getInt();
        tf2.getInt();
        tf3.getInt();
        tf4.getInt();
        tf5.getInt();
        tf6.getInt();
        tf7.getInt();
        
        if (zahl1>50 || zahl2>50 || zahl3>50|| zahl4>50 || zahl5>15 || zahl6>15 || zahl7>15) 
        {
          LottoautomatDLG3 lod1 = new LottoautomatDLG3(f,"Ziehung",true);
        } // end of if
        else
        {
        LottoautomatDLG4 lod2 = new LottoautomatDLG4(f2,"Ziehung",true);
        }
        
      }
    });
    
    setResizable(false);
    setVisible(true);
  } // end of public LottoautomatDLG6a49
  
  
  
} // end of class DatenlayoutDLG
 
Zuletzt bearbeitet:
Hast recht, die hab ich nicht.

Wenn ich jetzt jedoch in der Hauptklasse, wo ich die Variablen zahl1 - zahl6 definiere, diese jeweils =0 setze, hab ich diesen eine Zahl zugewiesen.
Die Frage ist nur, wie überschreibt er jetzt die Variablen, wenn ich sie eintippe?
Muss ich dann mit tf1.setText(zahl1); arbeiten?
 
Mit was für einer Entwicklungsumgebung arbeitest du eigentlich? Du müßtest doch hunderte von Warnings in Deinem Code bekommen.

Ich kenne AWT nicht aber ich schätze mal zu getInt() wird es sicher eine Dokumentation geben die man sich durchlesen kann?

Gruß

Claus
 
Arbeiten in Java-Editor und dessen API/Dokumentation gibt nichts konkret über getInt her, lediglich über das AWT allgemein.
Das ist halt dieses Schulzeugs

Edit:
Bei dem Fenster bekomm ich keine Warnung.

Warnungen bekomm ich wo anders, das ist aber ein anderes Thema.
 
Zuletzt bearbeitet:
Ausprobiert doch leider das gleiche Ergebnis. Es ist als ob die komplette if anfrage einfach ignoriert wird und automatisch einfach das erste dialogfenster geöffnet wird "LottoautomatDLG3".
Es ist völlig egal was ich eingebe, das wird geöffnet. Hier mal meine überarbeitete version.
Java:
import java.awt.*;
import java.awt.event.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 14.02.2014
  * @author 
  */

public class LottoautomatDLG6a49 extends Dialog {
  
  // Anfang Attribute
  private FlowLayout flo = new FlowLayout();
  
  public Frame f = new Frame();
  public Frame f2 = new Frame();
  
  private Label l1 = new Label ("6 aus 49");
  private Label l2 = new Label ("Wählen Sie hier ihre Zahlen");
  private Label l3 = new Label ("Wählen Sie hier ihre Superzahl");
  
  public NumberField tf1 = new NumberField();
  public NumberField tf2 = new NumberField();
  public NumberField tf3 = new NumberField();
  public NumberField tf4 = new NumberField();
  public NumberField tf5 = new NumberField();
  public NumberField tf6 = new NumberField();  
  public NumberField tf7 = new NumberField();
  
  private Button b1 = new Button("Bestätigen und Spielen");
  
  public int zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahl7;
  
  
  

  // Ende Attribute
  
  public LottoautomatDLG6a49(Frame owner, String title, boolean modal) { 
    
    // Dialog-Initialisierung
    super(owner, title, modal);
    setBounds(100,100,800,600);
    setLayout(null);
    setBackground(Color.green);
    
    //Fenster schließen
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { dispose(); }
    });
    
    
    Font font1 = new Font ("Arial", 2,24);     
    add(l1);
    l1.setBounds(50,25,100,50);
    l1.setFont(font1);
    
    Font font2 = new Font ("Arial", 1,20);     
    add(l2);
    l2.setBounds(50,100,400,30);
    l2.setFont(font2);
    
    
    
    add(tf1);
    tf1.setBounds(50,150,20,20);
    add(tf2);
    tf2.setBounds(75,150,20,20);
    add(tf3);
    tf3.setBounds(100,150,20,20);
    add(tf4);
    tf4.setBounds(125,150,20,20);
    add(tf5);
    tf5.setBounds(150,150,20,20);
    add(tf6);
    tf6.setBounds(175,150,20,20);
    
    add(l3);
    l3.setBounds(50,225,400,30);
    l3.setFont(font2);
    
    add(tf7);
    tf7.setBounds(50,275,20,20);
    
    add(b1);
    b1.setBounds(50,350,250,50);
    b1.addActionListener (new ActionListener(){;
      public void actionPerformed (ActionEvent e)
      {
        
        zahl1 = tf1.getInt();
        zahl2 = tf1.getInt();
        zahl3 = tf1.getInt();
        zahl4 = tf1.getInt();
        zahl5 = tf1.getInt();
        zahl6 = tf1.getInt();
        zahl7 = tf1.getInt();
        
        if (zahl1 == zahl2 || zahl1 == zahl3 || zahl1 == zahl4 ||
        zahl1 == zahl5 || zahl1 == zahl6 ) 
        {
          LottoautomatDLG3 lod = new LottoautomatDLG3 (f,"Fehlerhafte Eingabe",true);
        } // end of if
        else 
        {
          LottoautomatDLG4 lod2 = new LottoautomatDLG4 (f2,"Eingabe bestätigt",true);
        } // end of if-else
        
        
        
        
      }
    });
    
    
    
    
    
    
    
    
    
    
    // Anfang Komponenten
    
    // Ende Komponenten
    
    setResizable(false);
    setVisible(true);
  } // end of public LottoautomatDLG6a49
  
  
  
} // end of class DatenlayoutDLG

Edit: Hab den Fehler gefunden und nun funktionierts. habe beim kopieren von tf* fehler gemacht und da stand immer tf1
 
Zuletzt bearbeitet:
mir ist aufgefallen, dass du nur ein Teil deiner Eingaben prüfst, dir fehlt die komplette prüfung ob alle von tf2 gleich tf3, tf4, tf5, tf6; tf3 gleich tf4, tf5, tf6; tf4 gleich tf5, tf6; und tf5 gleich tf6 sind

Mein vorschlag ist;

also als Lottozahlgenerator kannst du ja so was schreiben

Java:
boolean[] zahlen =new boolean[ 50]; //tipp vom spieler über klick auf label
boolean[] ziehung =new boolean[ 50];
for( int i =0; i <zahlen.length; i++){
    zahlen[ i] =false;
    ziehung[ i] =false;
}

//Ziehung der Zahl vorher über die oberfläche noch den tipp einlesen
int kugelNr =0;
while( kugelNr <=6){
    int lottozahl= (int)(Math.random()*49 + 1);
    if( zahlen[ lottozahl] ==false){
         zahlen[ lottozahl] = true;
         kugelNr++;
    } else{}
}

und gegen diese array kannst du dann deine eingaben testen und wenn du diese in ein Array packst geht es auch schneller
Java:
int richtige =0;
for( int i=0; i <6; i++){
   if( zahlen[ tipp[ i]] == true){
       richtige++; //vlt auch das textfeld einfärben oder was auch immer
   }
}

für deinen Tipp kannst du labels in einem JLabel[][] mit den lottozahlen drauf generien und einen Mouslistener adden, der dir die zahl in ein Array schreibt und label.removeMouseListener(); ausführt wenn das Feld geklickt wurde.

das ist eigentlich ganz simpel

Grüße
 
Zuletzt bearbeitet:
Hab mal was zusammengestellt, vlt kannst du damit arbeiten. hat noch ein paar kleine bugs drin aber funktioniert schon soweit, dass man einen tipp abgeben kann und die ziehung ausgegeben wird. Leider wird manchmal in der Ziehung ein Erfolg verzeichnet, obwohl die Zahl nicht getippt worden ist.
[WR] einklassendesign[/WR]

Java:
package lotto;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import static lotto.gui.tipp;
import static lotto.gui.tippschein;

/**
 *
 * @author Christian
 */
public class gui {
    static MouseClickListener mcl =new MouseClickListener();
    
    public static JLabel[][] tippschein =new JLabel[ 7][ 7];
    public static int[] tipp =new int[ 6];
    public static int tippcounter =0;
    
    public static JLabel[] ziehungLabel =new JLabel[ 6];
    
    public static JButton ziehen =new JButton( "Schauen ob gewonnen");
    
    
    static void initTippschein(){
        int feldnr =1;
        for( int row =0; row <tippschein.length; row++){
            for( int col =0; col <tippschein[ row].length; col++){
                tippschein[ row][ col] =new JLabel( "" +feldnr);
                tippschein[ row][ col].addMouseListener( mcl);
                feldnr++;
            }
        }
    }
    
    static void initZiehung(){
        for( int i =0; i <ziehungLabel.length; i++){
            ziehungLabel[ i] =new JLabel( " 0 ");
        }
    }
    
    static void auslosung(){
        boolean[] zahlen =new boolean[ 50];
        for( int i =0; i <zahlen.length; i++){
            zahlen[ i] =false;
        }

        int kugelNr =0;
        while( kugelNr <6){
            int lottozahl= (int)(Math.random()*49 + 1);
            if( zahlen[ lottozahl] ==false){
                 zahlen[ lottozahl] = true;
                 ziehungLabel[ kugelNr].setText( " |" +lottozahl +" ");
                 kugelNr++;
            } else{}
        }

        for( int i =0; i <6; i++){
            if( zahlen[ tipp[ i]] ==true){
                 ziehungLabel[ i].setBorder( new LineBorder( Color.GREEN));
            } else{
                ziehungLabel[ i].setBorder( null);
            }
        }
    }
    
    public static void removeMouse(){
        for( int row =0; row <tippschein.length; row++){
            for( int col =0; col <tippschein[ row].length; col++){
                tippschein[ row][ col].removeAll();
                tippschein[ row][ col].removeMouseListener( mcl);
            }
        }
    }
    
    
    public static void start(){
        JFrame frame =new JFrame( "Lottotipp");
        JPanel spielfeld =new JPanel();
        JPanel lottoTipp =new JPanel();
        JPanel ziehung =new JPanel();
        JPanel buttons =new JPanel();
        
        initTippschein();
        initZiehung();
        
        GridBagConstraints gbc =new GridBagConstraints();
        lottoTipp.setLayout( new GridBagLayout());
        gbc.insets =new Insets( 5, 5, 5, 5);
        
        gbc.gridy =0;
        for( int row =0; row <tippschein.length; row++){
            gbc.gridx =0;
            for( int col =0; col <tippschein[ row].length; col++){
                lottoTipp.add( tippschein[ row][ col], gbc);
                gbc.gridx++;
            }
            gbc.gridy++;
        }
        
        ziehung.setLayout( new GridBagLayout());
        gbc.gridy =0;
        for( gbc.gridx =0; gbc.gridx <ziehungLabel.length; gbc.gridx++){
            ziehung.add( ziehungLabel[ gbc.gridx]);
        }
        
        ziehen.addActionListener( new ActionListener(){
           @Override
           public void actionPerformed( ActionEvent e){
               auslosung();
           }
        });
        
        buttons.add( ziehen);
        Box box =Box.createVerticalBox();
        box.add(lottoTipp);
        box.add( ziehung);
        box.add( buttons);
        
        spielfeld.add( box);
        
        frame.add( spielfeld);
        frame.setVisible(true);
        frame.pack();
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
                
        
        
    }
    public static void main( String[] args){
        start();
    }
    
    
}

class MouseClickListener extends MouseAdapter {
    private int tippcounter =0;
    @Override
    public void mouseClicked( MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)){
            Object obj =e.getSource();
            if( e.getSource() instanceof JLabel){
                int hash =obj.hashCode();
                    for( int rows =0; rows < tippschein.length; rows++){
                        for( int cols =0; cols <tippschein[ rows].length; cols++){
                            if( tippschein[ rows][ cols].hashCode() ==hash){
                                tippschein[ rows][ cols].setOpaque(true);
                                tippschein[ rows][ cols].setBackground( Color.green);
                                
                                break;
                            } 
                        }
                    }
                String s =(( JLabel) obj).getText();
                System.out.println( s);
                tipp[ tippcounter] =Integer.parseInt( s);
                tippcounter++;
                if( tippcounter ==6){
                    gui.removeMouse();
                }
            }
        }
    }
}
 
Dankeschön für die Hilfe nochmal.

Ich habe mittlerweile selbst auch noch eine Lösung gefunden. Damit ist das hier auch dann für mich erledigt.

Java:
public class LottoautomatDLG6a49 extends Dialog {
  
  // Anfang Attribute
  private FlowLayout flo = new FlowLayout();
  
  public Frame f = new Frame();
  public Frame f2 = new Frame();
  
  private Label l1 = new Label ("6 aus 49");
  private Label l2 = new Label ("Wählen Sie hier ihre Zahlen");
  private Label l3 = new Label ("Wählen Sie hier ihre Superzahl");
  
  public NumberField tf1 = new NumberField();
  public NumberField tf2 = new NumberField();
  public NumberField tf3 = new NumberField();
  public NumberField tf4 = new NumberField();
  public NumberField tf5 = new NumberField();
  public NumberField tf6 = new NumberField();  
  public NumberField tf7 = new NumberField();

  private Button b1 = new Button("Bestätigen und Spielen");
  
  public int zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahl7;
  
  
  

  // Ende Attribute
  
  public LottoautomatDLG6a49(Frame owner, String title, boolean modal) { 
    
    // Dialog-Initialisierung
    super(owner, title, modal);
    setBounds(100,100,800,600);
    setLayout(null);
    setBackground(Color.green);
    
    //Fenster schließen
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { dispose(); }
    });
    
    
    Font font1 = new Font ("Arial", 2,24);     
    add(l1);
    l1.setBounds(50,25,100,50);
    l1.setFont(font1);
    
    Font font2 = new Font ("Arial", 1,20);     
    add(l2);
    l2.setBounds(50,100,400,30);
    l2.setFont(font2);
    
    
    
    add(tf1);
    tf1.setBounds(50,150,20,20);
    add(tf2);
    tf2.setBounds(75,150,20,20);
    add(tf3);
    tf3.setBounds(100,150,20,20);
    add(tf4);
    tf4.setBounds(125,150,20,20);
    add(tf5);
    tf5.setBounds(150,150,20,20);
    add(tf6);
    tf6.setBounds(175,150,20,20);
    
    add(l3);
    l3.setBounds(50,225,400,30);
    l3.setFont(font2);
    
    add(tf7);
    tf7.setBounds(50,275,20,20);
    
    
    
    add(b1);
    b1.setBounds(50,350,250,50);
    b1.addActionListener (new ActionListener(){;
      public void actionPerformed (ActionEvent e)
      {
        
        zahl1 = tf1.getInt();
        zahl2 = tf2.getInt();
        zahl3 = tf3.getInt();
        zahl4 = tf4.getInt();
        zahl5 = tf5.getInt();
        zahl6 = tf6.getInt();
        zahl7 = tf7.getInt();
        
        if (zahl1 == zahl2 || zahl1 == zahl3 || zahl1 == zahl4 || zahl1 == zahl5 ||
        zahl1 == zahl6 || zahl2 == zahl3 || zahl2 == zahl4 || zahl2 == zahl5 ||
        zahl2 == zahl6 || zahl3 == zahl4 || zahl3 == zahl5 || zahl3 == zahl6 ||
        zahl4 == zahl5 || zahl4 == zahl6 || zahl5 == zahl6 || zahl1 >49 ||
        zahl2 >49 || zahl3 >49 || zahl4 >49 || zahl5 >49 || zahl6 >49 || zahl7 >9 ||
        zahl1 <1 || zahl2 <1 || zahl3 <1 || zahl4 <1 || zahl5 <1 || zahl6 <1 || zahl7 <1 ) 
        {
          LottoautomatDLG3 lod = new LottoautomatDLG3 (f,"Fehlerhafte Eingabe",true);
        } //end of if
        /*if (zahl1 >49 || zahl2 >49 || zahl3 >49 || zahl4 >49 || zahl5 >49 || zahl6 >49 || zahl7 >9) 
        {
          LottoautomatDLG3 lod = new LottoautomatDLG3 (f,"Fehlerhafte Eingabe",true);
        } //end of if
        if (zahl1 <1 || zahl2 <1 || zahl3 <1 || zahl4 <1 || zahl5 <1 || zahl6 <1 || zahl7 <1) 
        {
          LottoautomatDLG3 lod = new LottoautomatDLG3 (f,"Fehlerhafte Eingabe",true);
        } //end of if   */
        else 
        {
          LottoautomatDLGBestätigung lod2 = new LottoautomatDLGBestätigung (f2,"Eingabe bestätigt",true,
          zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahl7);
        } // end of if-else
        
        
        
        
      }
    });
    
    
    
    // Anfang Komponenten
    
    // Ende Komponenten
    
    setResizable(false);
    setVisible(true);
  } // end of public LottoautomatDLG6a49
  
  
  
} // end of class DatenlayoutDLG
 

Zurück
Oben