繁体   English   中英

单击后更改按钮文本,然后使用JAVA ActionEvent再次单击后还原更改

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

单击按钮后,我将显示编写的用于更改文本的代码。 现在,我试图通过再次单击按钮来获取默认文本,但是无法这样做。 请指教。

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");

        }
     }   
  }

您可以使用if -statement检查按钮的文本:

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

例如,您可以使用默认文本检查实际文本。

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);
    }
 }   }

或者,您可以使用更简单的方式使用布尔标志来做到这一点:

 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;
        }
    }
 }   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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