记录一个菜逼的成长。。
PS:因为Java不常用,但现在还要用。鉴于自己记性差,写在这里,以后翻看方便。
Java
Java控制精度
DecimalFormat是用来控制格式的
DecimalFormat df =
new DecimalFormat(
"0.00");
System.
out.println(df.format(a));
高精度类型BigDecimal
Java里还有一个高精度类型BigDecimal
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))
问题描述: 在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了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();
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 =
new Button();
为按钮添加鼠标处理事件
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);
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(
"****")
text.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();
sound.stop();
jar包
执行jar包命令
只有export runnable jar file才可以运行 只是普通的jar file是会报错的。
$ java -jar name.jar
转载请注明原文地址: https://ju.6miu.com/read-200186.html