JButton.setText

setText( ) method possible to change the text inside a JButton during program execution.

Program

hj

 

 

 

Program Source

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class Frame extends JFrame implements ActionListener{
    
    private JButton jb;
    private int counter = 0;
    
    Frame()
    {
        setTitle("Click Counter");
        setLayout(new FlowLayout());
        setButtons();
        setAction();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void setButtons()
    {
        jb = new JButton("Click Me");
        add(jb);
    }
    
    private void setAction()
    {
        jb.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent eve)
    {
        jb.setText(Integer.toString(counter));
        counter++;
    } 
}

public class Javaapp {
   
    public static void main(String[] args) {
        
        Frame frame = new Frame();
    }
}

Leave a Comment