반응형
 


//action event 처리하기

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

public class Action implements ActionListener{
 Frame f;
 Button b;
 public static void main(String args[]){
  Action a = new Action();
 }
 public Action(){
  f = new Frame();
  b = new Button("QUIT");
 
  f.add(b);
  b.addActionListener(this);
  f.setSize(200,200);
  f.setVisible(true);
 }
 public void actionPerformed(ActionEvent e){
  b.setBackground(Color.red);
  b.setLabel(""QUIT");
 }
}

//addActionListener()를 통해 해당 버튼의 이벤트를 Frame에 등록한다.
//해당 버튼이 클릭이 일어나면 public vodi actionPerformed(ActionEvent e)에 의해 상태가 변하고, 해당하는 코드가 실행된다.


//앞의 코드는 버튼이 었고 이번에는 윈도우에 대한 이벤트를 다루어 보자

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

public class Action implements WindowListener{
 Frame f;
 public static void main(String args[]){
  Action a = new Action();
 }
 public Action(){
  f = new Frame();
  f.addWindowListener(this);
  f.setSize(200,200);
  f.setVisible(true);
 }
 public void windowClosing(WindowEvent e){
  System.exit(0);
 }
 public void windowActivated(WindowEvent e){}
 public void windowClosed(WindowEvent e){}
 public void windowOpened(WindowEvent e){}
 public void windowDeactivated(WindowEvent e){}
 public void windowDeiconified(WindowEvent e){}
 public void windowIconified(WindowEvent e){}
}

addWindowListener()로 이벤트 코드를 등록한 뒤 메소드 구현한다.
 그러나, 리스너 인터페이스들을 implements 했을 때는 필요없는 메소들도 모두 구현해야 하는 불편함이 있기 때문에, 이를 추상 메소드로 만든 클래스인 어댑터클래스를 사용할 수 있다.


<Listener interface>

ComponentListener : ComponentAdapter (나머지도 모두 이름 뒤에 Adaptor가 붙는다)

ContainerListener

FocusListener

MouseListener

MouseMotionListener

WindowListener

KeyListener

 


다음은 어댑터 클래스 사용의 예제이다.

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

public class Action extends WindowAdapter{
 Frame f;
 public static void main(String args[]){
  Action a = new Action();
 }
 public Action){
  f = new Frame();
  f.addWindowListener(this);
  f.setSize(200,200);
  f.setVisible(true);
 }
 public void windowClosing(WindowEvent e){
  System.exit(0);
 }
}

 

반응형
반응형

자바로 GUI를 윈도우 기반으로 했을 때, 버튼을 만들고 마우스로 클릭했을 때,
해당 Frame을 닫을 수 있게 하는 코드이다.

import java.awt.*;
public class JPlayer extends Frame implements ActionListener {

  private Button quit;
  quit = new Button("Quit");
  quit.addActionListener(this);

  JPlayer(){}
 
 public void actionPerformed(ActionEvent e) {
  String arg = e.getActionCommand();

  if ("QUIT".equals(arg)) {
   System.out.println("QUIT");
   dispose();
  }
 }
 public static void main(String arg[])
{
 JPlayer j = new JPlayer();
}
}
 

반응형
반응형

자바에서 문자와 숫자의 변환은 어떻게 이루어 지나요?

문자에서 숫자로 변환은 어떻게 하나요?

1. int A = Integer.parseInt("1000");

2. String s = "1000";

    int x = Integer.parInt(s);

 

3. double d = Double.parseDouble("100.4");


숫자에서 문자로 변환은 어떻게 하나요?
1. String str = Integer.toString(1000));

2. double d = 10.5;

    String  doubleStr = "" + d;

//String에 어떤 것을 붙이든 지 그 값은 String으로 변환됩니다.

 

3. double d = 10.5;

   String doubleStr  = Double.toString(d);

   

두 개를 적절히 잘 사용하면 매우 유용합니다. ^^

 

cf. Boolean

    Character

    Byte

    Short

    Integer

    Long

    Float

    Double

 

package java_lang;

public class StringEx7 {
 public static void main(String[] args) {
  int value = 100;
  String strValue = String.valueOf(value); //int를 String으로 변환한다.
  
  int value2 = 100;
  String strValue2 = value2 + ""; //int를 String으로 변환하는 또 다른 방법
  
  System.out.println(strValue);
  System.out.println(strValue2);
 }

}

반응형

자바, 문자열 문자열 나누기

개발 및 관리/Java 2008. 4. 1. 21:57 posted by HighLighter
반응형

제가 몰라서 참 고생한 적이 있는 정보입니다. 참 유용한 정보인 것 같습니다.

하나, StringTokenizer를 이용한 문자열 나누기

public void token(){

  String str = "ABC|DEF|GHI|JKL";
  StringTokenizer tokens = new StringTokenizer(str,"|");
 
  for(int i = 1;tokens.hasMoreTokens(); i++)
  {
   System.out.println("문자열 " + i + ":" + tokens.nextToken());
  }

 }

둘, split을 이용한 문자열 나누기

public void token(){

  String str1 = "ABC|DEF|GHI|JKL";
  String[] str2 = str1.split("|");
 
  for(int i = 0 ;i < str2.length : i++)
  {
   System.out.println("문자열 " + i + ":" + str2[i]);
  }
 }

반응형