파일 open이 안되고있습니다.그리고 버튼하나누르면 다 날라가네요.
말글
open은 에매하게 되나 오류가 많이 뜨고있습니다. 물론 저장은 됩니다. 그리고
지우기 버튼을 누르면 기존에 있는 성적표.txt에 있는 내용이 함께 다 날라갑니다. 그렇게는 원하지 않습니다.
성적표.txt가 지우지 말아야 있어야하고, 지우기 버튼누르면 프로그램내에 있는 입력 정보들이 다 날라가야합니다.
그리고 help메뉴에 개발자 정보를 창을 하나더 띄울려고했는데 help.액션 리스너(this);라고하면 에러 난다고
이벤트를 못하게끔 하네요.
제코딩을 확인해주세요.
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.Scrollbar;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
import javax.swing.JDialog;
import javax.swing.JFrame;
class myFrame extends Frame implements ActionListener{
Button btn1,btn2,lnp,output,clear;
TextField field1, field2, field3, field4, field5,field6;
TextArea text;
Checkbox check1,check2,check3,check4,check5,cbxGrp1,cbxGrp2,cbxGrp3;
MenuBar bar;
Menu file,edit,view,helpMenu;
MenuItem open,save,exit;
MenuItem cut,paste;
MenuItem help;
CheckboxGroup cbxGrp;
Student[] student = new Student[10];
String name;
String num;
int lang;
int math;
int engl;
int sci;
private ActionListener a;
static int count=0;
public myFrame(String title) throws IOException{
super(title);
bar = new MenuBar();
file = new Menu(File);
helpMenu = new Menu(Help);
open=new MenuItem(open);
save = new MenuItem(save);
exit = new MenuItem(exit);
setLayout(null);
addWindowListener(new WinHandle());
Panel pan1 = new Panel();
pan1.setSize(250, 350);
pan1.setBackground(Color.pink);
pan1.setLocation(30, 30);
pan1.setLayout(null);
Scrollbar scr = new Scrollbar(Scrollbar.VERTICAL);
scr.setLocation(230, 0);
scr.setSize(20, 350);
pan1.add(scr);
Panel pan2 = new Panel();
pan2.setSize(270, 60);
pan2.setBackground(Color.red);
pan2.setLocation(30, 480);
pan2.setLayout(null);
Panel pan3 = new Panel();
pan3.setSize(250, 60);
pan3.setBackground(Color.yellow);
pan3.setLocation(30, 410);pan3.setLayout(null);
//라벨,텍스트필드,체크박스등 각각의 객체 생성
Label lB1 = new Label(이름 :);
Label lB2 = new Label(학번 :);
Label lB3 = new Label(국어 :);
Label lB4 = new Label(영어 :);
Label lB5 = new Label(수학 :);
Label IB9 = new Label(과학:);
Label lB6 = new Label(좋아하는 과목);
Label lB7 = new Label(좋아하는 음악);
Label lB8 = new Label(출 력);
field1 = new TextField();
field2 = new TextField();
field3 = new TextField();
field4 = new TextField();
field5 = new TextField();
field6 = new TextField();
text = new TextArea();
check1 = new Checkbox(리눅스);
check2 = new Checkbox(자바);
check3 = new Checkbox(Network);
check4 = new Checkbox(C언어);
check5 = new Checkbox(DB);
CheckboxGroup cbxGrp = new CheckboxGroup();
cbxGrp1 = new Checkbox(하드락, cbxGrp, true);
cbxGrp2 = new Checkbox(재즈, cbxGrp, false);
cbxGrp3 = new Checkbox(클래식, cbxGrp, false);
btn1 = new Button(계산);
btn2 = new Button(종료);
lnp = new Button(입력 );
output = new Button(출력);
clear= new Button(지우기);
btn1.addActionListener(this);
btn2.addActionListener(this);
lnp.addActionListener(this);
output.addActionListener(this);
clear.addActionListener(this);
btn2.setActionCommand(exit);
open.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);lB1.setBounds(15,30,30,30);
lB2.setBounds(15,80,30,30);lB3.setBounds(15,130,30,30);
lB4.setBounds(15,180,30,30);
lB5.setBounds(15,230,30,30);
lB6.setBounds(50,5,100,30);
IB9.setBounds(15,280,30,30);
lB7.setBounds(50,5,100,30);
check1.setBounds(10, 20, 50, 50);
check2.setBounds(60, 20, 50, 50);
check3.setBounds(110, 20, 50, 50);
check4.setBounds(170,20,50,50);
check5.setBounds(230,20,50,50);
cbxGrp1.setBounds(20, 20, 56, 50);
cbxGrp2.setBounds(110, 20, 50, 50);
cbxGrp3.setBounds(190, 20, 50, 50);
lB8.setBounds(300,50,50,50);
//패널1
field1.setBounds(50, 40, 150, 30);
field2.setBounds(50, 90, 150, 30);
field3.setBounds(50, 140, 150, 30);
field4.setBounds(50, 190, 150, 30);
field5.setBounds(50, 240, 150, 30);
field6.setBounds(50,290,150,30);
//각각의 패널 및 윈도우에 추가
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
helpMenu.add(help=new MenuItem(만든사람));
bar.add(file);
bar.add(helpMenu);
this.setMenuBar(bar);
this.setSize(400,200);
pan1.add(lB1);
pan1.add(lB2);
pan1.add(lB3);
pan1.add(lB4);
pan1.add(IB9);
pan1.add(lB5);
pan2.add(lB6);
pan3.add(lB7);
add(lB8);
pan1.add(field1);
pan1.add(field2);
pan1.add(field3);
pan1.add(field4);
pan1.add(field5);
pan1.add(field6);
add(text);
pan2.add(check1);
pan2.add(check2);
pan2.add(check3);
pan2.add(check4);
pan2.add(check5);
pan3.add(cbxGrp1);
pan3.add(cbxGrp2);pan3.add(cbxGrp3);
add(btn1);
add(btn2);
add(lnp);
add(output);
add(clear);
add(pan1);
add(pan2);
add(pan3);
text.setBounds(350, 60, 250, 230);
btn1.setBounds(350, 450, 100, 50);
btn2.setBounds(500, 450, 100, 50);
lnp.setBounds(500,500,100,50);
output.setBounds(350,500,100,50);
clear.setBounds(370, 400, 60, 50);
this.setSize(650, 600);
this.setBackground(getBackground().CYAN);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String rock=,jazz=,classic=;
String linux=,java=,net=,c= ,db=;
FileDialog fileopen;
FileWriter fw = null;
try {
fw = new FileWriter(성적표.txt);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(fw);
if(bsp; if(e.getSource()==open){
FileDialog fd1 = new FileDialog(this,열기,FileDialog.LOAD);
fd1.show();
String fileName = fd1.getFile()+fd1.getDirectory();
try {
fileload(fileName); ---------------------------이부분에서 에러가 나온다고 합니다.
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileReader fr = null;
try {
fr = new FileReader(fd1.getFile());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader br= new BufferedReader(fr);
try {
String line = br.readLine();
while(line!=null){
StringTokenizer st= new StringTokenizer(line);
while(st.hasMoreTokens()){
text.append(st.nextToken());
}
line=br.readLine();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource()==save){
FileDialog fd2=new FileDialog(this,저장,FileDialog.SAVE);
fd2.show();
String name=fd2.getDirectory()+fd2.getFile();
try {
filesave(name);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getSource()==exit){
System.exit(0);
this.setVisible(false);
} if(e.getSource()==help){
JFrame J = new JFrame(개발자);
JDialog J1 = new JDialog(J,개발자);
J.setSize(100,100);
J.setVisible(true);
J1.setSize(50,50);
J1.setVisible(true);
}
if(e.getSource()==lnp){
if(count100){
student[count]=new Student();
name=(String)field1.getText();
num=(String)field2.getText();
lang=Integer.parseInt(field3.getText());
math=Integer.parseInt(field4.getText());
engl=Integer.parseInt(field5.getText());
sci=Integer.parseInt(field6.getText());
student[count].setStudent(name, num, lang, math, engl,sci);
count++;
field1.setText();
field2.setText();
field3.setText();
field4.setText();
field5.setText();
field6.setText();
}
else {
System.out.println(더이상 입력할 수 없습니다.);
.);
}
if(lang100 || lang0){
text.append(국어 점수를 다시 입력해주십시오.);
text.append(\n);
System.out.println(다시입력해주세요);
}
if(engl100 || engl0){
text.append(\n);
text.append(엉어 점수를 다시 입력해주십시오.);
System.out.println(다시입력해주세요);
}
if(math100 || math0){
text.append(\n);
text.append(수학 점수를 다시 입력해주십시오.);
System.out.println(다시입력해주세요);
}
}
if(sci100 || sci0){
text.append(\n);
text.append(과학점수를 다시입력해주세요.);
System.out.println(다시입력해주세요);
}
else if(e.getSource()==clear){ text.setText(); ----------------------------지우기버튼 하나때문에 기존성적표.txt도 이거때문에 다날라감.
}
else if(e.getSource()==btn1){
if(lang100 || engl100||math100 ||sci100 || lang0||engl0||math0||sci0)
{ System.out.println(잘못입력햇습니다.);
}
else if(lang=100||engl=100||math=100||sci=100){
for(int i=0;icount; i++){
student[i].cal_sum_ave(student[i]);
System.out.println(총점과 평균계산이 완료되었습니다.);
}
}
}
else if(e.getSource()==output){
if(lang100 || engl100||math100||sci100 || lang0||engl0||math0||sci0)
{ System.out.println(잘못입력햇습니다.);
} else if(lang=100||engl=100||math=100||sci=100){
if(count=1){
for(int i=0;icount ; i++){
text.append(student[i].toString());
}
}if(count=2){
for(int i=1;icount;i++){
text.append(student[i].toString());
p;
}
}if(count=3){
for(int i=2;icount;i++){
text.append(student[i].toString());
}
}if(count=4){
for(int i=3;icount;i++){
text.append(student[i].toString());
}
}if(count=5){
for(int i=4;icount;i++){
text.append(student[i].toString());
}
}if(count=6){
for(int i=5;icount;i++){
text.append(student[i].toString());
}
}if(count=7){
for(int i=6;icount;i++){
text.append(student[i].toString());
}
}if(count=8){
for(int i=7;icount;i++){
text.append(student[i].toString());
}
}if(count=9){
for(int i=8;icount;i++){
text.append(student[i].toString());
}
}if(count=10){
for(int i=9;icount;i++){
text.append(student[i].toString());
}
}
String d=text.getText();
try {
bw.write(d+\n);
bw.flush();
bw.newLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(check1.getState()==true){
linux=check1.getLabel();
}if(check2.getState()==true){
java=check2.getLabel();
}if(check3.getState()==true){
net=check3.getLabel();
}if(check4.getState()==true){
c=check4.getLabel();
}if(check5.getState()==true){
db=check5.getLabel();
}if(cbxGrp1.getState()==true){
rock=cbxGrp1.getLabel();
}if(cbxGrp2.getState()==true){
jazz=cbxGrp2.getLabel();
}if(cbxGrp3.getState()==true){
classic=cbxGrp3.getLabel();
}
String output1= 좋아하는 음악은 +rock+jazz+classic+이고 \n+좋아하는 과목은+linux+java+net+c+db+입니다.입니다.\n;
text.append(output1);
try {
bw.write(output1+\n);
bw.flush();
bw.newLine();
bw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else if(e.getSource()==btn2){
String button_command;
button_command=e.getActionCommand();
System.out.println(button_command);
setVisible(false);
dispose();
System.exit(0);
}
}
private void filesave(String name) throws IOException {
FileOutputStream fis=null;
File f=new File(name);
String ts=new String(text.getText());
char b[]=new char[ts.length()];
ts.getChars(0, ts.length(), b, 0);
fis=new FileOutputStream(f);
OutputStreamWriter ir =new OutputStreamWriter(fis);
ir.write(b,0,ts.length());
ir.close();
fis.close();
// TODO Auto-generated method stub
}
private void fileload(String fileName) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis =null;
File f=new File(fileName);
char b[]=new char[(int)f.length()];
fis = new FileInputStream(f); -----------------------------------여기서 에러가 난다고 발견.
InputStreamReader ir = new InputStreamReader(fis);
ir.read(b,0,(int)f.length());
String ts=new String(b);
text.append(ts);
ir.close();
fis.close();
}
public class WinHandle extends WindowAdapter{
public void windowClosing(WindowEvent e){
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
System.exit(0);
}
}
}/*
파일명 : Student.java
기능 : Student클래스 정의한다.
*/
public class Student {
String StName;
String StNr;
int language;
int math;
int english;
int sci;
int sum;
double ave;
public Student()
{ setStudent(NULL,NULL,0,0,0,0); }
public Student(String Name, String Num, int l, int m, int e,int s)
{ setStudent(Name, Num, l, m, e,s); }
public void setStudent(String sa, String sb, int a, int b, int c,int d)
{ StName = new String(sa);
StNr = new String(sb);
language = a;
math=b;
english=c;
sci=d;
}
public Student cal_sum_ave(Student a)
{
a.sum = ((a.language)+(a.math)+(a.english)+(a.sci));
a.ave = (a.sum/4);
return a;
}
public String toString()
{
return (이름은 + StName + 이고\n + 학번은 + StNr + 이며\n 총점은 + sum + 평균은 + ave+입니다.\n );
}
}
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
myFrame my = new myFrame(성적관리 프로그램);
}
}