JAVA FIle 실습 #1

Programming/JAVA,JSP 2014. 1. 17. 17:36 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

package com.tistory.tansanc;


import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;


import javax.swing.JButton;

import javax.swing.JColorChooser;

import javax.swing.JFrame;

import javax.swing.JPanel;


class MyFrame extends JFrame {

private JButton button1;

private JButton button2;

private JPanel panel;


public MyFrame() {

this.setSize(300, 200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setTitle("이벤트 예제");

panel = new JPanel();

button1 = new JButton("색상선택");

button1.addActionListener(new MyListener());

panel.add(button1);

this.add(panel);

LoadFile();

this.setVisible(true);

}


private void LoadFile() {

// TODO Auto-generated method stub

ObjectInputStream oos = null;

try {

oos = new ObjectInputStream(

new FileInputStream("Color.bin"));

Color newColor = (Color)oos.readObject();

panel.setBackground(newColor);

oos.close();

} catch ( IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


private class MyListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == button1) {

JColorChooser colorChooser = 

new JColorChooser();

Color newColor =

colorChooser.showDialog(MyFrame.this,

"Hello",Color.red);

System.out.println(newColor);

panel.setBackground(newColor);

ObjectOutputStream oos = null;

try {

oos = new ObjectOutputStream(

new FileOutputStream("Color.bin"));

oos.writeObject(newColor);

oos.close();

} catch ( IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

}


public class MyFrameTest2 {

public static void main(String[] args) {

MyFrame t = new MyFrame();

}

}



'Programming > JAVA,JSP' 카테고리의 다른 글

JAVA 채팅 Server 예제 #1  (0) 2014.01.20
JAVA 채팅 Client 예제 #1  (0) 2014.01.20
JAVA File 실습  (0) 2014.01.17
JAVA 공던지기 게임 완성  (0) 2014.01.16
JAVA 공던지기 게임  (0) 2014.01.16