如何在 Java 桌面应用程序中设置菜单(网站样式导航链接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13556637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have menus (website style navigation links) in java desktop application
提问by Nabalad
I am using Netbeans and i want to develop a java desktop application. The application should be like a website somehow, i mean i want to have some menus in my java desktop application which by clicking on each one of those menus i should be able to access some different pages with different contains(like having main-menu, report-menu….). Any idea will be highly appreciated.
我正在使用 Netbeans,我想开发一个 Java 桌面应用程序。该应用程序应该以某种方式像一个网站,我的意思是我想在我的 Java 桌面应用程序中有一些菜单,通过单击这些菜单中的每一个,我应该能够访问一些具有不同内容的不同页面(例如有主菜单,报告菜单……)。任何想法都将受到高度赞赏。
回答by jewelsea
Here is a JavaFXbased sample, which generates a menu based on a set of hyperlinksto different content items. This is quite similar to how a lot of web pages work. The sample is styled via css, similar to a web page.
这是一个基于JavaFX的示例,它根据一组指向不同内容项的超链接生成一个菜单。这与许多网页的工作方式非常相似。该示例通过css 进行样式设置,类似于网页。
The sample creates the scene content in Java code, but you could construct the layouts and define the content items in fxmlgenerated by the SceneBuildertool if you prefer.
该样本创建Java代码的场景内容,但你可以构建布局和定义内容项FXML由产生SceneBuilder工具,如果你喜欢。
JavaFX also has traditional application menu barsas well (not demonstrated in this sample).
JavaFX 也有传统的应用程序菜单栏(本示例中未演示)。
Sample program output, with some different links clicked:
示例程序输出,点击了一些不同的链接:
Sample code:
示例代码:
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
/**
* Displays content panes activated by a hyper-link based navigation bar
*/
public class HyperlinkedNavMenu extends Application {
private LinkContent[] linkContent;
private final StackPane content = new StackPane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
linkContent = createLinkContent();
content.setPrefWidth(200);
HBox.setHgrow(content, Priority.ALWAYS);
stage.setTitle("Capello Pazzo");
stage.setScene(new Scene(createLayout()));
stage.show();
}
private Pane createLayout() {
HBox layout = new HBox(
10,
createNavBar(),
content
);
layout.getStylesheets().add(
getClass().getResource("nav.css").toExternalForm()
);
return layout;
}
private VBox createNavBar() {
VBox nav = new VBox();
nav.setMinWidth(100);
nav.getStyleClass().add("navbar");
for (int i = 0; i < linkContent.length; i++) {
Hyperlink link = createLink(
linkContent[i].linkText,
createContentNode(linkContent[i])
);
nav.getChildren().add(link);
if (i == 0) {
link.fire();
}
}
return nav;
}
private Node createContentNode(LinkContent linkContent) {
Label label = new Label(linkContent.contentText);
label.setWrapText(true);
VBox contentNode = new VBox(
10,
new ImageView(linkContent.image),
label
);
contentNode.getStyleClass().add("contentnode");
return contentNode;
}
private Hyperlink createLink(final String linkText, final Node contentNode) {
Hyperlink link = new Hyperlink(linkText);
link.setOnAction(t -> content.getChildren().setAll(
contentNode
));
return link;
}
private static class LinkContent {
final String linkText, contentText;
final Image image;
LinkContent(String linkText, String contentText, String imageLoc) {
this.linkText = linkText;
this.contentText = contentText;
this.image = new Image(imageLoc);
}
}
// icon license: http://creativecommons.org/licenses/by-nc-nd/3.0/
// icon attribution: http://www.iconarchive.com/artist/archigraphs.html
private LinkContent[] createLinkContent() {
return new LinkContent[] {
new LinkContent(
"Lorem",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"http://icons.iconarchive.com/icons/archigraphs/tea-party/128/Sugar-Cubes-icon.png"
),
new LinkContent(
"Vestibulum",
"Vestibulum a dui et massa laoreet vehicula.",
"http://icons.iconarchive.com/icons/archigraphs/tea-party/128/Tea-Cake-icon.png"
),
new LinkContent(
"Donec",
"Donec sed euismod risus.",
"http://icons.iconarchive.com/icons/archigraphs/tea-party/128/Tea-Cup-icon.png"
),
new LinkContent(
"Duis",
"Duis semper porttitor leo ac posuere.",
"http://icons.iconarchive.com/icons/archigraphs/tea-party/128/Tea-Pot-icon.png"
)
};
}
}
Sample css:
示例CSS:
/** file: nav.css
* place in same directory as HyperlinkedNavMenu.java and have your build system copy it
* to the same location as HyperlinkedNavMenu.java.class */
.root {
-fx-background-image: url("http://images.all-free-download.com/images/graphiclarge/linen_fabric_background_04_hd_picture_169825.jpg");
-fx-padding: 15;
-fx-font-size: 15;
}
.navbar {
-fx-background-color: burlywood, peachpuff;
-fx-background-radius: 10, 10;
-fx-background-insets: 0, 2;
-fx-font-style: italic;
-fx-padding: 10 15 15 10;
}
.contentnode {
-fx-background-color: aliceblue;
-fx-padding: 15 20 20 15;
-fx-effect: dropshadow(gaussian, slategrey, 10, 0, 5, 5);
}
回答by Inzimam Tariq IT
If you are using an IDE like Netbeans
it gives facility of menu bar
, menu
and menu item
you can drag and drop the menu bar
over JFrame
and add menus and menu items to it.
如果您使用的是IDE喜欢Netbeans
它给人的设施menu bar
,menu
并且menu item
你可以拖放menu bar
了JFrame
,并添加菜单和菜单项,以它。
OR
或者
You can use card layout to show multiple frames in a single place.
您可以使用卡片布局在一个位置显示多个框架。