在 JAVA swing 中,我们使用 JFXPanel 实现 JAVAFX 按钮,然后我们删除 jpanel 并再次添加它,然后场景被删除或隐藏
In JAVA swing when we implement JAVAFX button using JFXPanel, and then we remove jpanel and add it again then scene is removed or hide
我有一个演示应用程序,我在其中使用 JFXPanel 在 JPanel 中添加 JAVAFX 按钮并将场景添加到 JFXPanel。但是当我删除并再次添加实现我的 JFXPanel 的 JPanel 时,场景被删除或隐藏在 JFXPanel 中,请帮忙。
我也尝试过 repain() 和 revalidate() JFXPanel 和 JPanel 但没有任何反应。
这是应用程序的完整代码,请帮忙。
public class MainForm extends JFrame {
JButton btnTest;
JButton button;
JLabel lblMessage;
JPanel mainPanel;
JFXPanel jfxPanel;
JPanel panel1;
JPanel panel2;
public MainForm() {
initialize();
}
private void initialize() {
setTitle("Touch Screen Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
mainPanel = new JPanel();
mainPanel.setLayout(new MigLayout("insets 10 10 10 10"));
panel1 = new JPanel();
panel1.setLayout(new MigLayout("insets 10 10 10 10"));
panel2 = new JPanel();
panel2.setLayout(new MigLayout("insets 10 10 10 10"));
MouseListener listener = new TestButtonListener();
btnTest = new JButton("Test");
btnTest.addMouseListener(listener);
jfxPanel = new JFXPanel();
jfxPanel.setToolTipText("JFX Panel");
jfxPanel.setScene(createScene());
jfxPanel.setPreferredSize(new Dimension(300, 50));
lblMessage = new JLabel("Your message Here.");
lblMessage.setFont(new Font("Arial", Font.PLAIN, 80));
panel1.add(btnTest, "h 50, w 300, push, c, wrap");
panel1.add(jfxPanel, "h 50, w 300, push, c, wrap");
panel1.add(lblMessage, "pushx, c");
button = new JButton("change");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (button.getText().equals("change")) {
mainPanel.remove(panel1);
mainPanel.repaint();
mainPanel.revalidate();
button.setText("changed");
} else {
mainPanel.add(panel1, "push, grow, wrap, cell 0 0");
mainPanel.repaint();
mainPanel.revalidate();
button.setText("change");
}
}
});
panel2.add(button, "push, align c");
mainPanel.add(panel1, "push, grow, wrap, cell 0 0");
mainPanel.add(panel2, "pushx, growx, cell 0 1");
add(mainPanel);
setVisible(true);
setLocationRelativeTo(null);
}
class TestButtonListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
lblMessage.setText("Button pressed");
}
@Override
public void mouseReleased(MouseEvent arg0) {
lblMessage.setText("Button released");
}
}
public static void main(String[] args) {
new MainForm();
}
@SuppressWarnings("static-access")
private Scene createScene() {
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox);
scene.getStylesheets().add(getClass().getResource("res/style.css").toString());
Button button = new Button("JAVAFX Button");
button.setPrefSize(300, 50);
button.setOnTouchPressed(new EventHandler<Event>() {
@Override
public void handle(Event event) {
lblMessage.setText("Button pressed");
}
});
button.setOnTouchReleased(new EventHandler<Event>() {
@Override
public void handle(Event event) {
lblMessage.setText("Button released");
}
});
vBox.getChildren().add(button);
return scene;
}
}
终于找到了这个问题的答案post
Prevent JavaFX thread from dying with JFXPanel Swing interop?
我有一个演示应用程序,我在其中使用 JFXPanel 在 JPanel 中添加 JAVAFX 按钮并将场景添加到 JFXPanel。但是当我删除并再次添加实现我的 JFXPanel 的 JPanel 时,场景被删除或隐藏在 JFXPanel 中,请帮忙。
我也尝试过 repain() 和 revalidate() JFXPanel 和 JPanel 但没有任何反应。
这是应用程序的完整代码,请帮忙。
public class MainForm extends JFrame {
JButton btnTest;
JButton button;
JLabel lblMessage;
JPanel mainPanel;
JFXPanel jfxPanel;
JPanel panel1;
JPanel panel2;
public MainForm() {
initialize();
}
private void initialize() {
setTitle("Touch Screen Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
mainPanel = new JPanel();
mainPanel.setLayout(new MigLayout("insets 10 10 10 10"));
panel1 = new JPanel();
panel1.setLayout(new MigLayout("insets 10 10 10 10"));
panel2 = new JPanel();
panel2.setLayout(new MigLayout("insets 10 10 10 10"));
MouseListener listener = new TestButtonListener();
btnTest = new JButton("Test");
btnTest.addMouseListener(listener);
jfxPanel = new JFXPanel();
jfxPanel.setToolTipText("JFX Panel");
jfxPanel.setScene(createScene());
jfxPanel.setPreferredSize(new Dimension(300, 50));
lblMessage = new JLabel("Your message Here.");
lblMessage.setFont(new Font("Arial", Font.PLAIN, 80));
panel1.add(btnTest, "h 50, w 300, push, c, wrap");
panel1.add(jfxPanel, "h 50, w 300, push, c, wrap");
panel1.add(lblMessage, "pushx, c");
button = new JButton("change");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (button.getText().equals("change")) {
mainPanel.remove(panel1);
mainPanel.repaint();
mainPanel.revalidate();
button.setText("changed");
} else {
mainPanel.add(panel1, "push, grow, wrap, cell 0 0");
mainPanel.repaint();
mainPanel.revalidate();
button.setText("change");
}
}
});
panel2.add(button, "push, align c");
mainPanel.add(panel1, "push, grow, wrap, cell 0 0");
mainPanel.add(panel2, "pushx, growx, cell 0 1");
add(mainPanel);
setVisible(true);
setLocationRelativeTo(null);
}
class TestButtonListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
lblMessage.setText("Button pressed");
}
@Override
public void mouseReleased(MouseEvent arg0) {
lblMessage.setText("Button released");
}
}
public static void main(String[] args) {
new MainForm();
}
@SuppressWarnings("static-access")
private Scene createScene() {
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox);
scene.getStylesheets().add(getClass().getResource("res/style.css").toString());
Button button = new Button("JAVAFX Button");
button.setPrefSize(300, 50);
button.setOnTouchPressed(new EventHandler<Event>() {
@Override
public void handle(Event event) {
lblMessage.setText("Button pressed");
}
});
button.setOnTouchReleased(new EventHandler<Event>() {
@Override
public void handle(Event event) {
lblMessage.setText("Button released");
}
});
vBox.getChildren().add(button);
return scene;
}
}
终于找到了这个问题的答案post
Prevent JavaFX thread from dying with JFXPanel Swing interop?