使用JTextArea示例

相对于JLabel显示提示文字,JTextArea有一个先天优势:文字可以拷贝出来。经过下面设置它也能在外观上和JLabel一致。

代码如下:

JTextArea txtArea=new JTextArea(5,20);// 初始化
// 设置文字
txtArea.setText("We needto update personal business forecast data in staffing plan to get the budgetupdates. Due to the complexities of Staffing Plan sheet, it is necessary tohave one tool, which will help to inject the forecasted data into the staffingplan sheet automatically. The tool will thus, save time of a financial analystto get the budget on time.");
txtArea.setFont(new Font("Arial",Font.PLAIN,14));// 设置字体
txtArea.setLineWrap(true);// 自动换行
txtArea.setWrapStyleWord(true);// 根据单词封装换行字符
txtArea.setBackground(this.getBackground());// 取所在面板的背景色
txtArea.setPreferredSize(new Dimension(800, 30));// 设置宽度

效果如下:

注意以上图文无关。

原文地址:https://www.cnblogs.com/heyang78/p/3698463.html