Java 在Swing JFrame上隐藏和显示JFXPanel
我想在swing的javaFx应用程序中隐藏和显示FXPanel控件 我想在单击按钮时,FXPanel控件应隐藏,而在单击其他按钮时,控件应再次可见它正在隐藏,不再可见 使用以下代码Java 在Swing JFrame上隐藏和显示JFXPanel,java,swing,webview,javafx-2,javafx,Java,Swing,Webview,Javafx 2,Javafx,我想在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");
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();
}
}
除了一些打字错误外,您的代码还存在多个问题: 1) 如果使用ActionEvent#getActionCommand来确定单击了哪个按钮,则必须首先在按钮上设置action command属性。操作命令与按钮的文本不同 2) 您正在添加具有相同坐标的两个按钮,因此其中一个按钮不会显示 3) 不要使用不推荐使用的hide()-方法隐藏JFXPanel,请使用setVisisble(false) 此外,还有一些一般性的建议: 4) 不要对普通UI使用空布局。永远 5) 阅读java命名约定。这不仅仅是我的挑剔,它将帮助您更好地理解其他人的代码,并帮助其他人维护您的代码
6) 通过
SwingUtilities#invokeLater
从EDT调用显示swing组件的代码,就像使用平台
类一样。像您那样从主线程调用swing在大多数情况下都可以工作,但偶尔会导致难以跟踪的错误 @sarcan-coordinate不是问题,问题是,如果我将JFXPanel设置为setvisible(false),那么它就不能再次设置为setvisible(true),而且这是怎么可能的。我看到你更新了上面的代码,但你仍然缺少setActionCommand调用。添加b1.setActionCommand(“OK”);b2.setActionCommand(“隐藏”)
到您的构造函数,否则您的操作侦听器代码将不起任何作用。告诉您是否可以,我如何使其再次可见