如何显示在nextline中的JTextArea递增整数 [英] How to display incremented integer in nextline in JTextArea

查看:173
本文介绍了如何显示在nextline中的JTextArea递增整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我创建一个按钮被点击时被加一个变量的JTextArea。不过,我想显示在下一行,旧值停留在previous行递增号。

到现在我有codeD以下的动作侦听器,它是递增的,但取代了旧的价值和下一行不显示:

 公共无效的actionPerformed(ActionEvent的五){
    // TODO自动生成方法存根
      如果(e.getActionCommand()。等于(公司))
    {      串H =/ N的;
      INT结果=的Integer.parseInt(area.getText())+ 1;
      字符串ASTRING = Integer.toString(结果);      area.setText(将String.valueOf(ASTRING));
    }


解决方案

使用的 的JTextArea追加(字符串str)方法

 公共无效的actionPerformed(ActionEvent的五){
// TODO自动生成方法存根
  如果(e.getActionCommand()。等于(公司))
{
  字符串文本= area.getText();
  INT lastValue =的Integer.parseInt(text.charAt(text.length())); //获取JTextArea中的文本的最后一个值
  串ASTRING = Integer.toString(lastValue + 1); //增加该值
  area.append(\\ n+将String.valueOf(ASTRING)); //这个附加价值的JTextArea中
}

这将给定文本追加到JTextArea中的末尾,用\\ n它插入新行。

I have created a JTextArea in which I have created a variable which is incremented when a button is clicked. However I want the incremented number to be displayed in the next Line and the old value stay in the previous line.

Till now I have coded the following for action listener and it is incrementing but replaces the old value and does not display on next line:

 public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
      if(e.getActionCommand().equals("Inc")) 
    {

      String h = "/n";
      int result = Integer.parseInt(area.getText())+1;
      String aString = Integer.toString(result);

      area.setText(String.valueOf(aString));
    }

解决方案

Use the JTextArea append(String str) method:

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
  if(e.getActionCommand().equals("Inc")) 
{
  String text=area.getText();
  int lastValue=Integer.parseInt(text.charAt(text.length())); //gets the last value of the JTextArea text
  String aString = Integer.toString(lastValue+1); //Increment this value      
  area.append("\n"+String.valueOf(aString)); //Append this value to the JTextArea
}

It appends the given text to the end of the JTextArea and with the "\n" it inserts a new line.

这篇关于如何显示在nextline中的JTextArea递增整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆