본문 바로가기

Subject/Programming

JAVA] JFrame을 이용한 메모장 - 1) 기본 창 만들기, 메뉴바 만들기

0.기본 창 만들기


//main

new MyNoteFrame("Notepad"); //프레임 객체 생성



//MyNoteFrame Class

extends JFrame

{


public MyNoteFrame(String title)

{

super(title);

     this.setSize(500, 500); 

     this.setVisible(true);  //true여야 창이 보임

     this.setDefaultCloseOperation(EXIT_ON_CLOSE);  //exit 눌렀을 때, 실제로 프로세스를 종료시키는 옵션

}

}




1. 메뉴바 만들기


//MyNoteFrame 생성자

{

createMenuBar();

}



//createMenuBar()

{

menuBar = new JMenuBar(); //메뉴바 생성

this.setJMenuBar(menuBar); //이 메소드를 호출해야 메뉴바가 나타남.


menuFile= new JMenu("파일");

menuBar.add(menuFile); //메뉴바에 "파일" 메뉴 추가

///////////////////////////

menuItemNew = new JMenuItem("새 글"); //파일메뉴의 하위 메뉴인 "새 글" 추가 

menuFile.add(menuItemNew);

}


//모습




2. 툴버튼 만들기


//MyNoteFrame 생성자

{

createToolBar();

}



//createToolBar()

{

toolBar = new JToolBar(); //툴바 생성

this.add(BorderLayout.NORTH, toolBar);

java.net.URL imgUrl = MyNoteFrame.class.getResource("New.png"); //외부 프로젝트를 오픈한 경우, 

이미지 가져올 때 이렇게 해야함. 

toolNew = new JButton(new ImageIcon(imgUrl));

toolBar.add(toolNew); //툴바에 "New"버튼 추가

//////////////

toolBar.setFloatable(false); //툴바 드래그 잠금

}


//모습