Patrick Donny
Patrick Donny

Reputation: 15

JLabel not showing on frame or on a panel

I'm making a calculator as a final for a Java class and I've run into an issue that has halted all progress on the project. I've got all the buttons on a panel and put onto a frame, but for some reason I can't seem to get a JLabel with the entered numbers to appear on the frame. I know I'm probably missing something dumb, but help would be appreciated.

packing class:

import javax.swing.*;
import java.awt.*;

public class CalculatorFrame
{
public static void main (String[] args)
{

    JFrame frame = new JFrame ("Calculator");
    JPanel primary = new JPanel();
    JPanel numPad = new JPanel();
    JPanel textPanel = new JPanel();

    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    textPanel.add(new NumberText());

    //Grid layout for buttons
    numPad.setLayout(new GridLayout(4, 3));

    //Number Buttons
    numPad.add(new Button1());
    numPad.add(new Button2());
    numPad.add(new Button3());
    numPad.add(new Button4());
    numPad.add(new Button5());
    numPad.add(new Button6());
    numPad.add(new Button7());
    numPad.add(new Button8());
    numPad.add(new Button9());
    numPad.add(new ButtonNeg());
    numPad.add(new Button0());
    numPad.add(new ButtonDec());

    primary.add(textPanel,BorderLayout.NORTH);
    primary.add(numPad, BorderLayout.SOUTH);

    frame.getContentPane().add(primary);
    frame.pack();
    frame.setVisible(true);
}
}

JLabel Class:

import javax.swing.*;

public class NumberText extends JLabel
{
private JLabel opNumLabel;
private String opNum;
private double storeNum = 0;
private boolean firstOp = true;

public void NumberText()
{
    opNum = "ur mom lol";
    opNumLabel = new JLabel (" " + opNum);
}

Upvotes: 0

Views: 59

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You've actually got two JLablels in your code, one that the NumberText class extends, that you add no text to, and that you're adding to the GUI, and one held within the same class that you add text to, but that you're not adding to the GUI.

Solution: Don't create a class to override JLabel -- there's no need to do this if you're not extending its behavior. Instead simply use a single JLabel.

Other issues:

  • You will want to get most of that code out of the main method and into a class proper where it belongs.
  • Use arrays or ArrayLists to simplify your coding and thus your debugging.
  • Do you really have a new class for each button? That's a debugging and maintenance nightmare that your really don't want. Instead will want to use JButtons and change their text and listeners.
  • This all suggests that you're over-using inheritance where you instead will want to favor composition in most cases.

Upvotes: 1

UDKOX
UDKOX

Reputation: 737

You extended a JLabel but instead of using the instance itself, you created another JLabel opNumLabel and used it. Try this way:

public class NumberText extends JLabel
{
    private String opNum;
    private double storeNum = 0;
    private boolean firstOp = true;

    public void NumberText()
    {
        super();
        opNum = "ur mom lol";
        this.setText(opNum);
    }
}

Also, you may want to think if you really need this NumberText instead of using a normal JLabel.

Upvotes: 0

Related Questions