Comparing WFC and JFC by David M. Johnson Listing One import java.awt.*; import java.awt.event.*; import com.sun.java.swing.*; /** Frame with desktop pane and button that creates internal frames. */ public class example1 extends JFrame { private JDesktopPane _desktop = new JDesktopPane(); private JButton _button = new JButton("Add Internal Frame"); public example1() { super("example1"); // Add button in the north and desktop pane in center getContentPane().setLayout(new BorderLayout()); getContentPane().add(_button,"North"); getContentPane().add(_desktop,"Center"); // Add listener to button to create new internal frame _button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { _desktop.add(new intframe(),JLayeredPane.PALETTE_LAYER); } }); // Add listener to window to exit on window close addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Size and display the frame setVisible(true); setSize(500,400); } public static void main(String[] args) { new example1(); } } /** Internal frame containing a button */ class intframe extends JInternalFrame { public intframe() { getContentPane().setLayout(new BorderLayout()); getContentPane().add(new JButton("Internal Frame")); setSize(200,200); setVisible(true); } } Listing Two import java.awt.*; import java.awt.event.*; import com.sun.java.swing.*; /** Frame with menu bar, toolbar and two actions. */ public class example2 extends JFrame { JMenuBar _menubar = new JMenuBar(); JToolBar _toolbar = new JToolBar(); Action _action1 = new Action1(); Action _action2 = new Action2(); public example2() { super("example2"); getContentPane().setLayout(new BorderLayout()); // Create menu and add actions JMenu menu = new JMenu("Menu",false); menu.add(_action1); menu.add(_action2); _menubar.add(menu); setJMenuBar(_menubar); // Create toolbar and add actions _toolbar.add(_action1); _toolbar.add(_action2); getContentPane().add(_toolbar,"North"); // Add listener to window to exit on window close addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Size and display frame setVisible(true); setSize(500,400); } /** Private inner class for action 1 */ private class Action1 extends AbstractAction { public Action1() { super("Action1"); } public void actionPerformed(ActionEvent e) { System.out.println("You requested action 1"); } } /** Private inner class for action 2 */ private class Action2 extends AbstractAction { public Action2() { super("Action2"); } public void actionPerformed(ActionEvent e) { System.out.println("You requested action 2"); } } public static void main(String[] args) { new example2(); } } Listing Three import java.awt.*; import java.awt.event.*; import com.sun.java.swing.*; import com.sun.java.swing.text.*; /** Frame with text pane and buttons to display red and blue text. */ public class example3 extends JFrame { private JTextPane _display = new JTextPane(); private JButton _redbutton = new JButton("Add Red Text"); private JButton _bluebutton = new JButton("Add Blue Text"); private StyleContext _styles = new StyleContext(); public example3() { super("example3"); // Create panel to hold blue and red buttons JPanel buttonpanel = new JPanel(); buttonpanel.setLayout(new FlowLayout(FlowLayout.CENTER)); buttonpanel.add(_redbutton); buttonpanel.add(_bluebutton); // Add button panel in the north and text pane in center getContentPane().setLayout(new BorderLayout()); getContentPane().add(buttonpanel,"North"); getContentPane().add(_display,"Center"); // Define named styles for displaying blue and red text Style def = _styles.getStyle(StyleContext.DEFAULT_STYLE); Style bluestyle = _styles.addStyle("blue",def); Style redstyle = _styles.addStyle("red",def); StyleConstants.setForeground(bluestyle, Color.blue); StyleConstants.setForeground(redstyle, Color.red); // Add listener to blue button to display blue text _bluebutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { display("Here is some blue text\n","blue"); } }); // Add listener to red button to display red text _redbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { display("Here is some red text\n","red"); } }); // Add listener to window to exit on window close addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Size and display the frame setSize(500,400); setVisible(true); } /** Display text using specified style */ public void display( String text, String style ) { Document doc = _display.getDocument(); try { doc.insertString(doc.getLength(),text,_styles.getStyle(style)); } catch (Exception e) { System.out.println("Error displaying text!"); e.printStackTrace(); } } public static void main(String[] args) { new example3(); } } Listing Four import com.ms.wfc.app.*; import com.ms.wfc.core.*; import com.ms.wfc.ui.*; /** MDI container form with button that creates MDI client frames. */ public class example4 extends Form { Container components = new Container(); Button button1 = new Button(); public example4() { setSize(new Point(300, 300)); setText("example4"); setIsMDIContainer(true); button1.setDock(ControlDock.TOP); button1.setText("Add MDI Client Frame"); // Add event handler to button button1.addOnClick(new EventHandler(button1_click)); setNewControls(new Control[] { button1 }); } /** Handle button click by creating new MDI client frame. */ private void button1_click(Object sender, Event e) { mdiframe frame = new mdiframe(); frame.setMDIParent(this); frame.setVisible(true); } public static void main(String args[]) { Application.run(new example4()); } } /** MDI client frame with a button. */ class mdiframe extends Form { Container components = new Container(); Button button1 = new Button(); public mdiframe() { button1.setDock(ControlDock.FILL); button1.setText("MDI Client Frame"); this.setNewControls(new Control[] { button1 }); } } Listing Five import com.ms.wfc.app.*; import com.ms.wfc.core.*; import com.ms.wfc.ui.*; /** Frame with menu bar, toolbar and two actions. */ public class example5 extends Form { Container components = new Container(); MainMenu mainMenu1 = new MainMenu(); MenuItem menuItem1 = new MenuItem(); MenuItem menuItem2 = new MenuItem(); MenuItem menuItem3 = new MenuItem(); ToolBar toolBar1 = new ToolBar(); Button button1 = new Button(); Button button2 = new Button(); public example5() { setText("example5"); setSize(new Point(300, 300)); // Create menu and add event handlers menuItem2.setText("Action1"); menuItem2.addOnClick(new EventHandler(do_action1)); menuItem3.setText("Action2"); menuItem3.addOnClick(new EventHandler(do_action2)); menuItem1.setText("Menu"); menuItem1.setMenuItems(new MenuItem[] {menuItem2, menuItem3}); mainMenu1.setMenuItems(new MenuItem[] {menuItem1}); setMenu(mainMenu1); // Create toolbar buttons and add event handlers toolBar1.setDock(ControlDock.TOP); button1.setLocation(new Point(0, 0)); button1.setText("Action1"); button1.addOnClick(new EventHandler(do_action1)); button2.setLocation(new Point(75, 0)); button2.setText("Action2"); button2.addOnClick(new EventHandler(do_action2)); setNewControls(new Control[] { button2, button1, toolBar1}); } private void do_action1(Object sender, Event e) { System.out.println("You requested action 1"); } private void do_action2(Object sender, Event e) { System.out.println("You requested action 2"); } public static void main(String args[]) { Application.run(new example5()); } } Listing Six import com.ms.wfc.app.*; import com.ms.wfc.core.*; import com.ms.wfc.ui.*; /** Shows inserting colored text into a RichEdit control. */ public class example6 extends Form { Button btnRed = new Button(); Button btnBlue = new Button(); RichEdit richEdit1 = new RichEdit(); Container components = new Container(); public example6() { initForm(); } public void dispose() { super.dispose(); components.dispose(); } private void btnRed_click(Object source, Event e) { appendText("This is red text\n",Color.RED); } private void btnBlue_click(Object source, Event e) { appendText("This is blue text\n",Color.BLUE); } public void appendText(String str, Color col) { int begin = richEdit1.getText().length(); richEdit1.select(begin,begin); Clipboard.setDataObject(str); richEdit1.paste(); int end = richEdit1.getText().length(); richEdit1.select(begin,end-1); richEdit1.setSelColor(col); richEdit1.select(0,0); } private void initForm() { this.setText("example6"); this.setAutoScaleBaseSize(new Point(5, 13)); this.setBorderStyle(FormBorderStyle.FIXED_DIALOG); this.setClientSize(new Point(328, 211)); btnRed.setLocation(new Point(8, 8)); btnRed.setSize(new Point(75, 23)); btnRed.setText("Red Text"); btnRed.addOnClick(new EventHandler(this.btnRed_click)); btnBlue.setLocation(new Point(88, 8)); btnBlue.setSize(new Point(75, 23)); btnBlue.setText("Blue Text"); btnBlue.addOnClick(new EventHandler(this.btnBlue_click)); richEdit1.setLocation(new Point(8, 40)); richEdit1.setSize(new Point(312, 160)); this.setNewControls(new Control[] {richEdit1,btnBlue,btnRed}); } public static void main(String args[]) { Application.run(new example6()); } } 8