【问题标题】:how to make text appear when a button is clicked单击按钮时如何使文本出现
【发布时间】:2016-09-05 01:11:28
【问题描述】:

现在在我的 APCS 课程中,我们正在学习如何编写 GUI。我们已经学习了如何制作一个按钮并将背景颜色更改为绿色、红色、蓝色等。但是,我的老师这周剩下的时间都不会在这里,我只是好奇如何让文本出现在框架内单击一个按钮,并在我再次单击该按钮时使文本消失。如果有帮助,下面是代码。我想将背景颜色更改为绿色并在屏幕上显示“绿色”。非常感谢您的帮助!

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

}

【问题讨论】:

    标签: java eclipse text awt jbutton


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这部分需要在构造函数中。

          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.
      

      【讨论】:

      • 1) 不需要setVisible(..) 调用(没有文本/图标的标签已经不可见)。 2) 在运行时添加的组件不仅仅需要被实例化并添加到某些东西上。 3)*“你应该将标签声明为静态”不,你不应该。 - 有关正确方法,请参阅其他答案。
      • 1) 我不知道他/她是否会使用没有背景颜色的标签。 (没有文本/图标的标签并不总是不可见的)。 2)我想有一个误解,添加部分应该在构造函数中。我是认真的,但显然我应该写得更好 3)我认为没有 datBoi 对象,对不起我的不好:D。
      猜你喜欢
      • 1970-01-01
      • 2021-01-30
      • 2017-02-05
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多