본문 바로가기
프로그래밍/② JAVA(자바) 프로젝트

② JAVA(자바) 프로젝트-7 JFrame 안에 그림 넣기, 현재 파일위치 출력 == Insert image in JFrame, output current file position

by ronul 2017. 4. 29.
300x250

오늘 한 것은 JFrame 안에 그림을 넣어주고 현재 파일 명을 출력하는 것을 했습니다.

먼저 코드입니다.


import java.awt.*;
import javax.swing.*;

public class iconInput extends JFrame {
 Container contentPane;
 String thisLocation;
 JLabel lblImage = new JLabel();
 
 iconInput() {
 setTitle("현재 파일에서 그림 넣기");
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 contentPane = getContentPane();  
 inputIcon();
 setSize(500,500);
 setVisible(true);
 }
 void inputIcon(){
  thisLocation= System.getProperty("user.dir");
  lblImage.setIcon(new ImageIcon("koala.jpg"));
  contentPane.add(lblImage);
  
  System.out.println("현재 파일의 위치는 " + thisLocation + " 입니다~!");
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new iconInput();
  }

}


 

 

 

 

 

먼저 컨테이너와 string 형으로 thisLocation , 이미지를 넣을 label을 생성해줍니다.

이미지를 넣을 때는 라벨에다 넣어주는게 가장 좋다고 합니다.

이후 title과 크기 다같이 끌것인지 등을 정해줍니다

thisLocation= System.getProperty("user.dir"); 이것은 현재 파일 위치를 thisLocation에 넣어줍니다

그리고 이미지를 생성한 라벨에 넣어 줍니다.

system.out.prinln 은 현재 위치를 문자로 출력해줍니다.

 

이미지를 넣어줄때 위치는 현재 프로젝트가 있는 위치로부터 시작합니다.

현재 프로젝트 위치는 sysout 으로 찍어줬구요.

그폴더안에다 넣어주어야 합니다.

image 라는 폴더를 생성했을경우

new ImageIcon("image/koala.jpg"); 라고 적어주면 됩니다.

현재 파일 위치를 불러오는것은 나중에 프로젝트를 exe파일로 만들거나 할때 내가 참조(라이브러리 같은것) 한것이 필요한데 이 파일을 집어넣어준 공간이 매번 바뀌는 경우에 필요합니다.

파일 위치에 thisLocation적어주고 + "/참조한 파일명" 만 적어주면 되니가요~

300x250

댓글