public class Test extends JFrame {


private JPanel contentPane;


public static void main(String[] args) {

Test frame = new Test();

frame.setVisible(true);

}


public Test() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

this.setLocation(200, 200);

//프레임, 다이얼로그에서 setLocation(X,Y); 함수를 호출한다.

}

}

'컴퓨터공학 > Java Scala' 카테고리의 다른 글

JAVA 특징 장점 단점  (0) 2013.01.03
자바 Int형 변수를 돈 형식 스트링으로 리턴  (0) 2012.06.07
자바 파일 다이얼로그  (0) 2012.06.01
자바 GUI 이벤트 처리 방법  (0) 2012.06.01
자바 배열 복사  (0) 2012.06.01

public class Test extends JFrame implements ActionListener{


private JPanel contentPane;


public static void main(String[] args) {

Test frame = new Test();

frame.setVisible(true);

}


public Test() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JButton btnNewButton = new JButton("버튼");

btnNewButton.setFont(new Font("굴림", Font.PLAIN, 30));

btnNewButton.setBounds(76, 72, 240, 102);

contentPane.add(btnNewButton);

btnNewButton.addActionListener(this);

btnNewButton.setActionCommand("event");

}


@Override

public void actionPerformed(ActionEvent e) {

switch(e.getActionCommand())

{

case "event":

//this는 프레임을 가르킴

FileDialog fileDialog = new FileDialog(this,"파일 열기");

//생성자 호출시 다이얼로그가 만들어진다.


JOptionPane.showMessageDialog(null,fileDialog.getDirectory()fileDialog.getFile());

//getFile() 함수를 통해 파일 이름을 알 수 있다.

break;

}

}

}

1. 익명 클래스를 만들어서 클래스 안에 구현을 한다.


이 방법은 간단한 내용을 구현할 때 사용한다.


하지만, 이벤트를 부르는 클래스의 필드 접근이 번거로워서 잘 사용하지 않는다.


public class Test extends JFrame {


private JPanel contentPane;


public static void main(String[] args) {

Test frame = new Test();

frame.setVisible(true);

}


public Test() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JButton btnNewButton = new JButton("버튼");

btnNewButton.setFont(new Font("굴림", Font.PLAIN, 30));

btnNewButton.setBounds(76, 72, 240, 102);

contentPane.add(btnNewButton);

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(null,"이벤트 실행됨");

}

});

}

}


2. 클래스에 ActionLister 인터페이스를 구현


이 방법은 내가 주로 사용하는 방법이다.


클래스의 필드 접근 및 함수 접근이 용이하다


public class Test extends JFrame implements ActionListener{//ActionListener 구현


private JPanel contentPane;


public static void main(String[] args) {

Test frame = new Test();

frame.setVisible(true);

}


public Test() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JButton btnNewButton = new JButton("버튼");

btnNewButton.setFont(new Font("굴림", Font.PLAIN, 30));

btnNewButton.setBounds(76, 72, 240, 102);

contentPane.add(btnNewButton);

btnNewButton.addActionListener(this);

//ActionListener를 구현한 현재 클래스를 매개변수로 넘김


btnNewButton.setActionCommand("event");

//이벤트 컴포넌트 중 어느 컴포넌트에서 실행했는지 구분하기 위해

//다음과 같이 ActionCommand를 지정한다.

}


@Override

public void actionPerformed(ActionEvent e) {//ActionListener를 implements한 함수

switch(e.getActionCommand())//이벤트에서 지정된 ActionCommand를 가져옴

{

//자바 7 에서는 switch구문에 string를 사용할 수 있다.

//자바 하위버전 및 안드로이드에서는 안된다.....


case "event"://지정한 ActionCommand

JOptionPane.showMessageDialog(null,"이벤트 실행됨");

break;

}

}

}



'컴퓨터공학 > Java Scala' 카테고리의 다른 글

자바 Frame,Dialog 초기 위치 설정  (0) 2012.06.06
자바 파일 다이얼로그  (0) 2012.06.01
자바 배열 복사  (0) 2012.06.01
자바 파일 char 한 글자씩 가져오기  (0) 2012.05.31
자바 10진수 16진수간 변환  (0) 2012.05.28

public class Test {

public static void main(String[] args) {

char original[] = "0123456789".toCharArray();

char copy[] = "abcdefghi".toCharArray();

//System.arraycopy(오리지날 배열 이름, 오리지날 배열 위치, 카피 배열 이름, 카피 배열의 위치, 길이);

//original 배열의 1~3 (길이가 3) 을 copy 배열의 cde 자리


에 복사

System.arraycopy(original, 1, copy, 2, 3);

System.out.println(copy);

}

}




기본형 뿐만 아니라, 모든 객체가 복사 가능합니다.

public static void main(String[] args) 

{

try {

FileReader reader = new FileReader("text.txt");

int c;

while((c = reader.read()) != -1)

{

System.out.println((char)c);

}

catch (FileNotFoundException e) 

{

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}

public class Test {

public static void main(String[] args) {

System.out.println("10진수 -> 16진수");

System.out.println("10 -> " +Integer.toHexString(10));

System.out.println();

System.out.println("16진수 -> 10진수");

System.out.println("A -> " +  Integer.parseInt("A", 16));

}

}





예제...


public class Test {

public static void main(String[] args) {

Scanner scanner = new Scanner(new StringReader("123123\r\n4556"));

while(scanner.hasNextLine())

System.out.println(scanner.nextLine());

}

}


결과




public static boolean isPhoneNum(String s)

{

Pattern telephone = Pattern.compile("(\\d{3})-(\\d{3,4})-(\\d{4})");

Matcher m = telephone.matcher(s); 

if (m.matches()){

return true;

}

else {

return false;

   }

}

자바 GUI 프로그래밍을 쉽게 하기 위해 쓰는 툴인 Window Builder


이클립스에서 


help - install new software


http://dl.google.com/eclipse/inst/d2wbpro/latest/3.7

입력(불러오기)


File file = new File("file.txt");

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"euc-kr"));

while((line = br.readLine()) != null)

{

//line 으로 원하는 작업 수행하세요...

}


출력


File file = new File("msfile.txt");

BufferedWriter

 br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true),"euc-kr"));

+ Recent posts