Search code examples
javaeclipsetextawtjbutton

how to make text appear when a button is clicked


In my APCS class right now, we are learning how to program GUIs. We have learned about making a button and changing the background color to green, red, blue etc. However, my teacher will not be here for the rest of this week and I was just curious about how I can make text appear inside of the frame with the click of a button, and make the text disappear when I click the button again. If it helps, below is the code. I want to change the background color to green as well as displaying "green" on the screen. Thank you so much for your help!

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

    public class datBoi extends JFrame implements ActionListener{

JButton datBoi;

public datBoi(String title)
{
    super(title);

    datBoi = new JButton("dat boi");
    datBoi.setActionCommand("dat boi");


    datBoi.addActionListener(this);


    setLayout(new FlowLayout());
    add(datBoi);


}

public void actionPerformed( ActionEvent evt)
  {
    // check which command has been sent
    if ( evt.getActionCommand().equals( "dat boi" ) )
    { getContentPane().setBackground(  Color.green  );    

    }


    repaint();
  }

  public static void main ( String[] args )
  {
    datBoi demo  = new datBoi( "Get ready to be memed" ) ;

    demo.setSize( 420, 420 );     
    demo.setVisible( true );      
  }

}


Solution

  • Add JLabel add them into the JPanel for futher use. Use the function that I provided for displaying Your text and Green Text; You can change the text by changing it in the "" area.

    The code will be as below:

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
        public class datBoi extends JFrame implements ActionListener{
    
    JButton datBoi;
    JLabel jf;
    JLabel label;
    
    public datBoi(String title)
    {
        super(title);
    
        datBoi = new JButton("dat boi");
        datBoi.setActionCommand("dat boi");
    
    
        datBoi.addActionListener(this);
        jf = new JLabel();
        JPanel panel = new JPanel();
        panel.add(jf);
        getContentPane().add(panel);
    
        setLayout(new FlowLayout());
        add(datBoi);
        JPanel panel2 = new JPanel();
        getContentPane().add(panel2);
    
        label = new JLabel();
        panel.add(label);
    
    }
    
    public void actionPerformed( ActionEvent evt)
      {
        // check which command has been sent
        if ( evt.getActionCommand().equals( "dat boi" ) )
        { getContentPane().setBackground(  Color.green  );    
                if(jf.getText().equals("")){
                    jf.setText("put your text here");  
                }else{
                    jf.setText("");  
                }
                label.setText("GREEN");
        }
    
    
        repaint();
      }
    
      public static void main ( String[] args )
      {
        datBoi demo  = new datBoi( "Get ready to be memed" ) ;
    
        demo.setSize( 420, 420 );     
        demo.setVisible( true );      
      }
     }