java label 添加文字,如何向JLabel添加文本

I am creating a book reader, which takes the content of a file and sends it to an Object[]. I want this to be displayed, line by line on my page. I'm considering a loop of some sort that will add text to the label, but here's my question: How to I add text to the end of a JLabel, rather than setting the whole thing?

解决方案

You can use getText() to retrieve what's there, and then setText() to set the new value.

So to add something to the end, you'd do

label.setText(label.getText()+"something");

Remember you'll probably be wanting to add a space in the middle. If you've got a new String str you want to append, you will probably want

label.setText(label.getText()+" "+str);

to make sure you add the space and then the contents of str.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
淮海工学院计算机工程学院 实验报告书 课程名:《面向对象程序设计》 实验名称: java程序设计基础 班 级: 学 号: 姓 名: 一、目的与要求 1、熟悉Java开发工具 认识J2SE开发环境,对TextPad、JCreator、NetBeans、Eclipse等开发工具有初步的 了解,能够利用以上的某一种开发工具编写调试简单的Java Application和Applet程序,了解Java程序的编辑、编译和运行过程。 2、学会简单的Java程序设计 掌握Java的数据类型、变量、数组、表达式、流程控制语句的实用,并能编写Java Application和Applet,正确运用变量、表达式和流程控制语句,对你字符、图形界面下 的输入、输出有初步的体验。 二、实验内容或题目 1、编写一个Java Application字符界面程序,实现数论中的某个基本算法:最大公约数和最小公倍数的求 解。 2、编写一个Java Applet程序,利用图形界面输入一个数据,判断是否是回文数,并将结果输出在图形界 面中。 3、编写一个趣味性Java Applet小程序,根据界面上随机生成或任意输入的一个日期型数据,判断是否闰年,算 出是周几,同时判断对应的星座与性格,输出星座图片。 三、实验步骤与源程序 第一题 import java.util.Scanner; public class Test1 { public static void main(String args[]) { Scanner sc= new Scanner(System.in);//调用java.util.Scanner可以获得从键盘输入的数字 int a,b,m,n,temp;//定义整型数字的变量 System.out.print("输入第一个整数:"); a=sc.nextInt();//nextInt()方法用来获取输入的数字 System.out.print("输入第二个整数:"); b=sc.nextInt();//nextInt()方法用来获取输入的数字 if(a<b) { temp=a; a=b; b=temp; } m=a*b; while(b!=0) { n=a%b; a=b; b=n; } System.out.println("最大公约数是:"+a);//输出结果 System.out.println("最小公倍数是:"+m/a); } } 第二题 import java.applet.*;//加载applet包 import java.awt.*;//加载图形界面设计要用的抽象窗口工具包 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Test2 extends Applet implements ActionListener { Label prompt1,prompt2;//定义两个标签 TextField input;//定义一个文本框,用于输入 Button btn;//定义一个按钮 public void init()//实现Applet的init方法,初始化界面 { prompt1 = new Label("请输入一个整数:");//设置标签提示字符信息 prompt2 = new Label(" "); input = new TextField(10);//设置输入文本框的显示高度 btn = new Button("判断");//设置按钮上面的提示字符 setLayout(new FlowLayout());//设置窗体上各控件的布局为流式布局 add(prompt1); add(input); add(btn); add(prompt2); btn.addActionListener(this);//为按钮注册监听器对象 setSize(200,200);//设置窗口宽度、高度 setVisible(true);//让窗口可见 } public void actionPerformed(ActionEvent e) { if(e.getSource()==btn) { StringBuffer source = new StringBuffer(input.getText()); source.reverse(); String reverseString = new String(source); if(reverseString.equals(input.getText())) prompt2.setText(input.getText()+"是回文!"); else prompt2.setText(

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值