【问题标题】:Hide and display JFXPanel on Swing JFrame在 Swing JFrame 上隐藏和显示 JFXPanel
【发布时间】:2012-11-21 23:13:25
【问题描述】:

我想在 swing 中的 javaFx 应用程序中隐藏和显示 FXPanel 控件

我希望在单击按钮时 FXPanel 控件应该被隐藏,并且在单击其他控件时应该再次可见,它被隐藏而不是再次可见。

使用以下代码。

public class abc extends JFrame
{
JFXPanel fxpanel;
Container cp;
public abc()
{
cp=this.getContentPane();
cp.setLayout(null);
JButton b1= new JButton("Ok");
JButton b2= new JButton("hide");
cp.add(b1);
cp.add(b2);
b1.setBounds(20,50,50,50);
b2.setBounds(70,50,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
fxpanel= new JFXPanel();
cp.add(fxpanel);
fxpanel.setBounds(600,200,400,500);
}

public void actionPerformed(ActionEvent ae)
{ 
 if(ae.getActionCommand().equals("OK"))
 {
fxpanel.setVisible(true);
 }
   if(ae.getActionCommand().equals("hide"))
 {
 fxpanel.hide();
 }

 Platform.runLater(new Runnable())
{

 public void run()
 {
  init Fx(fxpanel);
  }}
 );
 }
 private static void initFX(final JFXPanel fxpanel) 
{
  Group group = ne Group();
  Scene scene= new Scene(group);
  fxpanel.setScene(scene);
  WebView webview= new WebView();
  group.getChildren().add(webview);
  webview.setMinSize(500,500);
  webview.setMaxSize(500,500);
  eng=webview.getEngine();
  File file= new File("d:/new folder/abc.html");
  try
 {
 eng.load(file.toURI().toURL().toString());
 }
catch(Exception ex)
{
}
}
public static void main(String args[])
{
 abc f1= new abc();
 f1.show();
}
}

【问题讨论】:

    标签: java swing webview javafx-2 javafx


    【解决方案1】:

    除了一些拼写错误之外,您的代码还有多个问题:

    1) 如果您使用 ActionEvent#getActionCommand 来确定单击了哪个按钮,则必须首先在按钮上设置操作命令属性。动作命令与按钮文本不同。

    2) 您正在添加具有相同坐标的两个按钮,因此不会显示一个。

    3) 不要使用已弃用的 hide()- 方法隐藏 JFXPanel,使用 setVisisble(false)

    此外,还有一些通用的提示:

    4) 不要对普通 UI 使用 null 布局。永远。

    5) 阅读 java 命名约定。这不仅仅是我挑剔,它会帮助你更好地理解其他人的代码,并帮助其他人维护你的代码。

    6) 通过 SwingUtilities#invokeLater 从 EDT 调用显示挥杆组件的代码,就像您使用 Platform 类一样。像你一样从主线程调用 swing 在大多数情况下都会起作用,但会导致难以跟踪的偶尔错误。

    【讨论】:

    • 我看到您更新了上面的代码,但您仍然缺少 setActionCommand 调用。将b1.setActionCommand("OK"); b2.setActionCommand("hide"); 添加到您的构造函数中,否则您的动作侦听器代码将不会执行任何操作。
    • 告诉你我怎样才能让它再次可见
    猜你喜欢
    • 2014-12-27
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多