0
我正在使用JavaFX控件的swing应用程序。我有三个控制按钮WebView
和JTable
。 点击button1
表应该被添加在屏幕上,并且在点击其他按钮时应该删除网页视图 表应该被删除并且网页视图应该被添加。JavaFX和Swing控件之间的交换
我正在使用以下代码。
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.web.*;
import javafx.stage.Stage;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class webviewtestclass extends JFrame implements ActionListener {
JFXPanel fxpanel,fxpanel1;
static String filepath;
JTable table;
public webviewtestclass()
{
setLayout(null);
JButton b1= new JButton("OK");
JButton b2= new JButton("OKK");
add(b1);
add(b2);
b1.setBounds(20,50,50,30);
b2.setBounds(70,50,50,30);
b1.addActionListener(this);
b2.addActionListener(this);
fxpanel= new JFXPanel();
add(fxpanel);
fxpanel.setBounds(10,50,1000,800);
Object obj=new Object[50][20];
String title[]={"head1","head2"};
table=new JTable(title,obj);
add(table);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("OK"))
{
remove(fxpanel);
add(table);
}
if(ae.getActionCommand().equals("OKK"))
{
remove(table);
add(fxpanel);
}
Platform.runLater(new Runnable() {
public void run()
{
initFx(fxpanel);
}}
);
}
private static void initFx(final JFXPanel fxpanel)
{
String htmlpath="d:/lcrux/html/";
Group group= new Group();
Scene scene= new Scene(group);
fxpanel.setScene(scene);
WebView webview = new WebView();
group.getChildren().add(webview);
webview.setMinSize(1200,800);
webview.setMaxSize(1200,800);
webview.setVisible(true);
final WebEngine eng = webview.getEngine();
File htmlFile = new File(htmlpath+filepath);
try
{
eng.load(htmlFile.toURI().toURL().toString());
}
catch(Exception ex)
{
}
}
public static void main(final String args[])
{
webviewtestclass frm=new webviewtestclass();
frm.show();
}
}
*“我使用下面的代码。” *使用的代码块一致性和逻辑缩进。 –
'setLayout(null); .. b1.setBounds(20,50,50,30);'*使用布局管理器*** –