import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.FileStoreAttributeView;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.basic.BasicCheckBoxMenuItemUI;;
public class MenuEx extends JFrame implements ActionListener{
JScrollPane sp;
JTextArea ta = new JTextArea("메뉴 만들기");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("파일");
JMenuItem newMenu = new JMenuItem("새글");
JMenuItem openMenu = new JMenuItem("열기");
JMenuItem saveMenu = new JMenuItem("저장");
JMenuItem endMenu = new JMenuItem("종료");
JMenu editMenu = new JMenu("편집");
JMenuItem copyMenu = new JMenuItem("복사");
JMenuItem cutMenu = new JMenuItem("오려두기");
JMenuItem pasteMenu = new JMenuItem("붙여넣기");
JMenu formMenu = new JMenu("서식");
JMenuItem fontMenu = new JMenuItem("글꼴");
JMenu colorMenu = new JMenu("색상표");
JMenuItem textColor = new JMenuItem("글자색");
JMenuItem backColor = new JMenuItem("배경색");
JMenu runMenu = new JMenu("실행");
JMenuItem chromMenu = new JMenuItem("크롬");
JMenuItem exepMenu = new JMenuItem("익스플로러");
JMenuItem memoMenu = new JMenuItem("메모장");
String textBuffer; //복사, 오려두기한 문자 저장 할 변수
//Dialog(내부 클래스) 객체 생성
FontDialog fDialog = new FontDialog();
public MenuEx() {
//단축키 설정
//컨트로+N 으로 새창열기
newMenu.setMnemonic(KeyEvent.VK_N);
newMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
//알트+O 으로 열기창 열기
openMenu.setMnemonic(KeyEvent.VK_O);
openMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
// 종료하기
endMenu.setMnemonic(KeyEvent.VK_X);
endMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.ALT_MASK));
//이벤트 등록
newMenu.addActionListener(this); //파일
openMenu.addActionListener(this);
saveMenu.addActionListener(this);
endMenu.addActionListener(this);
copyMenu.addActionListener(this); //편집
cutMenu.addActionListener(this);
pasteMenu.addActionListener(this);
fontMenu.addActionListener(this); //서식
textColor.addActionListener(this);
backColor.addActionListener(this);
chromMenu.addActionListener(this); //실행
exepMenu.addActionListener(this);
memoMenu.addActionListener(this);
//메뉴 만들기
//파일
fileMenu.add(newMenu); fileMenu.add(openMenu); fileMenu.add(saveMenu);
fileMenu.addSeparator();//경계선 만들기
fileMenu.add(endMenu);
menuBar.add(fileMenu);
//편집
editMenu.add(copyMenu); editMenu.add(cutMenu); editMenu.add(pasteMenu);
menuBar.add(editMenu);
//서식
formMenu.add(fontMenu); formMenu.add(colorMenu);
colorMenu.add(textColor); colorMenu.add(backColor);
menuBar.add(formMenu);
//실행
runMenu.add(chromMenu); runMenu.add(exepMenu); runMenu.add(memoMenu);
menuBar.add(runMenu);
setJMenuBar(menuBar); //JFrame 컨테이너에 메뉴바 추가
sp = new JScrollPane(ta);
add(sp);
setSize(800, 500);
setVisible(true);
}
//내부 클래스 시작
class FontDialog extends JDialog implements ActionListener{
JPanel northPane = new JPanel(new GridLayout(1, 3));
JLabel lbl1 = new JLabel("글꼴");
JLabel lbl2 = new JLabel("글꼴 유형");
JLabel lbl3 = new JLabel("글자 크기");
JPanel centerPane = new JPanel(new GridLayout(1, 3));
JScrollPane sp1, sp3;
JList<String> list1 = new JList<String>();
JList<String> list2 = new JList<String>();
JList<Integer> list3 = new JList<Integer>();
JPanel southPane = new JPanel();
JButton okBtn = new JButton("확인");
JButton cancleBtn = new JButton("취소");
//List 모델 만들기
DefaultListModel<String> fontModel = new DefaultListModel<String>();
DefaultListModel<String> typeModel = new DefaultListModel<String>();
DefaultListModel<Integer> sizeModel = new DefaultListModel<Integer>();
FontDialog(){
setModal(true); //모달 세팅 : dialog창 뒤면 비활성화
northPane.add(lbl1);northPane.add(lbl2); northPane.add(lbl3);
add(northPane, "North");
//List 목록 만들기
//시스템에서 지원하는 글꼴정보 얻어오기
String fontName[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(int i=0; i<fontName.length; i++){
fontModel.addElement(fontName[i]);
}
list1.setModel(fontModel);
//글자 유형 만들기
String typeName[] = {"보통", "진하게", "기울림꼴"};
for(int i=0; i<typeName.length; i++){
typeModel.addElement(typeName[i]);
}
list2.setModel(typeModel);
//글자 크기
for(int i=8; i<100; i+=2){
sizeModel.addElement(i);
}
list3.setModel(sizeModel);
sp1 = new JScrollPane(list1);
sp3 = new JScrollPane(list3);
centerPane.add(sp1); centerPane.add(list2); centerPane.add(sp3);
add(centerPane, "Center");
southPane.add(okBtn);
southPane.add(cancleBtn);
add(southPane, "South");
setSize(300, 300);
//이벤트 등록 : 두개의 버튼은 클릭하면 내부 클래스 오버라이딩으로 넘어간다.
okBtn.addActionListener(this);
cancleBtn.addActionListener(this);
}
//내부 클래스 오버라이딩
public void actionPerformed(ActionEvent ae){
String eventValue = ae.getActionCommand();
if(eventValue.equals("확인")){
Font fnt = new Font(list1.getSelectedValue(), list2.getSelectedIndex(), list3.getSelectedIndex());
ta.setFont(fnt);
setVisible(false);
}else if(eventValue.equals("취소")){
//dispose
setVisible(false);
}
}
}
//내부 클래스 끝
//오버라이딩
public void actionPerformed(ActionEvent ae){
Object obj = ae.getSource();
if(obj == endMenu){
dispose(); System.exit(0);
}else if(obj == newMenu){ //새글
ta.setText("");
}else if(obj == textColor){ //글자색
setTextColor();
}else if(obj == backColor){ //배경색
setBackColor();
}else if(obj == memoMenu){ //메모장 실행
run("notepad.exe");
}else if(obj == chromMenu){ //크롬 실행
run("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
}else if(obj == exepMenu){ //ie 실행
run("C:/Program Files/Internet Explorer/iexplore.exe");
}else if(obj == openMenu){ //열기
fileOpen();
}else if(obj == saveMenu){ //저장
fileSave();
}else if(obj == copyMenu){ //복사하기
textCopy();
}else if(obj == pasteMenu){ //붙여넣기
textPaste();
}else if(obj == cutMenu){ //오려두기
textCut();
}else if(obj == fontMenu){
fDialog.setVisible(true);
}
}
//오려두기
public void textCut(){
textBuffer = ta.getSelectedText();
ta.replaceSelection("");
}
//붙여넣기
public void textPaste(){
ta.replaceSelection(textBuffer);
}
//복사 실행
public void textCopy(){
textBuffer = ta.getSelectedText();
}
//저장 실행
public void fileSave(){
JFileChooser fc = new JFileChooser();
int fileCount = fc.showSaveDialog(this);
File f = fc.getSelectedFile();
//getPath() : 경로 + 파일명, getName() : 파일명 구함
ta.append(f.getPath()+"="+f.getName()+"\n");
}
//열기 실행
public void fileOpen(){
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true); //다중 선택 기능
//필터 객체 생성
FileFilter ff1 = new FileNameExtensionFilter("java", "Java", "JAVA", "JaVa");
fc.addChoosableFileFilter(ff1);//JFilechooser 객체에 필터 세팅
FileFilter ff2 = new FileNameExtensionFilter("class", "Class", "CLASS");
fc.addChoosableFileFilter(ff2);//JFilechooser 객체에 필터 세팅
FileFilter ff3 = new FileNameExtensionFilter("png", "jpg", "gif");
fc.addChoosableFileFilter(ff3);//JFilechooser 객체에 필터 세팅
//선택된 파일의 갯수가 리턴됨
int fileSelectCount = fc.showOpenDialog(this);
File f[] = fc.getSelectedFiles(); //선택된 파일 목록 얻어오기
for(int i=0; i <f.length; i++){
ta.append(f[i].getName()+"\n"); //파일명
}
}
//메모장 실행 예외처리
public void run(String programName){
Runtime run = Runtime.getRuntime();
try{
run.exec(programName);
}catch(IOException ie){
System.out.println(ie.getMessage()+"예외발생");
}
}
public void setTextColor(){ //글자색
JColorChooser cc = new JColorChooser();
// 부모컨테이너, 창제목, 기본컬러
Color color = cc.showDialog(this, "글자색", Color.black);
ta.setForeground(color);
}
public void setBackColor(){
JColorChooser cc = new JColorChooser();
Color color = cc.showDialog(this, "배경색", Color.white);
ta.setBackground(color);
}
public static void main(String[] args) {
new MenuEx();
}
}
'응용 SoftWare > JAVA' 카테고리의 다른 글
[Swing] 달력만들기 (0) | 2016.12.16 |
---|---|
FileReader (0) | 2016.12.16 |
[예제] 은행 대기열 만들기 (0) | 2016.12.14 |
HashMap (0) | 2016.12.14 |
TreeSet (0) | 2016.12.14 |