查看以下屏幕截图。一个仅是swing,另一个是JavaFX应用程序窗口,在该窗口中,借助SwingNode嵌入了相同的swing组件,从树节点文本中可以看到,字体呈现的方式有所不同。嵌入到swing节点中后,文本看起来不会更平滑。
这两个应用程序的代码如下。
请提供帮助,因为这是逐步将swing应用程序迁移到javafx的关键问题。
public class SwingComponent extends JPanel{
private static final long serialVersionUID = 4655993057136625809L;
public SwingComponent(){
super.setLayout(new BorderLayout(5,5));
//setBackground(Color.CYAN);
JTree jTree = new JTree();
jTree.setDragEnabled(true);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("JTree");
DefaultMutableTreeNode parent;
parent = new DefaultMutableTreeNode("I am");
root.add(parent);
parent.add(new DefaultMutableTreeNode("a Swing tree!"));
parent.add(new DefaultMutableTreeNode("on the right side..."));
parent = new DefaultMutableTreeNode("Animals");
root.add(parent);
parent.add(new DefaultMutableTreeNode("cat"));
parent.add(new DefaultMutableTreeNode("dog"));
parent.add(new DefaultMutableTreeNode("dolphin"));
parent.add(new DefaultMutableTreeNode("snake"));
parent = new DefaultMutableTreeNode("Activities");
root.add(parent);
parent.add(new DefaultMutableTreeNode("coding"));
parent.add(new DefaultMutableTreeNode("sleeping"));
parent.add(new DefaultMutableTreeNode("debugging"));
jTree.setModel(new DefaultTreeModel(root));
expandAllNodes(jTree, 0, 4);
JTextField jTextField = new JTextField("Type something here...");
super.add(BorderLayout.CENTER,new JScrollPane(jTree));
super.add(BorderLayout.SOUTH,jTextField);
}
private void expandAllNodes(JTree tree, int startingIndex, int rowCount){
for(int i=startingIndex;i<rowCount;++i){
tree.expandRow(i);
}
if(tree.getRowCount()!=rowCount){
expandAllNodes(tree, rowCount, tree.getRowCount());
}
}
}
纯秋千应用
public class SwingApplication {
public SwingApplication(){
buildUI();
}
private void buildUI(){
SwingComponent comp = new SwingComponent();
JFrame frame = new JFrame("Only Swing App");
frame.getContentPane().add(comp);
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setVisible(true);
}
public static void main (String args[]){
new SwingApplication();
}
}
具有使用SwingNode嵌入的Swing组件的FX应用程序
public class SwingFXApplication extends Application {
@Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setTitle("FX App with SwingComponent using SwingNode");
Scene scene = new Scene(pane, 400, 400);
System.out.println(scene.getAntiAliasing());
stage.setScene(scene);
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingNode.setContent(new SwingComponent());
}
});
}
public static void main(String[] args) {
launch(args);
}
}