
JavaApplet应用程序在你编译成功后会生成一个对应的.class文件,在HTML中可以用
<html>
....
<body>
<code ="xxx.class" width=200 height=300>
</body>
...
中调用
import java.applet.Appletimport java.awt.Button
import java.awt.FlowLayout
import java.awt.Label
import java.awt.TextField
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.Arrays
public class AppletSort extends Applet implements ActionListener {
TextField numbers = null
static File file = new File("data.txt")//记录结果集的文件
public void init(){
Label msg = new Label("Please input some digit numbers, separate with SPACE:")//提示用户输入,数字之间用空格隔开
numbers = new TextField(17)//文本框
super.setLayout(new FlowLayout())
super.add(msg)
super.add(numbers)
Button sortAsc = new Button("Sort ASC")//升序排列按钮
Button sortDesc = new Button("Sort DESC")//降序排列按钮
super.add(sortAsc)
super.add(sortDesc)
super.repaint()
sortAsc.addActionListener(this)
sortAsc.addActionListener(this)
}
public void actionPerformed(ActionEvent arg0) {
String text = numbers.getText()
String[] digits = text.split("\\s+")
int[] nums = new int[digits.length]//The array to store numbers
for(int i = 0, len = digits.lengthi <leni++){//将数字转换成成数字数组
nums[i] = Integer.parseInt(digits[i])
}
Arrays.sort(nums)//将输入的数字升序排序
try {
wirteSortedAryToFile(nums)//写入文件
} catch (IOException e) {
e.printStackTrace()
}
}
private void wirteSortedAryToFile(int[] nums) throws IOException {
FileOutputStream fo = new FileOutputStream(file)
fo.flush()
fo.write(intAryToString(nums, true).getBytes())//按照升序写入文件
fo.write(intAryToString(nums, false).getBytes())//按照降序写入文件
fo.close()
}
public String intAryToString(int[] ary, boolean isAsc){//将数组按照升序或者降序转换成string形式准备写入文件
StringBuffer sb = new StringBuffer(isAsc? "ASC: " : "DESC")
int length = ary.length
if(isAsc){
for(int digit: ary){
sb.append(digit + " ")
}
}else{
for(int i = lengthi >0i--){
sb.append(ary[i-1] + " ")
}
}
return sb.toString() + "\n"
}
}
----JDK 1.5通过
ASC: 1 2 3 4 56
DESC: 56 4 3 2 1
你是在IE里面运行的?如果是在IE里面运行这个很正常。基于安全原因,APPLET在IE netscape里面是没有权限写文件的。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)