高分求助 关于JAVA 文件读取

问题如下:1、我想加一个窗口关闭,但是没实现,不知道错哪了2、如何让下拉式列表默认为“5”3、添加一个文件读取,每点击“计算”按钮运算一次,就计数器(从sum.txt中取... 问题如下:
1、我想加一个窗口关闭,但是没实现,不知道错哪了
2、如何让下拉式列表默认为“5”
3、添加一个文件读取,每点击“计算”按钮运算一次,就计数器(从sum.txt中取)加一,计数器结果存在一个文本文件(sum.txt)中,可供下一次程序运行时用,如何实现。

初学,还不太明白,跪请各位大侠了,拜谢

import java.awt.*;
import java.awt.event.*;

public class WindowCal extends Frame implements ItemListener,ActionListener{
Frame f=new Frame("计算器");
Choice num1,opera,num2;
Button cal;
TextArea result;
WindowCal(){
f.setLayout(new FlowLayout());
num1=new Choice();
opera=new Choice();
num2=new Choice();
cal=new Button("计算");
result=new TextArea(3,20);
num1.add("1");
num1.add("2");
num1.add("3");
num1.add("4");
num1.add("5");
num1.add("6");
num1.add("7");
num1.add("8");
num1.add("9");
num1.add("10");
num2.add("1");
num2.add("2");
num2.add("3");
num2.add("4");
num2.add("5");
num2.add("6");
num2.add("7");
num2.add("8");
num2.add("9");
num2.add("10");
opera.add("+");
opera.add("-");
opera.add("*");
opera.add("/");
cal.addActionListener(this);
num1.addItemListener(this);
opera.addItemListener(this);
num2.addItemListener(this);

f.add(num1);
f.add(opera);
f.add(num2);
f.add(cal);
f.add(result);
f.setSize(200,150);
f.setVisible(true);
f.validate();
}

public void itemStateChanged(ItemEvent e){

}

public void actionPerformed(ActionEvent e) {
String name=opera.getSelectedItem();
int index=opera.getSelectedIndex();
float a=num1.getSelectedIndex();
float b=num2.getSelectedIndex();

float re=0;
if(e.getSource()==cal){
switch(index){
case 0:
re=a+b+2;
break;
case 1:
re=(a+1)-(b+1);
break;
case 2:
re=(a+1)*(b+1);
break;
case 3:
re=(a+1)/(b+1);
break;
default:
re=-100;
}
result.setText("\n"+((int)(a+1))+name+((int)(b+1))+"="+re);
}
}

}

class Test{
public static void main(String args[])
{
new WindowCal();

}
}
展开
 我来答
匿名用户
推荐于2016-10-08
展开全部
看看下面代码是不是解决你的问题。第三问我理解是对运算次数的计数。还有什么不明白的可追问
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

public class WindowCal extends Frame implements ItemListener,ActionListener{
Frame f=new Frame("计算器");
Choice num1,opera,num2;
Button cal;
TextArea result;
WindowCal(){
f.setLayout(new FlowLayout());
num1=new Choice();
opera=new Choice();
num2=new Choice();
cal=new Button("计算");
result=new TextArea(3,20);
num1.add("1");
num1.add("2");
num1.add("3");
num1.add("4");
num1.add("5");
num1.add("6");
num1.add("7");
num1.add("8");
num1.add("9");
num1.add("10");
num2.add("1");
num2.add("2");
num2.add("3");
num2.add("4");
num2.add("5");
num2.add("6");
num2.add("7");
num2.add("8");
num2.add("9");
num2.add("10");
num1.select(4);//选中num1中索引为4的数即为5
opera.add("+");
opera.add("-");
opera.add("*");
opera.add("/");
cal.addActionListener(this);
num1.addItemListener(this);
opera.addItemListener(this);
num2.addItemListener(this);

f.add(num1);
f.add(opera);
f.add(num2);
f.add(cal);
f.add(result);
f.setSize(200,150);
f.setVisible(true);
f.validate();
f.addWindowListener(new WindowListener() {

public void windowOpened(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowDeactivated(WindowEvent e){}

public void windowClosing(WindowEvent e) {
System.exit(0);//退出系统
}

public void windowClosed(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}
});

}

public void itemStateChanged(ItemEvent e){

}

public void actionPerformed(ActionEvent e) {
String name=opera.getSelectedItem();
int index=opera.getSelectedIndex();
float a=num1.getSelectedIndex();
float b=num2.getSelectedIndex();

float re=0;
if(e.getSource()==cal){
switch(index){
case 0:
re=a+b+2;
break;
case 1:
re=(a+1)-(b+1);
break;
case 2:
re=(a+1)*(b+1);
break;
case 3:
re=(a+1)/(b+1);
break;
default:
re=-100;
}
result.setText("\n"+((int)(a+1))+name+((int)(b+1))+"="+re);
count();//计数函数,使用计算器的次数
}

}
public void count(){
File file=new File("sum.txt");//sum.txt存放在你当前的工作目录下。
BufferedWriter bw;
BufferedReader br;
// DataInputStream dis;
// DataOutputStream dos;
try {
if(!file.exists()||file.length()==0){//如果sum不存在或者为空,创建sum.txt并写入1
file.createNewFile();
String s="1";
System.out.println("这是第"+s+"次使用该计算器");
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(s);
bw.close();
}
else {
br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String s=(Integer.parseInt(br.readLine())+1)+"";
System.out.println("这是第"+s+"次使用该计算器");
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(s);
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
new WindowCal();
}

}
百度网友845f74e61
2012-05-13 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1571万
展开全部
1.关闭窗体的代码如下.
f.addWindowListener(new WindowListener() {

public void windowOpened(WindowEvent e) {

}

public void windowIconified(WindowEvent e) {

}

public void windowDeiconified(WindowEvent e) {

}

public void windowDeactivated(WindowEvent e) {

}

public void windowClosing(WindowEvent e) {
System.exit(0);
}

public void windowClosed(WindowEvent e) {

}

public void windowActivated(WindowEvent e) {

}
});

-------------------------------
2. num1.select("5"); 这个代码是让5这一项选中.
3、添加一个文件读取,每点击“计算”按钮运算一次,就计数器(从sum.txt中取)加一,计数器结果存在一个文本文件(sum.txt)中,可供下一次程序运行时用,如何实现。

这个没看明白?你是把sum.txt的结果拿出来和谁相加?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式