Sidewinder


World


FishMusic


LaughingBaby


Thursday, September 4, 2008

java font




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.xml.crypto.Data;
import java.io.*;
import java.lang.reflect.Array;
import java.awt.CheckboxMenuItem;
import java.util.Calendar;
import java.awt.Font;
public class NotePad2Frame extends JFrame //implements TextListener
{
private ActionListener listener;
private TextListener txlistener;
private Font font;
JMenuBar bar;

JMenuItem NPrint,NClock,NFile,NEdit,NTool,NSave,NHelp,NExit,NFind,NChange,HTML,Def;
private Dialog d,dl;
private FileDialog fd;
private JTextPane Area;
private JLabel label;
private JPanel panel;
String str = "새문서";
String facename = "bold";


public NotePad2Frame()
{

bar = new JMenuBar();
JMenu file = new JMenu("FILE");
NFile = new JMenuItem("새문서");
NFile.setAccelerator(KeyStroke.getKeyStroke('N',Event.CTRL_MASK));
NFile.addActionListener(listener);
NEdit = new JMenuItem("불러오기");
NEdit.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK));
NEdit.addActionListener(listener);
NTool = new JMenuItem("저장하기");
NTool.addActionListener(listener);
NTool.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
JMenuItem NSave = new JMenuItem("다른이름으로저장");
NSave.addActionListener(listener);
NSave.setAccelerator(KeyStroke.getKeyStroke('A',Event.CTRL_MASK));
JMenuItem NPrint = new JMenuItem("인쇄");
NPrint.addActionListener(listener);
NPrint.setAccelerator(KeyStroke.getKeyStroke('P',Event.CTRL_MASK));
JMenuItem NExit = new JMenuItem("종료");
NExit.setAccelerator(KeyStroke.getKeyStroke('X',Event.CTRL_MASK));
NExit.addActionListener(listener);

file.add(NFile);
file.add(NEdit);
file.add(NSave);
//file.add(NHelp);
file.add(NPrint);
file.addSeparator();
file.add(NExit);

bar.add(file);
setJMenuBar(bar);

//여기까지 파일메뉴

JMenu edit = new JMenu("EDIT");
JMenuItem NFind = new JMenuItem("찾기");
NFind.setAccelerator(KeyStroke.getKeyStroke('F',Event.CTRL_MASK));
NFind.addActionListener(listener);
JMenuItem NChange = new JMenuItem("바꾸기");
NChange.setAccelerator(KeyStroke.getKeyStroke('W',Event.CTRL_MASK));
NChange.addActionListener(listener);
JMenuItem NSelectAll = new JMenuItem("모두선택");
NSelectAll.setAccelerator(KeyStroke.getKeyStroke('A',Event.CTRL_MASK));
NSelectAll.addActionListener(listener);
JMenuItem NClock = new JMenuItem("시간/날짜");
NClock.setAccelerator(KeyStroke.getKeyStroke("F5"));
edit.add(NFind);
edit.add(NChange);
edit.add(NSelectAll);
edit.add(NClock);

bar.add(edit);
setJMenuBar(bar);
//여기까지 Edit메뉴

JMenu Formula = new JMenu("FORMULA");
JMenuItem LineChange = new JMenuItem("자동 줄 바꿈");
LineChange.setAccelerator(KeyStroke.getKeyStroke('H',Event.ALT_MASK));
LineChange.addActionListener(listener);
JMenuItem NFont = new JMenuItem("글 꼴");
NFont.setAccelerator(KeyStroke.getKeyStroke('F',Event.ALT_MASK));
NFont.addActionListener(listener);

Formula.add(LineChange);
Formula.add(NFont);

bar.add(Formula);
setJMenuBar(bar);
//여기까지 서식(Formula)메뉴

JMenu Tool = new JMenu("TOOL");
JMenuItem HTML = new JMenuItem("html");

JMenuItem Def = new JMenuItem("사용자정의");
Def.addActionListener(listener);

Tool.add(HTML);
Tool.add(Def);

bar.add(Tool);
setJMenuBar(bar);
//여기까지 속성메뉴
setLayout(new BorderLayout());
Area = new JTextPane();
//Area.addTextListener(txlistener);

Area.setCaretColor(Color.blue);
Area.setSelectedTextColor(Color.green);
Area.setSelectionColor(Color.BLUE);
Area.setBackground(Color.green);
Area.setFont(new Font(null,font.ITALIC,15));
//Font클래스는 import를 할경우 선언을 안하고 사용이 가능하지만 Font의 static변수는
//전역변수로 꼭!! 선언을 하고 사용한다.!!!!!!!!!!!!!!!!!

add("Center",Area);

setVisible(true);


NFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Area.setText("");
}
});
NEdit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fd = new FileDialog(NotePad2Frame.this, "OPEN",FileDialog.LOAD); //LOAD대신 SAVE를 쓰면 저장하기다이얼로그
fd.setSize(300,200); //윈2000에서 다이얼로그 창이 빨리 사라지면 제목을 영어로 써주면됨.
fd.setVisible(true);
fd.getFile(); //파일명 알아내기
fd.getDirectory();//파일 경로알아내기

try{
FileReader fr = new FileReader(fd.getDirectory()+fd.getFile());
BufferedReader br = new BufferedReader(fr);

Area.setText("");
for(String str; (str = br.readLine()) != null;)
{
Area.setText(str + "\n");
}

br.close();
String fileName1 = fd.getFile(); //제목줄에 파일이름 붙이기;
setTitle(fileName1);

}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
});

/* NTool.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if()

}
});*/

NSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fd = new FileDialog(NotePad2Frame.this, "OPEN",FileDialog.LOAD); //LOAD대신 SAVE를 쓰면 저장하기다이얼로그
fd.setSize(300,200); //윈2000에서 다이얼로그 창이 빨리 사라지면 제목을 영어로 써주면됨.
fd.setVisible(true);
fd.getFile(); //파일명 알아내기
fd.getDirectory();//파일 경로알아내기

try{
FileWriter fw = new FileWriter(fd.getDirectory()+fd.getFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(Area.getText());
String filename1 = fd.getFile();
setTitle(filename1);

}catch(Exception ex)
{JOptionPane.showMessageDialog(null, ex.getMessage());}
}
});
NPrint.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PrintJob pj;
//pj = Toolkit.getDefaultToolkit().getPrintJob(this, "hi", null);
}
});



NPrint.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Area.print();
}
catch (Exception ex){}

}
});


NExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFrame fr = new JFrame();

JLabel lb = new JLabel("종료하시겠습니까");
//
JPanel pn = new JPanel();
JButton button1 = new JButton("YES?");
button1.addActionListener(listener);
JButton button2 = new JButton("NO?");
button2.addActionListener(listener);
pn.add(button1);
pn.add(button2);

//

fr.add(lb,BorderLayout.NORTH);
fr.add(pn,BorderLayout.CENTER);
fr.setSize(200,100);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
});
//여기까지 파일메뉴

NFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(400,200);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel pn = new JPanel();

JLabel lb = new JLabel("찾을내용");
final JTextField jd = new JTextField(15);
JButton button1 = new JButton("다음찾기");

button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//String fieldString = jd.getText();
//String textstring = Area.getText();

//if(textstring.indexOf(fieldString)!=-1)
//{

//}
if(Area.getText().indexOf(jd.getText())!=-1) {
Area.select(Area.getText().indexOf(jd.getText()),Area.getText().indexOf(jd.getText())+jd.getText().length());
}
//select()메서드는 해당블락을 표시해준다!!!

java.awt.Container com = null;
com = ((javax.swing.JButton)e.getSource()).getParent();
//component의 getParent메서드를 이용해서 JFrame(찾기프레임)을 찾습니다.
while(!com.getClass().getSimpleName().equals("JFrame")){
com = com.getParent();
}
javax.swing.JFrame comFrame = (javax.swing.JFrame)com;
//해당 프레임을 닫아줍니다.
comFrame.setVisible(false);
comFrame.dispose();


}
});




pn.add(lb);
pn.add(jd);
pn.add(button1);
add(pn,BorderLayout.NORTH);

//1행

JRadioButton jb = new JRadioButton("위로");
JRadioButton jb2 = new JRadioButton("아래로");

JButton button2 = new JButton("취소");


f.add(pn);
}
});



NChange.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

JFrame f = new JFrame("바꾸기");
f.setVisible(true);
f.setSize(400,150);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel pn = new JPanel();
JPanel pn2 = new JPanel();
JButton bt = new JButton("바꾸기");

JLabel lb1 = new JLabel("찾을 내용");
JLabel lb2 = new JLabel("바꿀 내용");
final JTextField jd = new JTextField(15);
final JTextField jd2 = new JTextField(15);
pn.add(lb1);
pn.add(jd);
pn.add(bt);
f.add(pn,BorderLayout.NORTH);


pn2.add(lb2);
pn2.add(jd2);
f.add(pn2,BorderLayout.CENTER);

bt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s = jd.getText();
String s2 = jd2.getText();
//indexOf() 꼭 기억하자!!!! replaceAll()마찬가지!!!
if(Area.getText().indexOf(s)!=-1){
Area.setText(Area.getText().replaceAll(s, s2));
}


}
});
}
});

NSelectAll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Area.selectAll();
}
});


NClock.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

Calendar cal = Calendar.getInstance();
//현재시각을 사용하기 위하여 getInstance()메서드 사용!!!!
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day= cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR);
int minutes = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);


Area.setText(Area.getText()+year+"년"+month+"월"+day+"일"+hour+"시"
+minutes+"분"+second+"초");

}
});

//여기까지 EDIT메뉴~~

LineChange.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int j=JOptionPane.showConfirmDialog(null,"자동줄바꿈을 설정하시겠습니까?"
,"자동줄 바꿈",JOptionPane.YES_NO_OPTION);

if(j!=0)
{
//
}
}

});

NFont.addActionListener(new ActionListener()
{
int fontstyle;
int size = 15;
String o;
String t;

JLabel lb;
public void actionPerformed(ActionEvent e)
{


JFrame frame = new JFrame("글 꼴");



/*JPanel pn1 = new JPanel();
JComboBox box1 = new JComboBox();

box1.addItem("궁서체");
box1.addItem("굴림체");
box1.addItem("고딕체");

*/

JPanel pn2= new JPanel();
///////////////////수정1/////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
//final를 추가하세요. 님이 만든 소스를 사용하다보니...
final JComboBox box2 = new JComboBox();

box2.addItem("bold");
box2.addItem("italic");
box2.addItem("type_1");
box2.addItem("truetype");

JPanel pn3 = new JPanel();
final JTextField box3 = new JTextField(10); //JTextField를 final로 선언한다!!

//box1.setEditable(true);
box2.setEditable(true);
box3.setEditable(true);

//pn1.add(box1);
pn2.add(box2);
pn3.add(box3);




//pn1.setBorder(new TitledBorder(new EtchedBorder(Color.BLUE,Color.DARK_GRAY),"글 꼴"));
pn2.setBorder(new TitledBorder(new EtchedBorder(Color.BLUE,Color.DARK_GRAY),"글꼴 스타일"));
pn3.setBorder(new TitledBorder(new EtchedBorder(Color.BLUE,Color.DARK_GRAY),"크 기"));

frame.setLayout(new GridLayout(2,1));
//frame.add(pn1);
frame.add(pn2);
frame.add(pn3);

JPanel pn4 = new JPanel();
lb = new JLabel("ABCDEF");

pn4.add(lb);
pn4.setBorder(new TitledBorder(new EtchedBorder(Color.BLUE,Color.DARK_GRAY),"미리보기"));
frame.add(pn4);

frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//////////////////////////////////////////// 요 부 분 !!!!!!!!!!! ///////////////////////////////
box2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

/*
이부분 삭제하세요
o = e.getActionCommand();

if (o=="bold") {fontstyle = font.BOLD;}
else if(o=="italic"){fontstyle = font.ITALIC;}
else if(o=="type_1"){fontstyle = font.TYPE1_FONT;}
else if(o=="truetype"){fontstyle = font.TRUETYPE_FONT;}
*/
///////////////////////수정2/////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
// 이부분 추가하세요
if(e.getSource() == box2){
JComboBox cb = (JComboBox)e.getSource();
// cb.getSelectedIndex() 이것이 0,1,2.. 값을 리턴합니다.
// 의문점 쪽지주세요
switch (cb.getSelectedIndex()) {
case 0:
fontstyle = font.BOLD;
break;
case 1:
fontstyle = font.ITALIC;
break;
case 2:
fontstyle = font.TYPE1_FONT;
break;
case 3:
fontstyle = font.TRUETYPE_FONT;

}
}
setSampleFont();


}
});


box3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t = box3.getText();
size = Integer.parseInt(t);
setSampleFont();
}
});


}
public void setSampleFont()
{
lb.setFont(new Font(null,fontstyle,size));
}


});


//여기까지 Formula(서식)메뉴~~

Def.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "2008/10/x 드디어 완성 ㅋㅋㅋ","메모장",JOptionPane.INFORMATION_MESSAGE );
}
});

}

public static void main(String args[])
{

JFrame frame = new NotePad2Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("메모장");
frame.setVisible(true);
frame.setSize(700,500);
}
}
/*문]
* ////////////////////////////// 요 부분 ///////////////
* 이라고 표시된 부분이 글꼴의 폰트를 변경시키는 부분인데요.
* 폰트가 딱 한번만 변경이 되고 다시 변경을 시킬때는 적용이 안되네요 ㅠㅠ
* 예를 들면 폰트를 ITALIC으로 변경을 시키면 변경이 됩니다.
* 하지만 ITALIC을 다시 BOLD로 변경을 시킬려면 적용이 안되네요.
* 왜 그런지 계속 고민을 하는데 잘안되네요.
* 고수님들의 도움좀 부탁드려여~~
*/

No comments:

Post a Comment