十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
几年没有碰swing了,给你详细注释了。仔细看。希望对你有所帮助。
我们提供的服务有:网站制作、成都网站制作、微信公众号开发、网站优化、网站认证、凌海ssl等。为上1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的凌海网站制作公司
import java.awt.*;//java抽象窗口工具包
import java.awt.event.*;//java抽象窗口工具包组件所激发的各类事件的接口和类
public class Test5{//类名
Frame f;//定义一个Frame窗体
TextArea center;//文本域
Label la1,la2,la3;//三个标签
Panel east,south,north;//三块面板
Button b1,b2,b3,b4;//四个按钮
Choice l1;//下拉单选框
TextField t1;//文本域
// textfield只有一行可写
// textarea是一个区域,可以有很多行
public static void main(String[] args){//主函数,程序入口
Test mb = new Test();
mb.go();//调用go方法,初始化界面
}
private void go(){
f = new Frame("留言版程序");//标题
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
f.setVisible(false);
System.exit(0);
//System.exit(0) 0--正常结束程序 1--异常关闭程序
}
});
f.setBounds(0,0,600,400);//布局大小
f.setLayout(new BorderLayout());//显示方式
f.setResizable(false);
//下面都将控件按钮初始化,你懂得
north = new Panel();
south = new Panel();
east = new Panel();
center = new TextArea("留言内容:");
center.setEditable(false);
b1 = new Button("清屏");
b2 = new Button("至顶");
b3 = new Button("至尾");
la1 = new Label("留言版");
la2 = new Label("你");
la3 = new Label(" 地说:");
t1 = new TextField(20);
b4 = new Button("提交");
l1 =new Choice();
l1.add("微笑 ");
l1.add("生气 ");
l1.add("伤心 ");
f.add(BorderLayout.NORTH,north);//布局
f.add(BorderLayout.SOUTH,south);//布局
f.add(BorderLayout.EAST,east);//布局
f.add(BorderLayout.CENTER,center);//布局
north.add(BorderLayout.CENTER,la1);
south.add(la2);//把东西加到面板上
south.add(l1);
south.add(la3);
south.add(t1);//把东西加到面板上
south.add(b4);
east.setLayout(new GridLayout(9,0,1,10));
east.add(b1);
east.add(b2);
east.add(b3);
f.setVisible(true);//可视化
b4.addActionListener(new ActionListener(){//点击提交按钮产生事件
public void actionPerformed(ActionEvent e){
String text1,text2;
text1 = l1.getSelectedItem();
text2 = t1.getText();
t1.setText(null);
if(t1.getText() != ""){//将下拉单选框的内容和你输入在文本框的内容显示在中间面板上
center.append("\n");
center.append(text1);
center.append(text2);
}
}
});
b1.addActionListener(new ActionListener(){//清屏按钮事件,只留下中间面板显示:“留言内容:”,其余清楚
public void actionPerformed(ActionEvent e){
center.setText("留言内容:");
}
});
b2.addActionListener(new ActionListener(){//至顶按钮事件,光标焦点至顶
public void actionPerformed(ActionEvent e){
center.requestFocusInWindow();
center.setCaretPosition(8);
}
});
b3.addActionListener(new ActionListener(){//至尾按钮事件,光标焦点至尾
public void actionPerformed(ActionEvent e){
center.requestFocusInWindow();
center.setCaretPosition(center.getText().length());
}
});
}
}
在 Java 中,可以使用下面的代码来计算 37 加 29 的值:
public class Main {
public static void main(String[] args) {
// 定义两个变量,表示要相加的数字
int x = 37;
int y = 29;
// 计算 x 加 y 的值
int sum = x + y;
// 输出结果
System.out.println(sum);
}
}
运行上面的代码,输出结果如下:
66
注意:在 Java 中,必须使用变量来存储数字,不能直接使用数字进行运算。
以下是上面代码中每一行的含义:
int x = 37;:定义一个名为 x 的变量,并将其初始值设为 37。
int y = 29;:定义一个名为 y 的变量,并将其初始值设为 29。
int sum = x + y;:使用加号(+)计算 x 和 y 的和,并将结果赋值给名为 sum 的变量。
System.out.println(sum);:使用 println() 方法输出 sum 变量的值。
注意:上面的代码假设你已经导入了 java.lang 包,否则无法使用 System 类和 out 对象。
希望我的回答可以帮到您,祝您身体健康,生活愉快!
Java文件操作大全
1.创建文件夹
//import java.io.*;
File myFolderPath = new File(%%1);
try {
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
2.创建文件
//import java.io.*;
File myFilePath = new File(%%1);
try {
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(%%2);
resultFile.close();
}
catch (Exception e) {
System.out.println("新建文件操作出错");
e.printStackTrace();
}
3.删除文件
//import java.io.*;
File myDelFile = new File(%%1);
try {
myDelFile.delete();
}
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
4.删除文件夹
//import java.io.*;
File delFolderPath = new File(%%1);
try {
delFolderPath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
5.删除一个文件下夹所有的文件夹
//import java.io.*;
File delfile=new File(%%1);
File[] files=delfile.listFiles();
for(int i=0;ifiles.length;i++){
if(files[i].isDirectory()){
files[i].delete();
}
}
6.清空文件夹
//import java.io.*;
File delfilefolder=new File(%%1);
try {
if (!delfilefolder.exists()) {
delfilefolder.delete();
}
delfilefolder.mkdir();
}
catch (Exception e) {
System.out.println("清空目录操作出错");
e.printStackTrace();
}
7.读取文件
//import java.io.*;
// 逐行读取数据
FileReader fr = new FileReader(%%1);
BufferedReader br = new BufferedReader(fr);
String %%2 = br.readLine();
while (%%2 != null) {
%%3
%%2 = br.readLine();
}
br.close();
fr.close();
8.写入文件
//import java.io.*;
// 将数据写入文件
try {
FileWriter fw = new FileWriter(%%1);
fw.write(%%2);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
9.写入随机文件
//import java.io.*;
try {
RandomAcessFile logFile=new RandomAcessFile(%%1,"rw");
long lg=logFile.length();
logFile.seek(%%2);
logFile.writeByte(%%3);
}catch(IOException ioe){
System.out.println("无法写入文件:"+ioe.getMessage());
}
10.读取文件属性
//import java.io.*;
// 文件属性的取得
File af = new File(%%1);
if (af.exists()) {
System.out.println(f.getName() + "的属性如下: 文件长度为:" + f.length());
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目录" : "不是目录");
System.out.println(f.canRead() ? "可读取" : "不");
System.out.println(f.canWrite() ? "是隐藏文件" : "");
System.out.println("文件夹的最后修改日期为:" + new Date(f.lastModified()));
} else {
System.out.println(f.getName() + "的属性如下:");
System.out.println(f.isFile() ? "是文件" : "不是文件");
System.out.println(f.isDirectory() ? "是目录" : "不是目录");
System.out.println(f.canRead() ? "可读取" : "不");
System.out.println(f.canWrite() ? "是隐藏文件" : "");
System.out.println("文件的最后修改日期为:" + new Date(f.lastModified()));
}
if(f.canRead()){
%%2
}
if(f.canWrite()){
%%3
}
11.写入属性
//import java.io.*;
File filereadonly=new File(%%1);
try {
boolean b=filereadonly.setReadOnly();
}
catch (Exception e) {
System.out.println("拒绝写访问:"+e.printStackTrace());
}
12.枚举一个文件夹中的所有文件
//import java.io.*;
//import java.util.*;
LinkedListString folderList = new LinkedListString();
folderList.add(%%1);
while (folderList.size() 0) {
File file = new File(folderList.peek());
folderList.removeLast();
File[] files = file.listFiles();
ArrayListFile fileList = new ArrayListFile();
for (int i = 0; i files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
%%2=f.getAbsoluteFile();
%%3
}
}
13.复制文件夹
//import java.io.*;
//import java.util.*;
LinkedListString folderList = new LinkedListString();
folderList.add(%%1);
LinkedListString folderList2 = new LinkedListString();
folderList2.add(%%2+ %%1.substring(%%1.lastIndexOf("\\")));
while (folderList.size() 0) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = 0; i file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator
+ file[i]);
} else {
temp = new File(folderList.peek() + File.separator
+ file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator
+ (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {// 如果是子文件夹
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek()
+ File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
//System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
}
14.复制一个文件夹下所有的文件夹到另一个文件夹下
//import java.io.*;
//import java.util.*;
File copyfolders=new File(%%1);
File[] copyfoldersList=copyfolders.listFiles();
for(int k=0;kcopyfoldersList.length;k++){
if(copyfoldersList[k].isDirectory()){
ArrayListStringfolderList=new ArrayListString();
folderList.add(copyfoldersList[k].getPath());
ArrayListStringfolderList2=new ArrayListString();
folderList2.add(%%2+"/"+copyfoldersList[k].getName());
for(int j=0;jfolderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" +
(temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
}
}
15.移动文件夹
//import java.io.*;
//import java.util.*;
LinkedListString folderList = new LinkedListString();
folderList.add(%%1);
LinkedListString folderList2 = new LinkedListString();
folderList2.add(%%2 + %%1.substring(%%1.lastIndexOf("\\")));
while (folderList.size() 0) {
(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File folders = new File(folderList.peek());
String[] file = folders.list();
File temp = null;
try {
for (int i = 0; i file.length; i++) {
if (folderList.peek().endsWith(File.separator)) {
temp = new File(folderList.peek() + File.separator
+ file[i]);
} else {
temp = new File(folderList.peek() + File.separator
+ file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
folderList2.peek() + File.separator
+ (temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
if (!temp.delete())
System.out.println("删除单个文件操作出错!");
}
if (temp.isDirectory()) {// 如果是子文件夹
for (File f : temp.listFiles()) {
if (f.isDirectory()) {
folderList.add(f.getPath());
folderList2.add(folderList2.peek()
+ File.separator + f.getName());
}
}
}
}
} catch (Exception e) {
// System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
folderList.removeFirst();
folderList2.removeFirst();
}
File f = new File(%%1);
if (!f.delete()) {
for (File file : f.listFiles()) {
if (file.list().length == 0) {
System.out.println(file.getPath());
file.delete();
}
}
}
16.移动一个文件夹下所有的文件夹到另一个目录下
//import java.io.*;
//import java.util.*;
File movefolders=new File(%%1);
File[] movefoldersList=movefolders.listFiles();
for(int k=0;kmovefoldersList.length;k++){
if(movefoldersList[k].isDirectory()){
ArrayListStringfolderList=new ArrayListString();
folderList.add(movefoldersList[k].getPath());
ArrayListStringfolderList2=new ArrayListString();
folderList2.add(%%2+"/"+movefoldersList[k].getName());
for(int j=0;jfolderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" +
(temp.getName()).toString());
byte[] b = new byte[5120];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
temp.delete();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
movefoldersList[k].delete();
}
}
17.以一个文件夹的框架在另一个目录创建文件夹和空文件
//import java.io.*;
//import java.util.*;
boolean b=false;//不创建空文件
ArrayListStringfolderList=new ArrayListString();
folderList.add(%%1);
ArrayListStringfolderList2=new ArrayListString();
folderList2.add(%%2);
for(int j=0;jfolderList.length;j++){
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
File folders=new File(folderList.get(j));
String[] file=folders.list();
File temp=null;
try {
for (int i = 0; i file.length; i++) {
if(folderList.get(j).endsWith(File.separator)){
temp=new File(folderList.get(j)+"/"+file[i]);
}
else{
temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
}
FileInputStream input = new FileInputStream(temp);
if(temp.isFile()){
if (b) temp.createNewFile();
}
if(temp.isDirectory()){//如果是子文件夹
folderList.add(folderList.get(j)+"/"+file[i]);
folderList2.add(folderList2.get(j)+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
18.复制文件
//import java.io.*;
int bytesum = 0;
int byteread = 0;
File oldfile = new File(%%1);
try {
if (oldfile.exists()) { //文件存在时
FileInputStream inStream = new FileInputStream(oldfile); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(%%2,oldfile.getName()));
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
19.复制一个文件夹下所有的文件到另一个目录
//import java.io.*;
File copyfiles=new File(%%1);
File[] files=copyfiles.listFiles();
for(int i=0;ifiles.length;i++){
if(!files[i].isDirectory()){
int bytesum = 0;
int byteread = 0;
try {
InputStream inStream = new FileInputStream(files[i]); //读入原文件
FileOutputStream fs = new FileOutputStream(new File(%%2,files[i].getName());
byte[] buffer = new byte[5120];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
}
20.提取扩展名
String %%2=%%1.substring(%%1.lastIndexOf(".")+1);
首先,你的代码有两处错,main函数定义需要 static关键字,
第二是定义数组容量,只需要在new对象的时候设置容量大小,
在你的代码中,目标是取平均值,这里其实不需要定义数组,显得多此一举,您可以自行去掉,下面是你的代码的注释,请采纳!
import java.util.Scanner;
public class Test {
// public void main(String[] args) {
// double[5] cj = new double[5];
//创建主函数
public static void main(String[] args) {
//定义double数组,容量5
double[] cj = new double[5];
//定义扫描键盘输入
Scanner sc = new Scanner(System.in);
int i;
//平均数变量
double avg = 0.0d;
//for循环,变量i从0开始没循环一次i+1,当不满足条件i5时跳出循环
for (i = 0; i 5; i++){
System.out.println("请输入第" + (i + 1) + "个数");
//键盘输入小数并赋值到double数组中
cj[i] = sc.nextDouble();
//将所有输入的数值相加
avg = avg + cj[i];
}
//取平均数
avg = avg / 5;
System.out.println("平均分为:" + avg);
}
}
环境:JDK1.8+。然后复制粘贴代码即可用。
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class MainController {
public static void main(String[] args) {
//一.将图一四个人的信息放入list中
ListStudent students = new ArrayList();
Student s1 = new Student("小赵",getTime("1992.08.11"),400);
Student s2 = new Student("小钱",getTime("1995.01.24"),750);
Student s3 = new Student("小孙",getTime("1990.06.05"),670);
Student s4 = new Student("小李",getTime("1991.12.17"),550);
students.add( s1 );
students.add( s2 );
students.add( s3 );
students.add( s4 );
//二.计算四个人成绩的平均值,并输出每个人比平均值多或少多少分。
System.out.println("二.计算四个人成绩的平均值,并输出每个人比平均值多或少多少分。");
average( students );
//三.将四人排序输出,控制台输出结果请参照图二
System.out.println("三.将四人排序输出,控制台输出结果请参照图二");
sort( students );
//四.用递归计算出小赵的成绩以每月2%的增长,多少个月后他的成绩可以超过小钱。
System.out.println("四.用递归计算出小赵的成绩以每月2%的增长,多少个月后他的成绩可以超过小钱。");
markBeyong( students.get(0).getMark() , students.get(1).getMark() );
}
public static void average( ListStudent students ){
int sum = 0;//总成绩
for ( Student student: students ){
sum = sum + student.getMark();
}
int average = sum/students.size();//平均成绩
students.forEach( student - {
if ( student.getMark() average ){
System.out.println( student.getName() + "比平均分高:" + (student.getMark() - average) );
}else if ( student.getMark() average ){
System.out.println( student.getName() + "比平均分低:" + ( average - student.getMark()) );
}else {
System.out.println( student.getName() + "和平均分一样" );
}
} );
}
public static void sort( ListStudent students ){
ListStudent studentBir = new ArrayList();
studentBir.addAll( students );
System.out.println("①第一种排序,按照出生年月日升序;");
Collections.sort( studentBir, (o1, o2) - ( o1.getBirthDay().compareTo( o2.getBirthDay() ) ) );
studentBir.forEach( stu -{
System.out.println( stu.getName() + " | " + getTime(stu.getBirthDay()) + " | " + stu.getMark() );
} );
System.out.println(" ②第二种排序,按照成绩降序;");
ListStudent markDesc = students.stream().sorted((a, b) - b.getMark() - a.getMark()).collect(Collectors.toList());
markDesc.forEach( stu -{
System.out.println( stu.getName() + " | " + getTime(stu.getBirthDay()) + " | " + stu.getMark() );
} );
System.out.println("③比较输出:计算他们互相大多少天,要考虑到闰年。");
for ( int i = 0 ; i students.size() ; i++ ){
for ( int j = i + 1 ; j students.size() ; j++ ){
Long ageSub = students.get( i ).getBirthDay().getTime() - students.get( j ).getBirthDay().getTime();
Long dateSub = ageSub/(1000*60*60*24);
if ( dateSub 0 ){
System.out.println( students.get( i ).getName() + "比" + students.get( j ).getName() + "大" + dateSub );
}else if ( dateSub 0 ){
System.out.println( students.get( i ).getName() + "比" + students.get( j ).getName() + "小" + -dateSub );
}else {
System.out.println( students.get( i ).getName() + "和" + students.get( j ).getName() + "同日出生" );
}
}
}
}
public static void markBeyong( int mark ,int targetMark){
double markDou = mark;
double targetMarkDou = targetMark;
boolean b = true;
int month = 0;
while ( b ){
if ( markDou targetMarkDou ){
markDou = markDou*1.02;
month ++;
}else {
System.out.println("一共用了" + month + "个月反超");
b = false;
}
}
}
public static Date getTime( String target ) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
Date date = sdf.parse(target);
return date;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getTime( Date target ) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
String date = sdf.format( target );
return date;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
class Student {
private String name;
private Date birthDay;
private Integer mark;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public Integer getMark() {
return mark;
}
public void setMark(Integer mark) {
this.mark = mark;
}
public Student(){
}
public Student(String name,Date birthDay , Integer mark){
this.name = name;
this.birthDay = birthDay;
this.mark = mark;
}
}