源代码下载
Sun推JavaFX,RIA之战进入白热化(1)
【IT168 专稿】
六、探索JavaFX语言 – 声明语法
Sun打算让JavaFX提供比Java更多的快速用户接口开发功能。同时,这个脚本语言可
以被使用在应用需要的各个方面。在本文的例子中使用JavaFX开发了一个用户接口,一个登录窗口。本例还使用了Java程序实现了同一个登录窗口。这两个文件的代码如下:
用JavaFX实现的登录窗口代码:
import javafx.ui.*; Frame { title: "Authenticate Please" width: 300 height: 125 visible: true content: GridBagPanel { border: EmptyBorder { top: 5 left: 5 bottom: 5 right: 5} cells: [GridCell { anchor: EAST gridx: 0 gridy: 0 insets: {top: 2} content: SimpleLabel {text: "Username: "}}, GridCell { anchor: WEST fill: HORIZONTAL weightx: 1 gridx: 1 gridy: 0 content: TextField {}}, GridCell { anchor: EAST gridx: 0 gridy: 1 insets: {top: 2} content: SimpleLabel {text: "PasswordField: "}}, GridCell { anchor: WEST fill: HORIZONTAL gridx: 1 gridy: 1 weightx: 1 content: PasswordField {}}, GridCell { gridx: 1 gridy: 2 content: Button {text: "Login"}}]} }
用Java实现同样的登录窗口的代码:
public class Login extends javax.swing.JFrame { /** Creates new form Login */ public Login() { initComponents(); } // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { usernameField = new javax.swing.JTextField(); usernameLabel = new javax.swing.JLabel(); passwordLabel = new javax.swing.JLabel(); passwordField = new javax.swing.JPasswordField(); loginButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Authenticate Please"); setName("loginFrame"); usernameField.setColumns(15); usernameLabel.setText("Username:"); passwordLabel.setText("Password:"); passwordLabel.setRequestFocusEnabled(false); passwordLabel.getAccessibleContext().setAccessibleName("Password:"); loginButton.setLabel("Login"); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(usernameLabel) .add(19, 19, 19)) .add(layout.createSequentialGroup() .add(passwordLabel) .add(23, 23, 23))) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(passwordField) .add(usernameField))) .add(layout.createSequentialGroup() .add(147, 147, 147) .add(loginButton))) .addContainerGap(92, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(usernameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(usernameLabel)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(passwordLabel) .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE) .add(loginButton) .addContainerGap()) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Login().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton loginButton; private javax.swing.JPasswordField passwordField; private javax.swing.JLabel passwordLabel; private javax.swing.JTextField usernameField; private javax.swing.JLabel usernameLabel; // End of variables declaration }
从上面的代码可以看出,JavaFX和传统的Java代码有些类似的地方,如类名上,但从总体结构上看还是有很大的差别。图5显示了这两种方式建立的登录窗口,上面的是用JavaFX建立的,下面的是用传统的Java代码建立的。

图5 登录窗口:
这个小例子很清楚地揭示了JavaFX和传统的Java的第一个主要的不同。JavaFX使用了类似于HTML的语法形式来建立用户接口。在JavaFX的语法中,开发人员只是定义了应该显示些什么东西,而不是用程序来指定需要如何建立。很显然,象JavaFX风格(有时这种风格也叫做声明语法)的语法更容易建立维护。
当然,在JavaFX中也可以使用类似于Java的方式来建立相同的登录窗口。如下面的代码所示:
import javafx.ui.*; var frame = new Frame(); frame.title = "Authenticate Please"; frame.width = 300; frame.height = 125; var panel = new GridBagPanel(); var border = new EmptyBorder(); border.bottom=5; border.top=5; border.left=5; border.right=5; panel.border = border; var inset = new Insets(); inset.top=2; var gcUserLabel = new GridCell(); gcUserLabel.anchor = EAST; gcUserLabel.gridx=0; gcUserLabel.gridy=0; gcUserLabel.insets=inset; var usernameLabel = new SimpleLabel(); usernameLabel.text = "Username: "; gcUserLabel.content = usernameLabel; var gcUserField = new GridCell(); gcUserField.anchor = WEST; gcUserField.fill=HORIZONTAL; gcUserField.weightx=1; gcUserField.gridx=1; gcUserField.gridy=0; gcUserField.insets=inset; gcUserField.content = new TextField(); var gcPassLabel = new GridCell(); gcPassLabel.anchor = EAST; gcPassLabel.gridx=0; gcPassLabel.gridy=1; gcPassLabel.insets=inset; var passwordLabel = new SimpleLabel(); passwordLabel.text = "Password: "; gcPassLabel.content = passwordLabel; var gcPassField = new GridCell(); gcPassField.anchor = WEST; gcPassField.fill=HORIZONTAL; gcPassField.weightx=1; gcPassField.gridx=1; gcPassField.gridy=1; gcPassField.insets=inset; gcPassField.content = new PasswordField(); var gcButton = new GridCell(); gcButton.gridx=1; gcButton.gridy=2; var loginButton = new Button(); loginButton.text = "Login"; gcButton.content = loginButton; panel.cells = [gcUserLabel, gcUserField, gcPassLabel, gcPassField, gcButton]; frame.visible = true; frame.content = panel;