2012-12-04 58 views
-1

我想隐藏在秋千显示FXPanel控制在JavaFX应用程序隐藏和Swing的JFrame中显示JFXPanel

我想在点击一个按钮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(); 
} 
} 

回答

1
从一些错别字

除此之外,有一个与你的代码中的多个问题:

1)如果你正在使用的ActionEvent#getActionCommand以确定哪个按钮被点击,你必须设置的动作命令属性上首先按钮。操作命令与按钮的文本不一样。

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

3)不要使用过时的hide() - 方法隐藏JFXPanel,使用setVisisble(假)

此外,一些普通指针:

4)不要使用空布局对于一个正常的UI。永远。

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

6)通过SwingUtilities#invokeLater调用显示摆动部件的代码,就像使用Platform类一样。像你一样从主线程调用swing会在大多数情况下工作,但会导致偶尔出现的难以追踪的错误。

+0

@ sarcan统筹不是问题,问题是,如果我做调用setVisible(假)的JFXPanel则不能再次调用setVisible(真)还怎么可能。 –

+0

我看到你更新了上面的代码,但是你仍然缺少setActionCommand调用。添加'b1.setActionCommand(“OK”); b2.setActionCommand(“隐藏”);'到你的构造函数,否则你的动作侦听器的代码不会做任何事情。 – sarcan

+0

告诉你是否可以如何让它再次可见 –