Advertisement
underkuerbis

JavaFXWithJFrames.java

Jan 7th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. //package javafxwithjframes;
  7.  
  8. import javafx.application.Application;
  9. import javafx.event.ActionEvent;
  10. import javafx.event.EventHandler;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.layout.HBox;
  14. import javafx.stage.Stage;
  15. import javafx.stage.WindowEvent;
  16. import javax.swing.JFrame;
  17.  
  18. /**
  19.  * When starting, a JavaFX window and 3 Swing JFrame-windows appear.
  20.  * The JavaFX window features 3 buttons that each are supposed to close one of the JFrames.
  21.  * The program, however, freezes after clicking a button. What am I doing wrong?
  22.  * It seems to work on Windows, but crash on OS X 10.10.1, using Java 8u25 (latest release) and Java 8u40 Dec 31 preview-build.
  23.  *
  24.  * @author a desperate developer
  25.  */
  26. public class JavaFXWithJFrames extends Application {
  27.  
  28.     JFrame jframe1, jframe2, jframe3;
  29.  
  30.     @Override
  31.     public void start(Stage primaryStage) {
  32.  
  33.         // Create 3 JFrames
  34.         jframe1 = new JFrame("JFrame 1");
  35.         jframe1.setBounds(50, 50, 200, 150);
  36.         jframe1.setVisible(true);
  37.  
  38.         jframe2 = new JFrame("JFrame 2");
  39.         jframe2.setBounds(275, 50, 200, 150);
  40.         jframe2.setVisible(true);
  41.  
  42.         jframe3 = new JFrame("JFrame 3");
  43.         jframe3.setBounds(500, 50, 200, 150);
  44.         jframe3.setVisible(true);
  45.  
  46.         // Create 3 buttons that close each one JFrame
  47.         // Button 1
  48.         Button closeButton1 = new Button();
  49.         closeButton1.setText("Close JFrame 1");
  50.         closeButton1.setOnAction(new EventHandler<ActionEvent>() {
  51.             @Override
  52.             public void handle(ActionEvent event) {
  53.                 jframe1.setVisible(false);
  54.                 System.out.print("Closing JFrame 1...");
  55.                 jframe1.dispose();
  56.             }
  57.         });
  58.  
  59.         // Button 2
  60.         Button closeButton2 = new Button();
  61.         closeButton2.setText("Close JFrame 2");
  62.         closeButton2.setOnAction(new EventHandler<ActionEvent>() {
  63.             @Override
  64.             public void handle(ActionEvent event) {
  65.                 jframe2.setVisible(false);
  66.                 System.out.print("Closing JFrame 2...");
  67.                 jframe2.dispose();
  68.             }
  69.         });
  70.  
  71.         // Button 3
  72.         Button closeButton3 = new Button();
  73.         closeButton3.setText("Close JFrame 3");
  74.         closeButton3.setOnAction(new EventHandler<ActionEvent>() {
  75.             @Override
  76.             public void handle(ActionEvent event) {
  77.                 jframe3.setVisible(false);
  78.                 System.out.print("Closing JFrame 3...");
  79.                 jframe3.dispose();
  80.             }
  81.         });
  82.  
  83.         // Setting up main window
  84.         HBox rootBox = new HBox();
  85.         rootBox.getChildren().addAll(closeButton1, closeButton2, closeButton3);
  86.         Scene scene = new Scene(rootBox, 400, 250);
  87.         primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  88.             @Override
  89.             public void handle(WindowEvent event) {
  90.                 System.exit(0);
  91.             }
  92.         });
  93.         primaryStage.setTitle("JavaFX with JFrames");
  94.         primaryStage.setScene(scene);
  95.         primaryStage.show();
  96.     }
  97.  
  98.     /**
  99.      * @param args the command line arguments
  100.      */
  101.     public static void main(String[] args) {
  102.         launch(args);
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement