Reputation: 15
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
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:
Upvotes: 1
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