如何在 JavaFX 中為圖片新增上下文選單?


上下文選單是一個在應用程式中與 UI 元素進行互動時出現的彈出選單。你可以透過例項化 javafx.scene.control.ContextMenu 類來建立上下文選單。就像一個選單一樣,在建立一個上下文選單後,你需要向其新增 MenuItems。

通常情況下,當你“右鍵單擊”附加的控制元件時會出現一個上下文選單。

為節點設定 ContextMenu−

你可以使用 setContextMenu() 方法為 javafx.scene.control 類的任意物件設定 ContextMenu。

每個節點都有一個名為 onContextMenuRequested 的屬性,它定義了一個當在這個節點上請求上下文選單時被呼叫的函式。你可以使用 setOnContextMenuRequested() 選單為此屬性設定值。

若要為影像檢視設定上下文選單,請建立一個嵌入所需影像的 ImageView 物件,然後呼叫它上的 setOnContextMenuRequested() 方法。

示例

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ContextMenuImage extends Application {
   @Override
   public void start(Stage stage) throws FileNotFoundException {
      //Creating the image view
      ImageView imageView = new ImageView();
      //Setting the image view parameters
      imageView.setFitWidth(575);
      // imageView.setFitHeight(295);
      imageView.setPreserveRatio(true);
      InputStream stream = new FileInputStream("D:\images\elephant.jpg");
      Image image = new Image(stream);
      imageView.setImage(image);
      //Creating a context menu
      ContextMenu contextMenu = new ContextMenu();
      //Creating the menu Items for the context menu
      MenuItem item1 = new MenuItem("Copy");
      MenuItem item2 = new MenuItem("remove");
      contextMenu.getItems().addAll(item1, item2);
      //Setting action to the context menu item
      item1.setOnAction((ActionEvent e) -> {
         Clipboard clipboard = Clipboard.getSystemClipboard();
         ClipboardContent content = new ClipboardContent();
         content.putImage(imageView.getImage());
         clipboard.setContent(content);
      });
      //Setting action to the context menu item
      item2.setOnAction((ActionEvent e) -> {
         imageView.setVisible(false);
      });
      //Setting context menu to the image view
      imageView.setOnContextMenuRequested(new EventHandler() {
         @Override
         public void handle(ContextMenuEvent event) {
            contextMenu.show(imageView, event.getScreenX(), event.getScreenY());
         }
      });
      HBox box = new HBox();
      box.setPadding(new Insets(10, 10, 10, 10));
      box.getChildren().add(imageView);
      //Setting the stage
      Group root = new Group(box);
      Scene scene = new Scene(root, 595, 360, Color.BEIGE);
      stage.setTitle("CustomMenuItem");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於: 20-May-2020

744 次瀏覽

啟動你的 職業

透過完成課程獲得認證

開始
Advertisements