User Interface Development (C# vs Java)

You can use the .NET Framework's rich set of windows forms components in C# for programming client-side forms applications.

Java

Most java applications use Abstract Windowing ToolKit (AWT) or Swing which uses the AWT infrastructure, including the AWT event model, for forms programming. AWT provides all the basic GUI capabilities and classes.

Java Example

A frame, a window with a title and border, is typically used for adding your components.

JFrame aframe = new JFrame();

The Component class, an object with graphical representation, is typically extended and the methods inherited are used or typically over-ridden, such as the paint method of a Shape component in the code illustrated.

    import java.awt.*;
    import javax.swing.*;
    
    class aShape extends JComponent {
    public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    
    // Draw the shape.
    }
    
    public static void main(String[] args) {
    JFrame aframe = new JFrame();
    frame.getContentPane().add(new aShape ());
    int frameWidth = 300;
    int frameHeight = 300;
    frame.setSize(frameWidth, frameHeight);
    frame.setVisible(true);
    }
}

You can register to listen on an action event for a component to handle events. For example, when a button is pressed and released, AWT sends an instance of ActionEvent to that button, by calling processEvent on the button. The button's processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button.

C#

In C#, the System.Windows.Forms namespace and classes of the .NET Framework provide a comprehensive set of components for windows forms development. For example, the following code uses Label, Button, and MenuStrip.

C# Example

Simply derive from the Form class, as follows:

public partial class Form1 : System.Windows.Forms.Form

And add your components:

this.button1 = new System.Windows.Forms.Button();
this.Controls.Add(this.button1);

The following code illustrates how to add a label, a button, and a menu to a form.

namespace WindowsFormApp
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        private System.ComponentModel.Container components = null;

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.MenuStrip menu1;

        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();

            this.label1 = new System.Windows.Forms.Label();
            this.Controls.Add(this.label1);

            this.button1 = new System.Windows.Forms.Button();
            this.Controls.Add(this.button1);

            this.menu1 = new System.Windows.Forms.MenuStrip();
            this.Controls.Add(this.menu1);
        }

        static void Main() 
        {
            System.Windows.Forms.Application.Run(new Form1());
        }
    }
}

Like Java, in C# you can register to listen on an event for a component. For example, when a button is pressed and released, the run time sends a Click event to any listeners that have registered an interest in the Click event of this button.

private void button1_Click(object sender, System.EventArgs e)
{
}

You can use the following code to register button1_Click to handle the Click event of an instance of Button called button1.

// this code can go in InitializeComponent()
button1.Click += button1_Click;

For more information, see Creating ASP.NET Web Applications (Visual C#).

For more information about the Forms classes, see Windows Forms Controls by Function and System.Windows.Forms.

See Also

Concepts

C# Programming Guide

Designing a User Interface (Visual C#)

Other Resources

C# for Java Developers