Ahmadjonov Elbek Muhammadjon o’g LI


Download 23.06 Kb.
Sana18.06.2023
Hajmi23.06 Kb.
#1588364
Bog'liq
blaknot


F.I.O

Ahmadjonov Elbek Muhammadjon o’g”li

Guruh

Dasturiy Injiniring 651-22

3-Topshiriq: Java dasturlash tilining JavaFX moduli yordamida Blaknot yaratish

Ishdan maqsad: Java dagi Javafx moduli kompanentalarini o’rganish.
Kerakli jixozlar : Kompyuter , printer ,sichqoncha ,klaviatura ,
Nazariy qism:
JavaFX, Java dasturchilarni yangi engil, yuqori performanslı grafikalar platformasi bilan ta'minlash uchun mo'ljallangan. Dastur yangi ilovalar uchun dasturning grafik foydalanuvchi interfeysi (GUI) ni yaratish uchun Swing o'rniga JavaFX dan foydalanishga mo'ljallangan. Bu degani emas, degani emas. Swing-dan foydalanib yaratilgan ko'plab amaliy dasturlar hali Java API ning bir qismi bo'lishini anglatadi.
Ayniqsa, bu ilovalar JavaFX funksiyasini o'z ichiga olishi mumkin, chunki ikkita grafik API yonma-yon yonma-yon ishlaydi.
JavaFX har qanday platforma uchun grafik foydalanuvchi interfeyslarini yaratish uchun ishlatilishi mumkin (masalan, ish stoli, veb, mobil va hokazo).
JavaFX dastur kodi har qanday Java kutubxona API mos yozuvlar mumkin. Misol uchun, JavaFX ilovalar ona tizim imkoniyatlarini kirish uchun Java API kutubxona foydalanishingiz mumkin va server asoslangan qavatni ilovalar ulanish.

JavaFX ilovalar versiya va his tayyorlangan bo'lishi mumkin. Uslublar kaskadli jadvallari (CSS) chiquvchilar kodlash joyga jamlanganda mumkin, shunday qilib amalga oshirish alohida ko'rinishi va uslublar. Grafik Dizaynerlar osonlikcha CSS orqali dastur tashqi ko'rinishini va xohishlarini sozlashingiz mumkin. Agar veb-dizayn fon bor, yoki siz foydalanuvchi interfeysi (UI) va oxirgi qism mantiq ajratib istayman bo'lsa, unda siz FXML scripting tilida UI taqdimot jihatlarini rivojlantirish va qo'llash uchun Java kodi foydalanishingiz mumkin bo'lsa mantiq. Agar kod yozmasdan Uis loyihalashtirish afzal bo'lsa, keyin JavaFX Scene Builder foydalaning. Agar UI loyihalashtirish kabi, Scene Builder chiquvchilar ish mantig'ini qo'shishingiz mumkin, shunday qilib, bir Integrated Development Environment (IDE) ga ko'chirildi mumkin FXML ustamasi yaratadi.


Amaliy qism:
package com.example.textdocument;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.print.PrinterJob;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloController {
FileChooser fileChooser = new FileChooser();
@FXML
private TextArea FIleContent;
private java.io.File file;
@FXML
private AnchorPane Layoutpane;
String selectedText;
@FXML
void OnMenuOpen(ActionEvent event) throws FileNotFoundException {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open File");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Txt Fayllar", "*.txt"));
file = fileChooser.showOpenDialog(null);
if (file != null) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder content = new StringBuilder();
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
FIleContent.setText(content.toString());
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@FXML
void OnMenuSave() throws IOException {
if (file == null) {
saveAsFile();
} else {
try {
FileWriter writer = new FileWriter(file);
writer.write(FIleContent.getText());
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@FXML
private void saveAsFile() throws IOException {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Faylni saqlash");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Txt Fayllar", "*.txt")
);
file = fileChooser.showSaveDialog(null);
if (file != null) {
OnMenuSave();
}
}
@FXML
void OnCloseMenu(ActionEvent event) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Chiqish");
alert.setHeaderText("Chiqish");
alert.setContentText("Chiqishdan oldin saqlashni hohlaysizmi??");
ButtonType saveButtonType = new ButtonType("Save");
ButtonType exitButtonType = new ButtonType("Exit");
ButtonType cancelButtonType = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(saveButtonType, exitButtonType, cancelButtonType);
alert.showAndWait().ifPresent(buttonType -> {
if (buttonType == saveButtonType) {
if (file == null) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save File");
file = fileChooser.showSaveDialog(null);
} else {
try {
FileWriter writer = new FileWriter(file);
writer.write(FIleContent.getText());
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Platform.exit();
} else if (buttonType == exitButtonType) {
Platform.exit();
}
});
System.exit(0);
}
@FXML
void newFile(ActionEvent event) {
FIleContent.clear();
file = null;
}
@FXML
void copyText(ActionEvent event) {
selectedText = FIleContent.getSelectedText();
}
@FXML
void cutText(ActionEvent event) {
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putString(FIleContent.getSelectedText());
clipboard.setContent(content);
FIleContent.deleteText(FIleContent.getSelection());
}
@FXML
void deleteText(ActionEvent event) {
}
@FXML
void pasteText(ActionEvent event) {
int indexToPaste = FIleContent.getCaretPosition();
String textToCaretPosition = FIleContent.getText().substring(0, indexToPaste);
String caretPostionToEnd = FIleContent.getText().substring(indexToPaste);
FIleContent.setText(textToCaretPosition + selectedText + caretPostionToEnd);
}
@FXML
void selectAll(ActionEvent event) {
FIleContent.selectAll();
}
@FXML
void undo(ActionEvent event) {
FIleContent.undo();
}
@FXML
private void find() {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Search");
dialog.setHeaderText(null);
dialog.setContentText("Enter search term:");
dialog.showAndWait().ifPresent(searchTerm -> {
String text = FIleContent.getText();
int index = text.indexOf(searchTerm);
if (index >= 0) {
FIleContent.requestFocus();
FIleContent.selectRange(index, index +
searchTerm.length());
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Search Result");
alert.setHeaderText(null);
alert.setContentText("No match found.");
alert.showAndWait();
}
});
}
@FXML
private void findNext() {
String searchTerm = FIleContent.getSelectedText();
String text = FIleContent.getText();
int index = text.indexOf(searchTerm, FIleContent.getCaretPosition());
if (index >= 0) {
FIleContent.requestFocus();
FIleContent.selectRange(index, index + searchTerm.length());
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Search Result");
alert.setHeaderText(null);
alert.setContentText("No more matches found.");
alert.showAndWait();
}
}
@FXML
private void findPrevious() {
String searchTerm = FIleContent.getSelectedText();
String text = FIleContent.getText();
int index = text.lastIndexOf(searchTerm, FIleContent.getCaretPosition() - 1);
if (index >= 0) {
FIleContent.requestFocus();
FIleContent.selectRange(index, index + searchTerm.length());
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Search Result");
alert.setHeaderText(null);
alert.setContentText("No more matches found.");
alert.showAndWait();
}
}
private String showTextInputDialog(String title, String content) {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle(title);
dialog.setHeaderText(null);
dialog.setContentText(content);
Optional result = dialog.showAndWait();
return result.orElse(null);
}
@FXML
void replace() {
String searchTerm = showTextInputDialog("Almashtirish", "Almashtirish uchun matn kiriting:");
if (searchTerm != null && !searchTerm.isEmpty()) {
String replacementText =
showTextInputDialog("Almashtirish", "Almashtiriladigan matnni kiriting:");
if (replacementText != null) {
String text = FIleContent.getText();
text = text.replaceAll(Pattern.quote(searchTerm), Matcher.quoteReplacement(replacementText));
FIleContent.setText(text);
}
}
}
@FXML
void onFontSelector() {
Dialog dialog = new Dialog<>();
dialog.setTitle("Shrift tanlash");
dialog.setHeaderText("Shrift, uslub va oʻlchamlarni tanlash");
Label fontLabel = new Label("Shrift:");
ComboBox fontComboBox = new ComboBox<>();
fontComboBox.getItems().addAll(Font.getFamilies());
Label styleLabel = new Label("Uslub:");
ComboBox styleComboBox = new ComboBox<>();
styleComboBox.getItems().addAll("Regular", "Bold", "Italic", "Bold Italic");
Label sizeLabel = new Label("O'lchami:");
TextField sizeTextField = new TextField();
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.add(fontLabel, 0, 0);
gridPane.add(fontComboBox, 1, 0);
gridPane.add(styleLabel, 0, 1);
gridPane.add(styleComboBox, 1, 1);
gridPane.add(sizeLabel, 0, 2);
gridPane.add(sizeTextField, 1, 2);
dialog.getDialogPane().setContent(gridPane);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK,
ButtonType.CANCEL);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == ButtonType.OK) {
String fontName = fontComboBox.getValue();
FontWeight style = FontWeight.NORMAL;
FontPosture posture = FontPosture.REGULAR;

switch (styleComboBox.getValue()) {


case "Bold":
style = FontWeight.BOLD;
break;
case "Italic":
posture = FontPosture.ITALIC;
break;
case "Bold Italic":
style = FontWeight.BOLD;
posture = FontPosture.ITALIC;
break;
default:
break;
}
int fontSize = Integer.parseInt(sizeTextField.getText());
return Font.font(fontName, style, posture, fontSize);

}
return null;


});
Optional result = dialog.showAndWait();
result.ifPresent(font -> FIleContent.setFont(font));
return;
}
@FXML
void about() {
Stage aboutStage = new Stage(); aboutStage.setTitle("Haqida"); aboutStage.setResizable(false);
Label nameLabel = new Label("Dastur nomi: JavaFX Notepad"); Label versionLabel = new Label("Versiya: 1.0");
Label authorLabel = new Label("Muallif: Ahmadjonov Elbek");
GridPane gridPane = new GridPane(); gridPane.setVgap(10); gridPane.setHgap(0);
gridPane.add(nameLabel, 0, 1);
gridPane.add(versionLabel, 0, 2);
gridPane.add(authorLabel, 0, 3);
Scene aboutScene = new Scene(gridPane, 300, 300); aboutStage.setScene(aboutScene); aboutStage.show();
}
@FXML
void printText() {
PrinterJob printerJob = PrinterJob.createPrinterJob(); if (printerJob != null &&
printerJob.showPrintDialog(FIleContent.getScene().getWindow())) { if (printerJob.printPage(FIleContent)) {
printerJob.endJob();
} else {
showErrorAlert("Bosib chiqarishda xato roʻy berdi.");
}
}
}
private void showErrorAlert(String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}


}

Download 23.06 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling