Skip to content

Instantly share code, notes, and snippets.

@gnusosa
Last active April 27, 2018 01:20
Show Gist options
  • Save gnusosa/775505310f5d7f322fc0439f623811fe to your computer and use it in GitHub Desktop.
Save gnusosa/775505310f5d7f322fc0439f623811fe to your computer and use it in GitHub Desktop.
Panagram JavaFX
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import java.util.HashSet;
public class Main extends Application {
public boolean isPanagram(String s) {
HashSet<Integer> h = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
int put = (int) s.charAt(i);
h.add(put);
}
}
if (h.size() == 26) {
return true;
} else {
return false;
}
}
@Override
public void start(Stage primaryStage) throws Exception{
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 600, 480 );
Text scenetitle = new Text("Let's check your Panagram!");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
Label panagramLabel = new Label("Panagram:");
grid.add(panagramLabel, 0, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 1);
Button btn = new Button("Check");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
final Text actionTarget = new Text();
grid.add(actionTarget, 1, 6);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
String panagramString = userTextField.getText();
if (isPanagram(panagramString)){
actionTarget.setFill(Color.FORESTGREEN);
actionTarget.setText("This is a panagram.");
} else {
actionTarget.setFill(Color.FIREBRICK);
actionTarget.setText("This is not a panagram.");
}
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("Panagram checker");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment