[英]increment value of integer in JTextArea
I have created a JTextField as follows: 我创建了一个JTextField,如下所示:
int val = 0;
JTextArea area = new JTextArea(20,15);
add(new JScrollPane(area), BorderLayout.CENTER);
I have added the value of val to the JTextArea: 我已经将val的值添加到JTextArea中:
area.setText(String.valueOf(val));
I have created a button called"Inc" which increments the value of val by 1 and displays it in the JTextArea BUT IT IS NOT WORKING. 我创建了一个名为“ Inc”的按钮,该按钮将val的值增加1,并在JTextArea中显示它,但它不起作用。 The action listener is as follows:
动作侦听器如下:
JMenuItem inc = new JMenuItem("Inc");
menu.add(inc);
JMenuItem
inc.addActionListener(this);
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("Inc"))
{
int result= val + 1;
area.setText(String.valueOf(result));
}
You'll need to post a Minimal, Complete, and Verifiable example 您需要发布一个最小,完整和可验证的示例
In the meantime, it looks like val
is an instance variable so will remain at 0
unless you change the value itself 同时,好像
val
是一个实例变量,因此除非您更改值本身,否则它将保持为0
val = val + 1;
area.setText(String.valueOf(val));
I think what you want in your action listener is: 我认为您在动作监听器中想要的是:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Inc"))
{
int result= Integer.parseInt(area.getText()) + 1;
area.setText(String.valueOf(result));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.