java line separator_Java System.lineSeparator()用法及代码示例

lineSeparator()是Java中的内置方法,该方法返回system-dependent行分隔符字符串。它总是返回相同的值–系统属性line.separator的初始值。

用法:

public static String lineSeparator()

参数:此方法不带任何参数。

返回值:在任何UNIX系统中,它将返回“\n”或正整数;在Windows系统上,它将返回“\r\n”或正整数。

异常:如果字符串为null,则抛出NullPointerException

以下示例程序旨在说明System.lineSeparator()方法:

示例1:为了说明静态字符串lineSeparator()方法的用法。

// Java program to demonstrate working

// of static String lineSeparator() method

import java.io.IOException;

import java.lang.*;

import java.nio.channels.Channel;

public class LineSeparatorExample {

public static void main(String[] args)

{

String s = System.lineSeparator();

for (char c : s.toCharArray()) {

System.out.println((int)c);

}

}

}

输出:

10

注意:在这里它返回10。所以在这里10是行分隔符。

示例2:为了说明静态字符串lineSeparator()方法用于整数值的工作原理。

// Java program to demonstrate working

// of static String lineSeparator() method

import java.io.IOException;

import java.lang.*;

import java.nio.channels.Channel;

class SystemDemo {

public static void main(String args[])

throws NullPointerException,

IOException

{

Integer x = 636;

System.out.println(System.lineSeparator());

}

}

输出:

\r\n

注意:由于它是Microsoft Windows系统,因此在此返回“\r\n”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给JFrame添加背景图片,可以使用JLabel来承载图片,然后将JLabel添加到JFrame中。 以下是修改后的代码: ``` package game; import java.awt.FlowLayout; import javax.swing.*; public class Jframe { public static void main(String[] args) { JFrame frame = new JFrame("横版英雄泪闯关游戏"); frame.setSize(800, 500); frame.setLocation(550, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); // 使用 null 布局 // 背景图片 ImageIcon bg = new ImageIcon("bg.jpg"); // 修改为你的图片路径 JLabel bgLabel = new JLabel(bg); bgLabel.setBounds(0, 0, 800, 500); frame.add(bgLabel); // 文字区域 JTextArea area = new JTextArea(9, 10); area.setEditable(false); // 禁止编辑 area.setOpaque(false); // 设置透明 area.setText("______________________________________________________________________________________________________" +System.lineSeparator()+"| 欢迎来到猿魔 |"+System.lineSeparator()+"| |" +System.lineSeparator()+"| 1.查看角色技能及属性 |" +System.lineSeparator()+"| |" +System.lineSeparator()+"| 2.查看装备属性与装备合成 |" +System.lineSeparator()+"| |" +System.lineSeparator()+"| 3.闯关(共五关) |" +System.lineSeparator()+"| |" +System.lineSeparator()+"| 4.退出游戏 |" +System.lineSeparator()+"______________________________________________________________________________________________________"); bgLabel.add(area); // 将 JTextArea 添加到 JLabel 中 area.setBounds(0, 0, 800, 400); // 四个按钮 JButton button1 = new JButton("查看角色技能及属性"); button1.setBounds(100, 430, 200, 30); bgLabel.add(button1); JButton button2 = new JButton("查看装备属性与装备合成"); button2.setBounds(350, 430, 200, 30); bgLabel.add(button2); JButton button3 = new JButton("闯关(共五关)"); button3.setBounds(100, 470, 200, 30); bgLabel.add(button3); JButton button4 = new JButton("退出游戏"); button4.setBounds(350, 470, 200, 30); bgLabel.add(button4); frame.setVisible(true); } } ``` 需要注意的是,使用了 null 布局后,需要手动设置组件的位置和大小。在这个例子中,我们将JTextArea添加到了JLabel中,再将JLabel添加到了JFrame中。同时,我们还设置了JTextArea和JButton的透明度为false,这样才能让背景图片显示出来。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值