100% found this document useful (1 vote)
224 views

Javafx - Application - Digital Assignment

JavaFX controls, menus and events allows for building rich user interfaces. It includes common UI elements like labels, buttons, text fields and menus. Events allow applications to respond to user interactions, like clicking buttons or selecting menu items. The document demonstrates creating a simple JavaFX application with a menu bar and buttons that change the text of a label when clicked.

Uploaded by

Sanjana chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
224 views

Javafx - Application - Digital Assignment

JavaFX controls, menus and events allows for building rich user interfaces. It includes common UI elements like labels, buttons, text fields and menus. Events allow applications to respond to user interactions, like clicking buttons or selecting menu items. The document demonstrates creating a simple JavaFX application with a menu bar and buttons that change the text of a label when clicked.

Uploaded by

Sanjana chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaFX controls, menus and events

1. JAVA FX:

JavaFX is widely used to deploy client-side applications to provide rich


user interface. Its libraries consist of a set of graphics and media
packages. For designing, testing and creating web applications.

JavaFx Controls

It consists of user interface elements. It includes UI


elements,Layouts,behaviour. Theses are the visual components that user
interact with. Commonly used controls are Label, Button, Colour Picker,
CheckBox,radioButton,TextField,Buttons,Slider,e.t.c

JavaFX Menu

JavaFX consists of a separate MenuClass to display Menu.  In


JavaFX, javafx.scene.control.Menu class provides all the methods to
deal with menus. This class needs to be instantiated to create a Menu.

JavaFX Event Handling


In JavaFX, we can develop GUI applications, web applications and graphical
applications. In such applications, whenever a user interacts with the
application (nodes), an event is said to have been occurred.
For example, clicking on a button, moving the mouse, entering a character
through keyboard, selecting an item from list, scrolling the page are the
activities that causes an event to happen.
Foreground events are the most common events.
2) Webpage

The webpage is created using JavaFX in Eclipse IDE. There is a Menu Bar
at the top, which is Created using MenuBar object. There are two
Buttons at the bottom of the page, on clicking, the text gets
displayed.setOnAction() method is used to perform this Action.

Screenshot:
Source Code:

import com.sun.prism.paint.Color;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class MyApplication extends Application{

Label message;

@Override

public void start(Stage primaryStage){


BorderPane root= new BorderPane();

MenuBar menuBar = new MenuBar();


menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.setTop(menuBar);

Menu FileMenu = new Menu("File");


MenuItem copyMenuItem = new MenuItem("New");
MenuItem cutMenuItem = new MenuItem("Open File");
MenuItem pasteMenuItem = new MenuItem("Save as");
MenuItem exitMenuItem = new MenuItem("Close");

Menu FileMenu1 = new Menu("Edit");

exitMenuItem.setOnAction(actionEvent->Platform.exit());

menuBar.getMenus().addAll(FileMenu);

FileMenu.getItems().addAll(copyMenuItem,cutMenuItem,pasteMenuItem,
new SeparatorMenuItem(), exitMenuItem);

exitMenuItem.setOnAction(actionEvent->Platform.exit());

menuBar.getMenus().addAll(FileMenu1);

message = new Label("");


Button btn1=new Button();
Button btn2 = new Button();

btn1.setText("Topic");
btn2.setText("Name");

btn1.setOnAction(new EventHandler<ActionEvent>(){

@Override

public void handle(ActionEvent event){


message.setText("JAVAFX Assignment");
message.setFont(Font.font("Monotype Corsiva",FontWeight.BOLD,50));

}
);
btn2.setOnAction((ActionEvent e)->{
message.setText("Sanjana");
message.setFont(Font.font("Monotype Corsiva",FontWeight.BOLD,60));

});

root.setCenter(message);
HBox buttons = new HBox();

buttons.getChildren().addAll(btn1,btn2);

root.setBottom(buttons);
root.setTop(menuBar);

Scene scene = new Scene(root,700,400);

primaryStage.setTitle("My Application");
primaryStage.setScene(scene);
primaryStage.show();

public static void main(String[] args){


launch(args);

}
}

You might also like