title: How to repaint a JLabel?
body: <p>I have a problem with my calculator which i want to make in Java.
  I have a JLabel which shoulds display over buttons inputted numbers. My problem is every time the user press a button, the frame get not repainted, but it opens a new JFrame with the updated JLabel text. I think the new JFrame belongs to the new in my generateCalculatorFrame()-method, but I dont know how to repaint my JFrame rightly. I have searched for solutions about two hours and asked a more experienced programmer than me, but he also couldnt found a solution. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>package com.decase.taschenrechner;
  </code></pre>
  
  <p>public class Calculator {</p>
  
  <pre><code>public static void main(String[] args) {
  
      // Erzeugt das CalculatorFrame
      CalculatorFrame.generateCalculatorFrame();
  
      // Legt die Funktionen der Buttons fest
      ButtonEvents.generateButtonEvents();
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>package com.decase.taschenrechner;
  </code></pre>
  
  <p>import javax.swing.JFrame;</p>
  
  <p>public class CalculatorFrame {</p>
  
  <pre><code>public static JFrame generateCalculatorFrame ()
  {
    JFrame calculatorFrame = new JFrame("Taschenrechner");
    // Legt die Größe des Fensters fest
    calculatorFrame.setSize(450, 600);
    // Schließt das Fenster beim Klick auf "X"
    calculatorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Fügt das calculatorPanel dem calculatorFrame hinzu
    calculatorFrame.add(CalculatorPanel.generateCalculatorPanel());
    // Setzt die Position des Fensters auf Null, also auf die Mitte des Bildschirms
    calculatorFrame.setLocationRelativeTo(null);
    // Setzt die Sichtbarkeit des Fensters auf sichtbar
    calculatorFrame.setVisible(true);
    return calculatorFrame;
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>package com.decase.taschenrechner;
  </code></pre>
  
  <p>import java.awt.Color;
  import java.awt.GridLayout;</p>
  
  <p>import javax.swing.BoxLayout;
  import javax.swing.JPanel;</p>
  
  <p>public class CalculatorPanel {</p>
  
  <pre><code>public static JPanel generateCalculatorPanel ()
  {
    // Erzeugt ein JPanel auf dem die anderen beiden Panel dargestellt werden
    JPanel calculatorPanel = new JPanel();
    // Legt das Layout des  JPanels fest
    calculatorPanel.setLayout(new BoxLayout(calculatorPanel, BoxLayout.PAGE_AXIS));
    // Legt die Farbe des JPanels fest
    calculatorPanel.setBackground(Color.DARK_GRAY);
    // Legt das Layout des  JPanels fest
    calculatorPanel.setLayout(new GridLayout(2, 1));
    calculatorPanel.add(CalculatorDisplay.generateCalculatorDisplay());
    calculatorPanel.add(generateButtonPanel());
    return calculatorPanel;       
  }
  
  public static JPanel generateButtonPanel ()
  {
    // Erzeugt ein neues JPanel auf dem die Buttons dargestellt werden
    JPanel buttonPanel = new JPanel();
    // Legt die Farbe des JPanels fest
    buttonPanel.setBackground(Color.DARK_GRAY);
    // Legt das Layout des  JPanels fest
    buttonPanel.setLayout(new GridLayout(5, 4));
    // Fügt die einzelnen Buttons dem JPanel hinzu
    buttonPanel.add(CalculatorButtons.buttonOne);
    buttonPanel.add(CalculatorButtons.buttonTwo);
    buttonPanel.add(CalculatorButtons.buttonThree);
    buttonPanel.add(CalculatorButtons.buttonDeleteLast);
    buttonPanel.add(CalculatorButtons.buttonFour);
    buttonPanel.add(CalculatorButtons.buttonFive);
    buttonPanel.add(CalculatorButtons.buttonSix);
    buttonPanel.add(CalculatorButtons.buttonPlus);
    buttonPanel.add(CalculatorButtons.buttonSeven);
    buttonPanel.add(CalculatorButtons.buttonEight);
    buttonPanel.add(CalculatorButtons.buttonNine);
    buttonPanel.add(CalculatorButtons.buttonMinus);
    buttonPanel.add(CalculatorButtons.buttonDeleteAll);
    buttonPanel.add(CalculatorButtons.buttonComma);
    buttonPanel.add(CalculatorButtons.buttonNull);
    buttonPanel.add(CalculatorButtons.buttonMultiply);
    buttonPanel.add(CalculatorButtons.buttonBracketRight);
    buttonPanel.add(CalculatorButtons.buttonBracketLeft);
    buttonPanel.add(CalculatorButtons.buttonEqual);
    buttonPanel.add(CalculatorButtons.buttonDivide);
    return buttonPanel;
  }       
  </code></pre>
  
  <p>}</p>
  
  <pre><code>package com.decase.taschenrechner;
  </code></pre>
  
  <p>import java.awt.Color;</p>
  
  <p>import javax.swing.JLabel;</p>
  
  <p>public class CalculatorDisplay {</p>
  
  <pre><code>public static String displayText = "";
  
  public static JLabel generateCalculatorDisplay ()
  {
      // Erstellt ein neues JLabel
      JLabel calculatorDisplay = new JLabel();
      // Legt die Größe des JLabels fest
      calculatorDisplay.setSize(50, 50);
      // Legt die Hintergrundfarbe des JLabels fest
      calculatorDisplay.setOpaque(true);
      calculatorDisplay.setBackground(Color.BLACK);
      // Legt die Schriftgröße des JLabels fest
      calculatorDisplay.setFont(calculatorDisplay.getFont().deriveFont(30f));
      // Legt die Schriftfarbe des JLabels fest
      calculatorDisplay.setForeground(Color.WHITE);
      // Legt den Text des JLabels fest
      calculatorDisplay.setText(displayText);
      // Gibt das JLabel zurück
      return calculatorDisplay;
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>package com.decase.taschenrechner;
  </code></pre>
  
  <p>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;</p>
  
  <p>public class ButtonEvents {</p>
  
  <pre><code>public static void generateButtonEvents() {
  
      // Legt die Funktionen von buttonNull fest
      CalculatorButtons.buttonNull.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "0";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
  
      // Legt die Funktionen von buttonOne fest
      CalculatorButtons.buttonOne.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "1";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
  
      // Legt die Funktionen von buttonTwo fest
      CalculatorButtons.buttonTwo.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "2";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonThree fest
      CalculatorButtons.buttonThree.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "3";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonFour fest
      CalculatorButtons.buttonFour.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "4";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonFive fest
      CalculatorButtons.buttonFive.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "5";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonSix fest
      CalculatorButtons.buttonSix.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "6";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonSeven fest
      CalculatorButtons.buttonSeven.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "7";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonEight fest
      CalculatorButtons.buttonEight.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "8";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonNine fest
      CalculatorButtons.buttonNine.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "9";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonComma fest
      CalculatorButtons.buttonComma.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + ".";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonPlus fest
      CalculatorButtons.buttonPlus.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "+";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonMinus fest
      CalculatorButtons.buttonMinus.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "-";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonMultiply fest
      CalculatorButtons.buttonMultiply.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "x";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonDivide fest
      CalculatorButtons.buttonDivide.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "\u00F7";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonBracketRight fest
      CalculatorButtons.buttonBracketRight.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "(";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonBracketLeft fest
      CalculatorButtons.buttonBracketLeft.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + ")";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonEqual fest
      CalculatorButtons.buttonEqual.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = CalculatorDisplay.displayText + "=";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
      // Legt die Funktionen von buttonDeleteLast fest
      CalculatorButtons.buttonDeleteLast.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              try {
                  CalculatorDisplay.displayText = CalculatorDisplay.displayText.substring(0,
                          CalculatorDisplay.displayText.length() - 1);
                  CalculatorFrame.generateCalculatorFrame().repaint();
              } catch (Exception e2) {
              }
          }
      });
      // Legt die Funktionen von buttonDeleteAll fest
      CalculatorButtons.buttonDeleteAll.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              CalculatorDisplay.displayText = "";
              CalculatorFrame.generateCalculatorFrame().repaint();
          }
      });
  
  }
  </code></pre>
  
  <p>}</p>