🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> 默认是不显示右键菜单的,需要注册Browser.setContextMenuHandler。如果是重量级模式下,需要设置一下 JPopupMenu.setDefaultLightWeightPopupEnabled(false);,否则显示不 出来轻量级的上下文菜单 > 默认情况下,当用户右键单击加载的网页时,JxBrowser不会显示上下文菜单。不支持标准Chromium上下文菜单,因为它是Google Chromium应用程序的一部分。 要在用户右键单击加载的网页时显示上下文菜单,您必须注册ContextMenuHandler实现。当用户右键单击网页时,将调用ContextMenuHandler.showContextMenu(ContextMenuParams params)方法。此方法接收ContextMenuParams参数,该参数包含有关网页上鼠标右键单击位置的HTML元素的信息,鼠标指针的位置,鼠标指针下的HTML元素类型,链接URL,链接文本,图像src属性值等。使用此信息配置上下文菜单。 要注册ContextMenuHandler的实现,请使用Browser.setContextMenuHandler(ContextMenuHandler contextMenuHandler)方法。 ### Swing ``` import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.ContextMenuHandler; import com.teamdev.jxbrowser.chromium.ContextMenuParams; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * The sample demonstrates how to register custom ContextMenuHandler, * to handle mouse right clicks and display custom Swing context menu. */ public class ContextMenuSample { public static void main(String[] args) { JPopupMenu.setDefaultLightWeightPopupEnabled(false); Browser browser = new Browser(); BrowserView view = new BrowserView(browser); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(view, BorderLayout.CENTER); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.setContextMenuHandler(new MyContextMenuHandler(view)); browser.loadURL("http://www.google.com"); } private static class MyContextMenuHandler implements ContextMenuHandler { private final JComponent component; private MyContextMenuHandler(JComponent parentComponent) { this.component = parentComponent; } public void showContextMenu(final ContextMenuParams params) { final JPopupMenu popupMenu = new JPopupMenu(); if (!params.getLinkText().isEmpty()) { popupMenu.add(createMenuItem("Open link in new window", new Runnable() { public void run() { String linkURL = params.getLinkURL(); System.out.println("linkURL = " + linkURL); } })); } final Browser browser = params.getBrowser(); popupMenu.add(createMenuItem("Reload", new Runnable() { public void run() { browser.reload(); } })); final Point location = params.getLocation(); SwingUtilities.invokeLater(new Runnable() { public void run() { popupMenu.show(component, location.x, location.y); } }); } private static JMenuItem createMenuItem(String title, final Runnable action) { JMenuItem reloadMenuItem = new JMenuItem(title); reloadMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { action.run(); } }); return reloadMenuItem; } } } ``` ### JavaFX ``` import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.ContextMenuHandler; import com.teamdev.jxbrowser.chromium.ContextMenuParams; import com.teamdev.jxbrowser.chromium.javafx.BrowserView; import javafx.application.Application; import javafx.application.Platform; import javafx.event.*; import javafx.geometry.Point2D; import javafx.scene.Scene; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import java.awt.*; /** * The sample demonstrates how to register custom ContextMenuHandler, * to handle mouse right clicks and display custom JavaFX context menu. */ public class JavaFXContextMenuSample extends Application { @Override public void start(Stage primaryStage) { Browser browser = new Browser(); BrowserView browserView = new BrowserView(browser); browser.setContextMenuHandler(new MyContextMenuHandler(browserView)); StackPane pane = new StackPane(); pane.getChildren().add(browserView); Scene scene = new Scene(pane, 700, 500); primaryStage.setScene(scene); primaryStage.show(); browser.loadURL("http://www.google.com"); } public static void main(String[] args) { launch(args); } private static class MyContextMenuHandler implements ContextMenuHandler { private final Pane pane; private MyContextMenuHandler(Pane paren) { this.pane = paren; } public void showContextMenu(final ContextMenuParams params) { Platform.runLater(new Runnable() { @Override public void run() { createAndDisplayContextMenu(params); } }); } private void createAndDisplayContextMenu(final ContextMenuParams params) { final ContextMenu contextMenu = new ContextMenu(); // Since context menu doesn't auto hide, listen mouse press events // on BrowserView and hide context menu on mouse press pane.getChildren().get(0).setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { contextMenu.hide(); } }); // If there's link under mouse pointer, create and add // the "Open link in new window" menu item to our context menu if (!params.getLinkText().isEmpty()) { contextMenu.getItems().add(createMenuItem( "Open link in new window", new Runnable() { public void run() { String linkURL = params.getLinkURL(); System.out.println("linkURL = " + linkURL); } })); } // Create and add "Reload" menu item to our context menu contextMenu.getItems().add(createMenuItem("Reload", new Runnable() { public void run() { params.getBrowser().reload(); } })); // Display context menu at required location on screen Point location = params.getLocation(); Point2D screenLocation = pane.localToScreen(location.x, location.y); contextMenu.show(pane, screenLocation.getX(), screenLocation.getY()); } private static MenuItem createMenuItem(String title, final Runnable action) { MenuItem menuItem = new MenuItem(title); menuItem.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { action.run(); } }); return menuItem; } } } ```