silveira neto

carbon-based lifeform. virgo supercluster

Menu Close

JavaFX, Exemplos Básicos

Alguns exemplo básicos de JavaFX usando a construção de interfaces de forma declarativa.
Para testa-los eu recomendo o JavaFX Pad ou o plugin JavaFX para Netbeans.

import javafx.ui.*; 
 
Frame {
    title: "Label JavaFX"
    width:  300
    height: 50
    content: Label {
            text: "Olá Mundo!"
    }
    visible: true   
}

JavaFX label

import javafx.ui.*;
import java.lang.System;
 
Frame {
    title: "Botão JavaFX"
    width:  300
    height: 100
    content: Button {
           text: "Clique-me"
           action: operation(){
              System.out.println("Botão pressionado");
           }
    }
    visible: true   
}

Botão em JavaFX

import javafx.ui.*;
import java.lang.System;
 
Frame {
  title: "Menu JavaFX"
  width:  300
  height: 100
  menubar: MenuBar {
    menus: Menu {
      text: "Menu"
      items:  foreach (name in ["Menu1", "Menu2", "Menu3"])  
              MenuItem {
                text: name
                action: operation() {
                  System.out.println("MenuItem: {name}");
                }
             }
     }
  }
    visible: true
}

JavaFX Menu

import javafx.ui.*;
import java.lang.System;
 
var N = 4;
 
Frame {
    title: "Tabela JavaFX"
    width:  300
    height: 150
    onClose: operation(){ System.exit(0); }
    content: Table {
        columns: [
        TableColumn {
            text: "numero"
        },
        TableColumn {
            text: "quadrado"
        },
        TableColumn {
            text: "cubo"
        }]
 
        cells: bind foreach(n in [1..N])[
        TableCell {
            text: "{n}"
        },
        TableCell {
            text: bind "{n * n}"
        },
        TableCell {
            text: bind "{n * n * n}"
        },
        ]
    }
    visible: true
}

JavaFX Tabela

import javafx.ui.*;
 
var selectedTab = 0;
 
Frame{
    title: "Tab Example"
    width: 300
    height: 120
    content: BorderPanel{
        top: Label { text: bind "Selected tab: {selectedTab + 1}" }
        center: TabbedPane{
            selectedIndex: bind selectedTab
            tabs: foreach(i in [1..5])
            Tab {
                title: "Tab{i}"
                content: Label{ text: "Label{i} "}
            }            
        }
    }
    visible: true
}

JavaFX abas

import javafx.ui.*;
 
Frame {
    title: "FlowPanel JavaFX"
    width:  300
    height: 100
    content: FlowPanel{
        content: [
        Label{ text: "Label1" },
        Label{ text: "Label2" },
        Label{ text: "Label3" },
        ]
    }
    visible: true
}

JavaFX FlowPanel

import javafx.ui.*;
 
Frame {
    title: "BorderPanel JavaFX"
    width:  400
    height: 200
    content: BorderPanel{
        top   :  Button{ text: "Topo" }
        center:  Button{ text: "Centro" }
        bottom:  Button{ text: "Fundo" }
        left  :  Button{ text: "Esquerda" }
        right :  Button{ text: "Direita" }
    }
    visible: true
}

JavaFX BorderPanel

Esses exemplos eu retirei da página de exemplos do Wiki do JavaFX (russo). Se você quiser saber mais sobre componentes de interface gráfica em JavaFX veja o tutorial
Learning More About the JavaFX Script Language (for Swing Programmers).

© 2016 silveira neto. All rights reserved.

Theme by Anders Norén.