날짜 시간 을 원하는 양식에 맞게 출력
HH: 24시간 MM:월 mm:분
a:오전/오후 hh:12시간
EEE:요일짧게 EEEE:요일길게
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd a hh:mm:ss");
SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd HH:mm EEEE");
SimpleDateFormat sdf4=new SimpleDateFormat("yyyy년 MM월 dd일 HH시 mm분");
숫자,돈 컴마 등등
int money=6785420;
double num=67.45123;
화폐단위와 컴마제공
NumberFormat nf1=NumberFormat.getCurrencyInstance();
컴마제공
NumberFormat nf2=NumberFormat.getInstance();
public class ExFormat_04 {
public static void main(String[] args) {
Date date=new Date();
System.out.println(date);
//날짜 시간 을 원하는 양식에 맞게 출력
//HH: 24시간 MM:월 mm:분
//년-월-일 시간:분:초
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf1.format(date));
//a:오전/오후 hh:12시간
//년-월-일 오전/오후 시간:분:초
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd a hh:mm:ss");
System.out.println(sdf2.format(date));
//EEE:요일짧게 EEEE:요일길게
//년-월-일 시간:분 요일길게
SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd HH:mm EEEE");
System.out.println(sdf3.format(date));
SimpleDateFormat sdf4=new SimpleDateFormat("yyyy년 MM월 dd일 HH시 mm분");
System.out.println(sdf4.format(date));
//숫자,돈 컴마 등등
int money=6785420;
double num=67.45123;
NumberFormat nf1=NumberFormat.getCurrencyInstance();//화폐단위와 컴마제공
System.out.println(nf1.format(money));
NumberFormat nf2=NumberFormat.getInstance();//컴마제공
System.out.println(nf2.format(num));
System.out.println(nf2.format(money));
}
}
클래스 전역변수
클래스는 자동 초기값 지정이기에 null 안줘도 됨
FileWriter fw;
메서드 안에서 초기값 지정해야하기 때문에 null 선언
FileWriter fw=null;
String fileName="파일경로/생성할 파일 이름.확장자";
파일에 내용 추가
메모장줄넘김
fw.write("Have a Nice Day!!!!\n");
날짜를 String 형식으로 넘김
fw.write(new Date().toString());
추가모드
1번 더 실행하면 똑같은 글귀가 또 생성됨
fw=new FileWriter(fileName, true);
public class FileWriter_05 {
//FileWriter fw;//클래스는 자동 초기값 지정이기에 null 안줘도 됨
public static void filewrite()
{
FileWriter fw=null;//메서드 안에서 초기값 지정해야하기 때문에 null 선언
//경로/저장할파일이름.확장자
String fileName="/Users/sunghyunchoi/Desktop/sist0616/file/filetest1.txt";
try {
fw=new FileWriter(fileName); //파일 새로 생성(같은 이름이 있어도 새로 생성)
//파일에 내용 추가
fw.write("Have a Nice Day!!!!\n"); //메모장줄넘김
fw.write(new Date().toString()); //날짜를 String 형식으로 넘김
System.out.println("***파일 저장 성공!!***");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void filewrite2()
{
FileWriter fw=null;
String fileName="/Users/sunghyunchoi/Desktop/sist0616/file/fileTest2.txt";
try {
fw=new FileWriter(fileName, true);//추가모드 //1번 더 실행하면 같은 글귀가 또 생성됨
fw.write("내 이름은 홍길동\n");
fw.write("=========================\n");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
filewrite();
filewrite2();
}
}
이미지 추가
ImageIcon icon1=new ImageIcon("파일경로");
버튼생성
버튼 2행 2열 배치
this.setLayout(new GridLayout(2, 2));
버튼1에 글귀와 아이콘이미지추가
텍스트 세로위치 가운데
btn1.setVerticalTextPosition(JButton.BOTTOM);
텍스트 가로위치 아래
btn1.setHorizontalTextPosition(JButton.CENTER);
버튼추가
this.add(btn1);
마우스를 위에 올렸을때 아이콘
btn1.setRolloverIcon(icon2);
클릭할때 아이콘
btn1.setPressedIcon(icon3);
public class SwingGrid_06 extends JFrame implements ActionListener{
Container cp;
ImageIcon icon1=new ImageIcon("/Users/sunghyunchoi/Desktop/sist0616/Swingimage/swingimage/img2.gif");
ImageIcon icon2=new ImageIcon("/Users/sunghyunchoi/Desktop/sist0616/Swingimage/logoImg/images02.jpg");
ImageIcon icon3=new ImageIcon("/Users/sunghyunchoi/Desktop/sist0616/Swingimage/logoImg/ironman.png");
ImageIcon icon4=new ImageIcon("/Users/sunghyunchoi/Desktop/sist0616/Swingimage/logoImg/images03.jpg");
//버튼선언
JButton btn1,btn2,btn3,btn4;
public SwingGrid_06(String title) {
super();
cp=this.getContentPane();
this.setBounds(200, 100, 300, 400);
cp.setBackground(new Color(255,255,100));
setDesign();
setVisible(true);
}
public void setDesign()
{
//버튼4개 생성
this.setLayout(new GridLayout(2, 2));//버튼 2행 2열 배치
btn1=new JButton("hello", icon1);
btn1.setVerticalTextPosition(JButton.BOTTOM); //텍스트 세로위치 가운데
btn1.setHorizontalTextPosition(JButton.CENTER); //텍스트 가로위치 아래
this.add(btn1);
btn1.setRolloverIcon(icon2);//마우스를 위에 올렸을때 아이콘
btn1.setPressedIcon(icon3);//클릭할때 아이콘
//2
btn2=new JButton(icon4);
this.add(btn2);
//3
btn3=new JButton("JAVA");
this.add(btn3);
//4
btn4=new JButton("Hi!!", icon2);
this.add(btn4);
//이벤트객체
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
}
public static void main(String[] args) {
new SwingGrid_06("그리드 레이아웃");
}
@Override
public void actionPerformed(ActionEvent e) {
Object ob=e.getSource();
//선택하기 위한 변수를 줌
if(ob==btn1)
JOptionPane.showMessageDialog(this, "1번 이미지 입니다");
else if(ob==btn2)
JOptionPane.showMessageDialog(this, "2번 이미지 입니다");
else if(ob==btn3)
JOptionPane.showMessageDialog(this, "3번 JAVA글씨 입니다");
else if(ob==btn4)
JOptionPane.showMessageDialog(this, "4번 이미지 입니다");
}
}