'C/Java'에 해당되는 글 59건

  1. 2007.11.07 AWT>TextArea by 청웨일
  2. 2007.11.07 AWT>Button by 청웨일
  3. 2007.11.07 AWT>TextField by 청웨일
  4. 2007.11.07 GUI에 AWT컴포넌트 추가하기 by 청웨일
  5. 2007.11.07 Dialog 프로그래밍 by 청웨일

AWT>TextArea

C/Java : 2007. 11. 7. 11:59

- appebd() : 텍스트의 끝에 문자열 추가

- insert() : TextArea의 x 위치에 문자열 추가 후, 나머지 텍스트를 오른쪽으로 이동

- replaceRange() : 시작과 끝 지점을 지정하여 전달된 문자열을 사용해 택스트 대치


*

import java.awt.*;
import java.awt.event.*;


public class TestTextArea extends Frame implements ActionListener {
 Button btnExit;
 Button btnAppend;
 Button btnInsert;
 Button btnReplace;
 TextArea taLetter;
 
 public TestTextArea() {                                    //버튼 객체 생성
  btnAppend = new Button("Append");
  btnAppend.addActionListener(this);
  btnInsert = new Button("Insert");
  btnInsert.addActionListener(this);
  btnReplace = new Button("Replace");
  btnReplace.addActionListener(this);
  btnExit = new Button("Exit");
  btnExit.addActionListener(this);
 

//비어있는 TextArea 객체 생성
  taLetter = new TextArea("", 10, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);

//TextArea영역에 텍스트 생성
  taLetter.append("I am writing this letter to inform you that you");
  taLetter.append(" havebeen drafted into the United States Army.");
  taLetter.append(" You will report to Fort Bragg, North Carolina ");
  taLetter.append("on July 20, 1966. You will be assigned to ");
  taLetter.append("Vietnam.");
 
  add(btnAppend);
  add(btnInsert);
  add(btnReplace);
  add(btnExit);
  add(taLetter);
 
  this.setLayout(new FlowLayout());
 

//윈도우창 생성
  addWindowListener(new WinCloser());
  setTitle("Using a TextArea Object");
  setBounds(100,100,400,400);
  setVisible(true);
 }
 
 public void actionPerformed(ActionEvent ae) {    //각 버튼에 이벤트 적용
  if(ae.getActionCommand().equals("Append"))
   taLetter.append("\n\nSincerely, \n     The DraftBoard");   //Sincerely라인추가
  if(ae.getActionCommand().equals("Insert"))
   taLetter.insert("Dear Steve, \n     ",0);                           //인사말 추가
  if(ae.getActionCommand().equals("Replace"))
   taLetter.replaceRange("Dear Jerry, \n     ", 0,12);             //다른인사말로 교체/대치
  if(ae.getActionCommand().equals("Exit"))
   System.exit(0);
 }
 
 public static void main(String[] args) {
  TestTextArea tta = new TestTextArea();
 }
}

class WinCloser extends WindowAdapter {
 public void WindowClosing(WindowEvent e) {
  System.exit(0);
 }
}

*


Posted by 청웨일

AWT>Button

C/Java : 2007. 11. 7. 11:58

java.awt.Button 클래스


- addActionListener() : 버튼이 클릭되면 통보받는다.

- setLabel() : 버튼을 위한 레이블 텍스트 설정

- removeActionListener() : 버튼과 리스터 클래스 사이의 연결 제거


* 프레임에 버튼 추가하기

import java.awt.*;
import java.awt.event.*;


public class TestButton extends Frame implements ActionListener {
 Button btnExit;                                  //버튼객체의 주소를 할당받을수 있는 변수
 
 public TestButton() {
  btnExit = new Button("Exit");               //실제 객체 생성

//Button의 레이블에 나타날 글꼴 제어
  btnExit.setFont(new Font("Courier", Font.BOLD, 24));
//배경색 설정
  btnExit.setBackground(Color.cyan);

//Cursor객체 : 마우스가 객체 위에 있을때 커서모양 제어
  Cursor curs = new Cursor(HAND_CURSOR);   //손모양 커서로 지정
  btnExit.setCursor(curs);
  btnExit.addActionListener(this);
 
  add(btnExit);
  this.setLayout(new FlowLayout());
 
  addWindowListener(new WinCloser());
  setTitle("Using a Button and an ActionListener");
  setBounds(100,100,300,300);
  setVisible(true);
 }
 //버튼이 클릭될때마다 호출된다.
 public void actionPerformed(ActionEvent ae) {
  if(ae.getActionCommand().equals("Exit"));  

//버튼이 클릭되었을때 어떤 버튼인지 설정한다.
  System.exit(0);
 }
 
 public static void main(String[] args) {
  TestButton td = new TestButton();
 }
}


class WinCloser extends WindowAdapter {
 public void WindowClosing(WindowEvent e) {
  System.exit(0);
 }
}

*


Posted by 청웨일

AWT>TextField

C/Java : 2007. 11. 7. 11:57

- addActionListener() : action이벤트가 발생했을때 통지를 받는다.

- setColumus() : 열의 수 설정

- setText() : 텍스트를 설정/수정한다.

- removeActionListener() : action 리스너 제거

- getListener() : 이 객체롸 연관된 모든 리스너를 배열로 반환


* 프레임에 TextField 객체를 위치시킨다.

import java.awt.*;
import java.awt.event.*;


public class TestTextFields extends Frame {
 TextField tfield1;
 TextField tfield2;
 
 public TestTextFields(){
  tfield1 = new TextField(15);                //보여질 글자수를 미리 설정하여 생성한다.
  tfield2 = new TextField(20);


  tfield1.setEchoChar('*');                    //모든 글자가 '*'로 찍히게 한다.
  tfield2.setText("Some Sample Text");    //기본값을 출력하도록 한다.
  tfield2.setFont(new Font("Couroer", Font.BOLD, 16));   //글꼴 지정
  tfield2.setEditable(false);                  //텍스트 수정 불가
  tfield2.select(12, 15);                        //지정된 텍스트를 블록지정한다.
 
  Panel p1 = new Panel();          //패널생성
  p1.add(tfield1);                      //패널 p1에 TextField1 추가
  p1.add(tfield2);                     //패널 p1에 TextField2 추가
  add(p1);                              //TestTextFields tp에 패널 추가
 
  addWindowListener(new WinCloser());
  setTitle("Using TextFields");
  setBounds(100,100,300,300);
  setVisible(true);
 }
 
 public static void main(String[] args) {
  TestTextFields tp = new TestTextFields();
 }
}


class WinCloser extends WindowAdapter {
 public void windowClosing(WindowEvent e) {
  System.exit(0);
 }
}

*

Posted by 청웨일

컴포넌트

java.awt.Component 클래스 확장


TextField : 한줄 텍스트(길이변화가능, 줄 넘김 불가)

    └ TextArea : 한줄 이상의 덱스트 박스, 스크롤바 지원

Button : 그래픽 컴포넌트

Label

CheckBox : 체크상태를 On/Off할수 있는 스위치박스, 그룹묶기 가능, 여러개 선택가능

    └ CheckBoxGroup : 선택 개수 제한

Choice : 풀다운 메뉴/리스트, 한개의 항목만 선택가능

List : Choice와 유사, 여러개의 항목 출력가능

Menu Component

   ├ MenuBar : 프레임에 첨부 가능, 한번에 한개만 선택가능한 풀다운 리스트

   └ MenuItem : 메뉴에서 항목표현, 선택되었을때 이벤트 발생

Posted by 청웨일

Dialog 프로그래밍

C/Java : 2007. 11. 7. 11:56

- Frame객체나 다른 Dialog는 항상 Dialog객체를 가지고 있다.

- 다이얼로그가 닫힐때까지 다른 처리를 잠그는 modal이거나

  다른 윈도우에서 작업하는 동안 열린 상태로 있게 해주는 modaless일수도 있다.

- Dialog객체는 윈도우 상태가 변할때 윈도우 이벤트가 발생한다.


*

import java.awt.*;
import java.awt.event.*;


public class TestDialog extends Frame implements ActionListener{
 Button btnExit;
 Button btnYes;
 Button btnNo;
 Dialog dlgConfirm;


 //TestDialog생성
 public TestDialog() {
  btnExit = new Button("Exit");             //Exit버튼 생성
  btnExit.addActionListener(this);
  add(btnExit);
 
  this.setLayout(new FlowLayout());      //TestDialog를 위한 레이아웃관리자 설정
 
  dlgConfirm = new Dialog(this);           //다이얼로그를 생성하고 클래스에 부착
  dlgConfirm.setResizable(false);
 
  btnYes = new Button("Yes");             //다이얼로그에 버튼 추가
  btnYes.addActionListener(this);
  btnNo = new Button("No");
  btnNo.addActionListener(this);
  dlgConfirm.add(btnYes);
  dlgConfirm.add(btnNo);


  dlgConfirm.setTitle("Are you sure?");    //다이얼로그의 제목과 크기 설정
  dlgConfirm.setSize(200,100);


//디폴트 BorderLayout()은 서도 다른 버튼을 덮어쓸수 있기 때문에 쓰지 않았다.
  dlgConfirm.setLayout(new FlowLayout());
  addWindowListener(new WinCloser());
  setTitle("Using a Dialog");
  setBounds(100,100,300,300);
  setVisible(true);
 }
 

//actionPerformed는 버튼클릭과 같은 동작 action이벤트가 발생하면 자동으로 호출
 public void actionPerformed(ActionEvent ae) {
  if(ae.getActionCommand().equals("Exit"))
   dlgConfirm.show();                             //Dialog객체를 가져와서 출력한다.
  if(ae.getActionCommand().equals("Yes"))
   System.exit(0);
  if(ae.getActionCommand().equals("No"))
   dlgConfirm.setVisible(false);   //Dialog 사라짐
 }
 
 public static void main(String[] args) {
  TestDialog td = new TestDialog();
 }
}


class WinCloser extends WindowAdapter {
  public void windowClosing(WindowEvent e) {
   System.exit(0);
  }
}

*

Posted by 청웨일