how to make text appear when a button is clicked

喜夏-厌秋 提交于 2019-12-12 01:37:41

问题


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

}


回答1:


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



回答2:


This part needs to be in constructor.

    label = new JLabel("Text you want to be seen");
    add(label);

This code needs to be in actionPerformed() method.

label.setVisible(!label.isVisible()); // This code will be the change of visibility of the label.


来源:https://stackoverflow.com/questions/37132055/how-to-make-text-appear-when-a-button-is-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!