IT_Programming/Java

[AWT_CheckBox] 동적으로 객체를 생성하고 제거하기

JJun ™ 2008. 10. 16. 01:20

package Test;

 

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

 

/**
 * Example: Dynamically add or remove the CheckBox Object
 * This is the sample code which dynamically add or remove the CheckBox Object in Frame. 
 */

public class ProblemInputView extends Frame
{
      private int countLength = 0;
 
      public ProblemInputView()
      {
           super();
           setLayout(new FlowLayout());
  
           FucListener();
 
          setSize(500, 500);
          setVisible(true);
     }
 
     private void FucListener()
     {
        addMouseListener( new MouseAdapter()
        {
              Checkbox cb;
              ArrayList<Checkbox> ArrCB = new ArrayList<Checkbox>();
   
              public void mouseClicked(MouseEvent me)
              {
                   if(me.getButton() == me.BUTTON1) // Check if the left button is clicked
                   {
                       cb = new Checkbox("hello"+countLength); // Construct CheckBox Object(cb)
                       ArrCB.add(cb); // add the cb into the ArrayList
                       ++countLength; // The Number of CheckBox Objects (Count Operation)
                
                       add(cb); // add cb into the main Frame
                       validate(); // Validate the object we just added
                   }
                   else if(me.getButton() == me.BUTTON3) // else if the right button is clicked
                   { 
                       if(countLength > 0)
                       { 
                           remove(ArrCB.get(--countLength)); // remove the object we just added...
                           ArrCB.remove(countLength); // remove the object of ArrayList.
                           invalidate(); // Invalidate the object which has been lost
                       }
                  }
           } 
    });

   

    addWindowListener( new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {    
             e.getWindow().setVisible(false);
             e.getWindow().dispose();
             System.exit(0);
        }
    });
 }
 
 public static void main(String[] args)
 {
     new ProblemInputView();
 } 
}

 

'IT_Programming > Java' 카테고리의 다른 글

JInternalFrame 예제 .. MDI 만들기..  (0) 2008.11.09
SWING 주요 컴포넌트 클래스  (0) 2008.11.09
JAVA Perfomance Tuning 1, 2, 3  (0) 2008.09.08
Java performance tips  (0) 2008.09.08
DB 관련된 한글 처리 방법  (0) 2008.09.08