Java FXML-1 Formlar arası geçiş (Eclipse)

Merhaba arkadaşlar bugün sizlere javada 2 adet form oluşturma ve bu formlar arasında herhangi bir butona tıklandığında diğer forma geçmeyi görsel olarak anlatacağım.

İlk olarak Eclipse sol üstte bulunan pencerelerde şu adımları takip edin File->New->Class Burada bir adet java sınıfı oluşturuyoruz bunun sebebi oluşturulan görsel arayüzdeki buttonların ve diğer araç-gereçlerin java kodlarını bu kısımda yazacağız.

Daha sonra ise Class'ı oluşturduğumuz şekilde bir adet fxml dosyası oluşturuyoruz File -> New ->Fxml (Seçenekler arasında yoksa other->Fxml) 

Şimdi elimizde bir adet fxml bir adet de Java dosyası var

Bendeki ilk formun java ve fxml kodları şu şekildedir.

JAVA
1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package application;
import java.util.Random;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class giris {
  private int f;
  private int s;

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField tf_kadi;

    @FXML
    private PasswordField tf_sifre;

    @FXML
    private Slider sl_kontrol;

    @FXML
    private Label lbl_islem;

    @FXML
    private Label lbl_sonuc;

    @FXML
    private Button btn_giris;

    @FXML
    private Button btn_kayit;

    
    @FXML
    void btn_girisOnclick(ActionEvent event) throws IOException {
     
     int sonuc=Integer.parseInt(lbl_sonuc.getText());
    
     String kadi,sifre;
     kadi=tf_kadi.getText();
     sifre=tf_sifre.getText();
     
     
     if(f+s==sonuc)
     {
     try {
      vt baglan=new vt();
      Connection con=baglan.baglan();
      
      String query="select * from kullanicilar where eposta=? and sifre=?";
      PreparedStatement pstmt=con.prepareStatement(query);
      pstmt.setString(1, kadi);
      pstmt.setString(2, sifre);
      
      ResultSet rs=pstmt.executeQuery();
      
      if(rs.next()) {
       System.out.println(rs.getString("eposta")+kadi+rs.getString("sifre"));
          
          
        Stage stg=new Stage();
           
           AnchorPane root= (AnchorPane)FXMLLoader.load(getClass().getResource("icerde.fxml"));
           
           stg.setScene(new Scene(root,400,400));
           stg.show();
        
      

      }
      
         
  
     }catch (Exception ex) {
      System.out.println("Hata"+ex.getMessage());
     }
     }
     
    }

    @FXML
    void btn_kayitOnclick(ActionEvent event) {

    }

    @FXML
    void sl_slideControl(MouseEvent event) {
     double deger=sl_kontrol.getValue(); int a =(int) Math.floor(deger); lbl_sonuc.setText(a+"");

    }

    @FXML
    void initialize() {
     Random rnd=new Random();
      f=rnd.nextInt(10);
      s=rnd.nextInt(10);
     lbl_islem.setText(""+(f)+"+"+s+" =");
     
        assert tf_kadi != null : "fx:id=\"tf_kadi\" was not injected: check your FXML file 'giris.fxml'.";
        assert tf_sifre != null : "fx:id=\"tf_sifre\" was not injected: check your FXML file 'giris.fxml'.";
        assert sl_kontrol != null : "fx:id=\"sl_kontrol\" was not injected: check your FXML file 'giris.fxml'.";
        assert lbl_islem != null : "fx:id=\"lbl_islem\" was not injected: check your FXML file 'giris.fxml'.";
        assert lbl_sonuc != null : "fx:id=\"lbl_sonuc\" was not injected: check your FXML file 'giris.fxml'.";
        assert btn_giris != null : "fx:id=\"btn_giris\" was not injected: check your FXML file 'giris.fxml'.";
        assert btn_kayit != null : "fx:id=\"btn_kayit\" was not injected: check your FXML file 'giris.fxml'.";

    }
}

FXML
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<AnchorPane prefHeight="411.0" prefWidth="487.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.giris">
   <children>
      <GridPane layoutX="112.0" layoutY="59.0" prefHeight="117.0" prefWidth="240.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Kullanıcı Adı" />
            <Label text="Şifre" GridPane.rowIndex="1" />
            <TextField fx:id="tf_kadi" GridPane.columnIndex="1" />
            <PasswordField fx:id="tf_sifre" GridPane.columnIndex="1" GridPane.rowIndex="1" />
         </children>
      </GridPane>
      <Slider fx:id="sl_kontrol" blockIncrement="1.0" layoutX="104.0" layoutY="207.0" max="20.0" onMouseClicked="#sl_slideControl" onMouseDragged="#sl_slideControl" prefHeight="14.0" prefWidth="257.0" />
      <Label fx:id="lbl_islem" layoutX="112.0" layoutY="184.0" text="10+10 =" />
      <Label fx:id="lbl_sonuc" layoutX="166.0" layoutY="184.0" text="0" />
      <Button fx:id="btn_giris" layoutX="105.0" layoutY="231.0" mnemonicParsing="false" onAction="#btn_girisOnclick" prefHeight="58.0" prefWidth="257.0" text="Giriş Yap" />
      <Button fx:id="btn_kayit" layoutX="104.0" layoutY="308.0" mnemonicParsing="false" onAction="#btn_kayitOnclick" prefHeight="58.0" prefWidth="257.0" text="Kayıt Ol" />
   </children>
</AnchorPane>


Burda önemli olan husus XML kodunda işaretlemiş olduğum satırdaki

fx:controller="application.giris"

Formun butonlarını ve diğer araç-gereçlerini kontrol edecek olan Java Class'ını belirtmemiz gerekiyor.

Şimdi aynı şekilde birer tane daha fxml ve java dosyaları oluşturuyoruz.

Gerekli Controller Tanımlaması yapıldıktan sonra butonumuzun click eventini açıyoruz

ve içine üstte de verildiği gibi.

Stage stg=new Stage();
AnchorPane root= (AnchorPane)FXMLLoader.load(getClass().getResource("icerde.fxml"));
stg.setScene(new Scene(root,400,400));
stg.show();

Bir adet stage oluşturuyoruz ve bu oluşturduğumuz stage'in Paneini 2. satırda görüldüğü gibi tanımlıyoruz. Daha sonra ise SetScene adlı fonksiyon ile sahnemizin boyutlarını belirliyoruz.Son olarak da oluşturduğumuz stage'i show() fonksiyonu ile gösteriyoruz.

Sorularınızı yorum kısmından belirtebilirsiniz

Yorumlar

Bu blogdaki popüler yayınlar

.Net 6.0 ile ModelState kullanımı (RedirectToAction)

Delegate Prediction Func C#

Açıklama