如何在单击按钮时显示文本
how to make text appear when a button is clicked
现在在我的 APCS class 中,我们正在学习如何对 GUI 进行编程。我们已经学习了如何制作一个按钮并将背景颜色更改为绿色、红色、蓝色等。但是,我的老师这周剩下的时间不会来这里,我只是好奇如何让文本出现在框架内单击一个按钮,并在我再次单击该按钮时使文本消失。如果有帮助,下面是代码。我想将背景颜色更改为绿色并在屏幕上显示 "green"。非常感谢您的帮助!
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 );
}
}
这部分需要在构造函数中。
label = new JLabel("Text you want to be seen");
add(label);
此代码需要在 actionPerformed() 方法中。
label.setVisible(!label.isVisible()); // This code will be the change of visibility of the label.
添加JLabel 将它们添加到JPanel 中以供进一步使用。
使用我提供的显示您的文本和绿色文本的功能;
您可以通过在“”区域更改文本来更改文本。
代码如下:
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 );
}
}
现在在我的 APCS class 中,我们正在学习如何对 GUI 进行编程。我们已经学习了如何制作一个按钮并将背景颜色更改为绿色、红色、蓝色等。但是,我的老师这周剩下的时间不会来这里,我只是好奇如何让文本出现在框架内单击一个按钮,并在我再次单击该按钮时使文本消失。如果有帮助,下面是代码。我想将背景颜色更改为绿色并在屏幕上显示 "green"。非常感谢您的帮助!
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 );
}
}
这部分需要在构造函数中。
label = new JLabel("Text you want to be seen");
add(label);
此代码需要在 actionPerformed() 方法中。
label.setVisible(!label.isVisible()); // This code will be the change of visibility of the label.
添加JLabel 将它们添加到JPanel 中以供进一步使用。 使用我提供的显示您的文本和绿色文本的功能; 您可以通过在“”区域更改文本来更改文本。
代码如下:
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 );
}
}