/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javafx_leike; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.StackPane; import javafx.scene.layout.TilePane; import javafx.scene.text.Text; import javafx.stage.Stage; public class JavaFX_Leike extends Application { int valinta = 0; String str =""; public void start(Stage primaryStage) { Text tulos = new Text("Teksti"); TextField teksti = new TextField(); TextField teksti_2 = new TextField(); Button nappain = new Button("Aseta"); Button nappain_2 = new Button("Aseta joka x. kirjain"); Button nappain_3 = new Button("Väärinpäin"); Button nappain_4 = new Button("Lisää leikepöytään"); Button nappain_5 = new Button("Lue leikepöytä"); Button nappain_6 = new Button("Fonttikoko"); TilePane r = new TilePane(); tulos.setStyle("-fx-font: 20 arial"); //Lisätään objektit. r.getChildren().add(tulos); r.getChildren().add(teksti); r.getChildren().add(teksti_2); r.getChildren().add(nappain); r.getChildren().add(nappain_2); r.getChildren().add(nappain_3); r.getChildren().add(nappain_4); r.getChildren().add(nappain_5); r.getChildren().add(nappain_6); EventHandler<ActionEvent> event = (ActionEvent e) -> { //Asetaan teksti leikepöydälle. String mjn = teksti.getText(); str = ""; StringSelection selection = new StringSelection(mjn); Clipboard leikepoyta = Toolkit.getDefaultToolkit().getSystemClipboard(); leikepoyta.setContents(selection,selection); }; EventHandler<ActionEvent> event_2 = (ActionEvent e) -> { //Asetaan x. kirjain. String mjn = teksti.getText(); str = ""; int x = Integer.valueOf(teksti_2.getText()); //Muodostetaan merkkijono. String t = ""; for (int i = 0; i < mjn.length(); i += x){ t += mjn.charAt(i); } StringSelection selection = new StringSelection(t); Clipboard leikepoyta = Toolkit.getDefaultToolkit().getSystemClipboard(); leikepoyta.setContents(selection,selection); }; EventHandler<ActionEvent> event_3 = (ActionEvent e) -> { //Laitetaan väärinpäin. String mjn = teksti.getText(); str = ""; //Muodostetaan merkkijono väärinpäin.. String t = ""; for (int i = mjn.length()-1; i >= 0; i--){ t += mjn.charAt(i); } StringSelection selection = new StringSelection(t); Clipboard leikepoyta = Toolkit.getDefaultToolkit().getSystemClipboard(); leikepoyta.setContents(selection,selection); }; EventHandler<ActionEvent> event_4 = (ActionEvent e) -> { //Lisätään merkkijonoon. str += teksti.getText(); StringSelection selection = new StringSelection(str); Clipboard leikepoyta = Toolkit.getDefaultToolkit().getSystemClipboard(); leikepoyta.setContents(selection,selection); }; EventHandler<ActionEvent> event_5 = (ActionEvent e) -> { String t = ""; //Luetaan leikepöydästä. try { t = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException ex) { Logger.getLogger(JavaFX_Leike.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JavaFX_Leike.class.getName()).log(Level.SEVERE, null, ex); } tulos.setText(t); }; EventHandler<ActionEvent> event_6 = (ActionEvent e) -> { //Muutetaan fonttikokoa. //Fonttikokoa on kaksi vaihtoehtoa. if (valinta == 0){ valinta = 1; tulos.setStyle("-fx-font: 20 arial"); } else { valinta = 0; tulos.setStyle("-fx-font: 40 arial"); } }; //Lisätään kuuntelijat nappain.setOnAction(event); nappain_2.setOnAction(event_2); nappain_3.setOnAction(event_3); nappain_4.setOnAction(event_4); nappain_5.setOnAction(event_5); nappain_6.setOnAction(event_6); Scene scene = new Scene(r, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
Aihe on jo aika vanha, joten et voi enää vastata siihen.