I have created a JTextField as follows:
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:
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. 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 = 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));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.