简体   繁体   中英

Change button text after click, then revert the changes after clicking again using JAVA ActionEvent

I am displaying the code that I wrote for changing the text when a button is clicked. Now I am trying to get the default text by clicking the button again, but unable to do so. Please advice.

import javax.swing.*;
import java.awt.event.*;

public class Actions extends JFrame implements ActionListener{

    JPanel pnl=new JPanel();
    ImageIcon ic1=new ImageIcon("/home/mudit/Downloads/duke.png");
    JButton btn1=new JButton("Click",ic1);
    JTextArea ta=new JTextArea(5,37);

    public static void main(String[]args)   
     {
        Actions gui=new Actions();
     } 


    public Actions()
     {
        super("Swing Window");
        setSize(500,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(pnl);
        btn1.setHorizontalTextPosition(JButton.CENTER);
        btn1.setVerticalTextPosition(JButton.BOTTOM);
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        pnl.add(btn1);
        pnl.add(ta);
        btn1.setEnabled(true);
        ta.setText("Button is enabled");
        btn1.addActionListener(this);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent event)
     {
        if(event.getSource()==btn1)
        {
            ta.setText("Button is Clicked");

        }
     }   
  }

You can check the text of the button with an if -statement:

if (event.getSource() == btn1) {
    if (ta.getText().equals("Button is enabled")) {
        ta.setText("Button is Clicked");
    } else {
        ta.setText("Button is enabled");
    }
}

You could check actual text with the default text for example.

JPanel pnl=new JPanel();
ImageIcon ic1=new ImageIcon("/home/mudit/Downloads/duke.png");
JButton btn1=new JButton("Click",ic1);
JTextArea ta=new JTextArea(5,37);
String defaultText = "Button is enabled";

public static void main(String[]args)   
 {
    Actions gui=new Actions();
 } 


public Actions()
 {
    super("Swing Window");
    setSize(500,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(pnl);
    btn1.setHorizontalTextPosition(JButton.CENTER);
    btn1.setVerticalTextPosition(JButton.BOTTOM);
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    pnl.add(btn1);
    pnl.add(ta);
    btn1.setEnabled(true);
    ta.setText(defaultText);
    btn1.addActionListener(this);
    setVisible(true);
}
public void actionPerformed(ActionEvent event)
 {
    if(event.getSource()==btn1)
    {
        if(ta.getText().equals(defaultText))
              ta.setText("Button is Clicked");
        else
              ta.setText(defaultText);
    }
 }   }

Or in a simpler way you could use a boolean flag to do it so:

 JPanel pnl=new JPanel();
ImageIcon ic1=new ImageIcon("/home/mudit/Downloads/duke.png");
JButton btn1=new JButton("Click",ic1);
JTextArea ta=new JTextArea(5,37);
boolean hasBeenSet = false;

public static void main(String[]args)   
 {
    Actions gui=new Actions();
 } 


public Actions()
 {
    super("Swing Window");
    setSize(500,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(pnl);
    btn1.setHorizontalTextPosition(JButton.CENTER);
    btn1.setVerticalTextPosition(JButton.BOTTOM);
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    pnl.add(btn1);
    pnl.add(ta);
    btn1.setEnabled(true);
    ta.setText(defaultText);
    btn1.addActionListener(this);
    setVisible(true);
}
public void actionPerformed(ActionEvent event)
 {
    if(event.getSource()==btn1)
    {
        if(!hasBeenSet){
              ta.setText("Button is Clicked");
              hasBeenSet = true;
        }
        else{
              ta.setText(defaultText);
              hasBeenSet = false;
        }
    }
 }   }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM