다음을 통해 공유


사용자 인터페이스 개발(C# 및 Java)

업데이트: 2007년 11월

C#에서 .NET Framework의 풍부한 Windows Forms 구성 요소 집합을 사용하여 클라이언트측 폼 응용 프로그램을 프로그래밍할 수 있습니다.

Java

대부분의 java 응용 프로그램에서는 AWT(Abstract Windowing ToolKit)를 사용하여 폼을 프로그래밍하거나 AWT 이벤트 모델이 포함된 AWT 인프라를 사용하는 Swing을 통해 폼을 프로그래밍합니다. AWT에서는 모든 기본 GUI 기능과 클래스를 제공합니다.

Java 예제

제목과 테두리가 있는 창인 프레임은 구성 요소를 추가하는 데 일반적으로 사용됩니다.

JFrame aframe = new JFrame();

그래픽으로 표현되는 개체인 Component 클래스는 예제 코드에 나와 있는 Shape 구성 요소의 paint 메서드 같이 일반적으로 확장되고 상속된 메서드가 사용되거나 일반적으로 재정의됩니다.

    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);
    }
}

구성 요소에 대한 동작 이벤트를 수신하도록 등록하여 이벤트를 처리할 수 있습니다. 예를 들어, 단추를 눌렀다 놓으면 AWT에서는 단추에 대한 processEvent를 호출하여 이 단추에 ActionEvent의 인스턴스를 전달합니다. 단추의 processEvent 메서드는 단추에 대한 모든 이벤트를 전달받고 자체 processActionEvent 메서드를 호출하여 동작 이벤트를 함께 전달합니다. 이 두 번째 메서드는 이 단추에서 생성되는 동작 이벤트와 관련하여 등록된 모든 동작 수신기에 동작 이벤트를 전달합니다.

C#

C#에서는 .NET Framework의 System.Windows.Forms 네임스페이스와 클래스를 통해 Windows Forms 개발에 필요한 풍부한 구성 요소 집합을 제공합니다. 예를 들어, 다음 코드에서는 Label, ButtonMenuStrip을 사용합니다.

C# 예제

다음과 같이 Form 클래스에서 간단하게 파생합니다.

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

그런 다음 구성 요소를 추가합니다.

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

다음 코드에서는 레이블, 단추 및 메뉴를 폼에 추가하는 방법을 보여 줍니다.

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());
        }
    }
}

Java와 마찬가지로 C#에서 구성 요소에 대한 이벤트를 수신하도록 등록할 수 있습니다. 예를 들어, 단추를 눌렀다 놓으면 이 단추의 Click 이벤트와 관련하여 등록된 모든 수신기에 Click 이벤트가 런타임에 전달됩니다.

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

다음과 같은 코드를 사용하여 button1_Click을 등록하고 button1이라는 Button의 인스턴스에 대한 Click 이벤트를 처리할 수 있습니다.

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

자세한 내용은 ASP.NET 웹 응용 프로그램 만들기(Visual C#)를 참조하십시오.

Forms 클래스에 대한 자세한 내용은 기능별 Windows Forms 컨트롤System.Windows.Forms을 참조하십시오.

참고 항목

개념

C# 프로그래밍 가이드

사용자 인터페이스 디자인(Visual C#)

기타 리소스

Java 개발자를 위한 C#