Java 杂七杂八的笔记

1 篇文章 0 订阅
1 篇文章 0 订阅

记录一个菜逼的成长。。

PS:因为Java不常用,但现在还要用。鉴于自己记性差,写在这里,以后翻看方便。

Java

Java控制精度

DecimalFormat是用来控制格式的

DecimalFormat df = new DecimalFormat("0.00"); //保留小数点后两位,精度可自行调整
//如要输出double类型的变量a,则
System.out.println(df.format(a));

高精度类型BigDecimal

Java里还有一个高精度类型BigDecimal

//BigDecimal bd = new BigDecimal(12.233333);
//也可以输入一个数
BigDecimal bd = cin.nextBigDecimal();
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(bd));

获取当前时间,以当前时间为种子生成随机数

long t = System.currentTimeMillis();
Random rd = new Random(t);
System.out.println(rd.nextInt(100));[0,100)

问题描述:
在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了FileOutputStream(文件名,true)间接的构建了ObjectOutputStream流对象,在向外读数据的时候第一次运行的时候不会报错,在第二次就会报java.io.StreamCorruptedException: invalid type code: AC错误。
[转]解决方法

文件流输入输出

输入

FileReader fin = null;
try{
    fin = new FileReader();
    char ch;
    while((ch = fin.read()) != -1){

    }
}catch(Exception e){
    e.printStackTrace();
}finally{
    try{
        if(fin != null)fin.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

输出(附换行)

FileWriter fout = null;
try{
    String str = "";
    fout = new FileWriter();
    //如果要输出换行.(网上的什么butterwrite,\r\n的,全是瞎扯。。
    str += "aaadfsaf";
    fout.write(fout + System.getProperty("line.separator"));
}catch(Exception e){
    e.printStackTrace();
}finally{
    try{
        if(fin != null)fout.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

Javafx

生成一个窗口

Pane pane = new Pane();
Stage stage = new Stage();


Scene scene = new Scene(pane,width,height);
stage.setScene(scene);
stage.setTitle("title");
stage.show();

Button

创建按钮

Button button = new Button();
//Button button = new Button("OK");

为按钮添加鼠标处理事件

public class ClickEvent implements EventHandler<MouseEvent>{
    public void handle(MouseEvent e){
        //处理事件
    }
}
ClickEvent handler = new ClickEvent();
button.setOnMouseClicked(handler);

为按钮添加图片

Image imageBack = new Image("/res/img/back.png");//相对路径,绝对路径貌似不行。。
button.setGraphic(new ImageView(imageBack));

Label

设置内容

Label label = new Label();
label.setText("Accepted");

设置颜色

Label label = new Label();
label.setTextFill(Color.web("#FFFFFF"));//白色

设置字体与大小

Label label = new Label();
label.setFont(new Font("Arial", 30));
//font(String family, FontWeight weight, double size)
label.setFont(Font.font("Arial",FontWeight.BLACK,30))  

Pane

AnchorPane

AnchorPane pane = new AnchorPane();
//添加控件
pane.getChildren().add(button);
//给第i+1个控件设置位置
pane.getChildren().get(i).setLayoutX(50.0);//距离左边界
pane.getChildren().get(i).setLayoutY(50.0);//距离上边界

给平面设置背景图片

Image image = new Image("your url",width,height,false,true);
BackgroundImage background = new BackgroundImage(image, null, null, null, null);
pane.setBackground(new Background(background));
//API
public Image(String url,
             double requestedWidth,
             double requestedHeight,
             boolean preserveRatio,
             boolean smooth)

url - the string representing the URL to use in fetching the pixel data
requestedWidth - the image's bounding box width
requestedHeight - the image's bounding box height
preserveRatio - indicates whether to preserve the aspect ratio of the original image when scaling to fit the image within the specified bounding box
smooth - indicates whether to use a better quality filtering algorithm or a faster one when scaling this image to fit within the specified bounding box

TextField

设置文本

TextField text = new TextField("****");// 1.可以用构造方法
text.setText("****");// 2.也可以用setText方法

从文本框获取内容

TextField text = new TextField();
CharSequence cs = text.getCharacters();
String str = cs.toString();

播放音乐

URL music = getClass().getResource("路径");
AudioClip sound = Applet.newAudioClip(music);
sound.play();
sound.loop();  //循环播放 写了loop就不用再写play
sound.stop();  //暂停播放

jar包

执行jar包命令

只有export runnable jar file才可以运行
只是普通的jar file是会报错的。

$ java -jar name.jar
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值