2012-12-04 39 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

告訴你是否可以如何讓它再次可見 –