fxml文件如下(省略头文件):
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:id="pane"
fx:controller="com.github.parboiled1.grappa.debugger.mainwindow.MainWindowUi">
<top>
<MenuBar BorderPane.alignment="CENTER">
<Menu mnemonicParsing="false" text="File">
<MenuItem fx:id="loadInput" mnemonicParsing="false"
text="Load file" onAction="#loadFileEvent"/>
<MenuItem fx:id="parse" mnemonicParsing="false"
text="Parse" onAction="#parseEvent"/>
<MenuItem fx:id="closeButton" mnemonicParsing="false"
text="Close" onAction="#closeWindowEvent"/>
</Menu>
</MenuBar>
</top>
<center>
<SplitPane dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<SplitPane dividerPositions="0.5" orientation="VERTICAL">
<TreeView fx:id="traceTree" prefHeight="200.0"
prefWidth="200.0" editable="false"/>
<TextArea fx:id="traceDetail" prefHeight="200.0"
prefWidth="200.0"/>
</SplitPane>
<TextArea fx:id="inputText" prefHeight="200.0" prefWidth="200.0"/>
</SplitPane>
</center>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)
TreeView我可以毫无问题地设置根。树已更新,没有问题。
我遇到的问题是我无法设法在视图中的给定项目上触发事件。我尝试使用onMouseClicked简单的 System.out.println() 添加一个事件,无论我在树中单击哪个项目,我都可以看到该事件被触发。但我根本无法获取视图中已单击的项目。
我怎么做?
我正在尝试创建我自己的 iTunes 版本。我正在尝试创建一个音乐播放器,这是我的方法:
public void audioPlayerButtons(ActionEvent actionEvent) {
if (actionEvent.getSource() == playbtn) {
String bip = "/Users/april121/Work/MyMusic!/src/sample/Songs/01 Clarity.m4a";
Media hit = new Media(bip);
MediaPlayer mediaPlayer = new MediaPlayer(hit);
MediaPlayer.play();
}
else (actionEvent.getSource()== pausebtn){
MediaPlayer.pause();
}
else (actionEvent.getSource()==forwardbtn){
MediaPlayer.seek(MediaPlayer.getStartTime());
MediaPlayer.stop();
}
else (actionEvent.getSource()==backwardbtn){
//to be finished
}
Run Code Online (Sandbox Code Playgroud)
但我现在已经尝试了几个小时 - 无论是通过从 Maven 导入库还是硬编码,它都不起作用。
我希望它显示正在播放的内容并具有基本功能,即。播放、暂停、快退和快进,并有进度条。
这是它显示的错误:
non-static method can't be accessed in static context. And the part that is causing the error is the ".stop()" or ".play()" bits
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么 - 因为我的方法无论如何都是非静态的

我试图设置我的选择框的默认选定项目,但是它没有按预期工作......
<ChoiceBox fx:id="d" value="- Select choice -">
<String fx:value="hellow"/>
</ChoiceBox>
Run Code Online (Sandbox Code Playgroud) 我在 JavaFX 中使用 fxml,并且想要一个列表主/详细视图。左侧的固定大小的列表,以及宽度随着窗口大小而增长的 TextArea 的细节。
这是我的 fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar prefWidth="671.0" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<HBox BorderPane.alignment="CENTER">
<children>
<ListView prefHeight="754.0" prefWidth="236.0" /> …Run Code Online (Sandbox Code Playgroud) 在 youtube 上有这个视频https://www.youtube.com/watch?v=Idtm2Y6I23w 其中
这个人动态填充了从存储在数据库中的项目创建的按钮。例如在这张图片中

它有一些类别,包括Tables,Stock,Reports和Users
当在用户点击Table类别它显示Buttons动态地从存储在数据库中的MySQL项创建的。例如,这是结果图像:
一个人怎么发展成这样?是否有将要使用的 for 循环语句?
编辑
我就是这样做的。这是源代码。
public class OrderController implements Initializable {
@FXML
private ListView<Button> listvieww;
@FXML
private HBox hboxx;
List<String> listOfSomething = null;
//@FXML
// private Pane panedynamic;
private DBConnection database = new DBConnection();
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ObservableList<Button> buttons = FXCollections.observableArrayList();
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle …Run Code Online (Sandbox Code Playgroud) 我刚刚完成了我的第一个 Java 项目,但是我似乎无法正确构建它。它将加载整个计算器运行的初始 fxml 文件,但是当我尝试在假期/休息菜单中添加假期/休息时,应该显示一个对话框,以便用户可以填写信息,但我一直没有得到一个位置在cmd中运行jar文件时设置错误。当我在 intellij 中启动它但不在 jar 中时,这一切都有效。
这就是我设置文件的方式。我在 FinishedCalCulator 项目中创建了一个资源目录,并将其标记为资源根
这就是我将 FXML 文件的位置设置到加载器中的方式。当我在 Intellij 中运行它但不在 jar 文件中时,这有效。
我环顾四周,发现一些帖子谈论将所有 fxml 文件放入资源文件中,我这样做并使其在 Intellij 中再次工作,但是当我尝试添加它们时,它仍然不会显示对话框窗格。我不确定我在这里做错了什么。我不认为我完全理解我做错了什么。任何帮助都会很棒。
我是 Java 新手,我想知道如何在单击项目时防止 MenuButton 弹出窗口关闭,我尝试了以下功能,我在本网站上找到了该功能,但似乎没有任何作用。我需要这个来克隆我的 C# 应用程序,以便学习 Java。
@FXML
private MenuButton menuButton;
@FXML
void initialize() {
CheckMenuItem menuButtonItem1 = new CheckMenuItem("Item 1");
CheckMenuItem menuButtonItem2 = new CheckMenuItem("Item 2");
CheckMenuItem menuButtonItem3 = new CheckMenuItem("Item 3");
menuButtonItem1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
e.consume();
}
});
menuButtonItem2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
e.consume();
}
});
menuButtonItem3.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
e.consume();
}
});
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
<VBox id="menu" styleClass="menu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.app.menu.MenuController">
<stylesheets>
<URL value="@menu.css"/>
</stylesheets>
<Button text="Customers" onAction="#handleCustomers" />
<Button text="Suppliers" onAction="#handleSuppliers" />
<Separator/>
<Button text="Families" onAction="#handleFamilies" />
<Button text="Products" onAction="#handleProducts" />
<Separator/>
</VBox>
Run Code Online (Sandbox Code Playgroud)
这会生成一组堆叠的按钮(根据需要),但它们不会占据容器的整个宽度。
如果我试试这个:
我有以下代码:
<VBox id="menu" styleClass="menu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.app.menu.MenuController">
<stylesheets>
<URL value="@menu.css"/>
</stylesheets>
<AnchorPane>
<Button text="Customers" onAction="#handleCustomers" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Button text="Suppliers" onAction="#handleSuppliers" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Separator/>
<Button text="Families" onAction="#handleFamilies" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Button text="Products" onAction="#handleProducts" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Separator/>
</AnchorPane>
</VBox>
Run Code Online (Sandbox Code Playgroud)
然后我将按钮水平调整大小,但它们不再堆叠。我无法使用 FXML 在 Java FX 中实现这一点。
所需的输出是这样的: