How to update a JLabel text?

后端 未结 5 876
一整个雨季
一整个雨季 2020-12-11 07:43

I am making a Hangman game and one of the things I want to make is JLabel text , which updates with ex.\"_ _ _ _ \", depending on word.

I

5条回答
  •  Happy的楠姐
    2020-12-11 07:57

    This will create a new jLabel and set its text.

    JLabel label = new JLabel();
    label.setText("____");
    

    You will need to add this label to something like a JFrame.

    If you want to quick and easy, here is the code to make a simple window with a label.

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class App {
    
      public static void main(String[] args) {
        JFrame frame = new JFrame("Swing Frame");
    
        JLabel label = new JLabel("This is a Swing frame", JLabel.CENTER);
        label.setText("____");  // Look familiar?  <----------
    
        frame.add(label);
    
        frame.setSize(350, 200); // width=350, height=200
        frame.setVisible(true); // Display the frame
      }
    
    }
    

提交回复
热议问题