Reputation: 1
I am trying to design a calculator shell (non-functioning calculator) using Java but for some reason I cannot get the program to display my buttons. What am I doing wrong here?
import java.awt.*;
import javax.swing.*;
import java.awt.color.*;
public class Calculator extends JFrame {
public Calculator() {
JPanel P1 = new JPanel();
P1.setLayout(new GridLayout(4, 4));
//Panel Buttons
for (int i = 1; i <= 9; i++) {
P1.add(new JButton("" + i));
}
P1.add(new JButton("" + 0));
P1.add(new JButton("."));
P1.add(new JButton("*"));
P1.add(new JButton("/"));
P1.add(new JButton("+"));
P1.add(new JButton("-"));
P1.add(new JButton("="));
P1.setBackground(Color.cyan);
P1.setForeground(new Color(100, 1, 1));
//Content panel
JPanel P2 = new JPanel();
P2.setLayout(new BorderLayout());
P2.add(new JTextField("Hello world"), BorderLayout.NORTH);
P2.add(P1, BorderLayout.CENTER);
}
public static void main(String[] args) {
Calculator frame = new Calculator();
frame.setTitle("Simple Calculator");
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
OK here is what I have now...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
Calculator()
{
super("Wk 3 Calculator"); setBounds(100,100,300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(p1);
con.add(p2);
p1.setLayout(new GridLayout(4, 4));
//Panel Buttons
for (int i =1; i <=9; i++){
p1.add(new JButton ("" + i));
}
p1.add(new JButton (""+0));
p1.add(new JButton ("."));
p1.add(new JButton ("*"));
p1.add(new JButton ("/"));
p1.add(new JButton ("+"));
p1.add(new JButton ("-"));
p1.add(new JButton ("="));
//Content panel
p2.setLayout(new BorderLayout());
p2.add (new JTextField("Hello world"),BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
//Frame specs
Calculator frame = new Calculator();
frame.setSize(250,250);
frame.setTitle("Simple Calculator");
frame.add(p1, BorderLayout.NORTH);
frame.add(p2, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){new Calculator();}
}
It is still not working :(
Upvotes: 0
Views: 2667
Reputation: 10920
Well:
1). Call Swing-related code on the EDT (Event Dispatch Thread), not on the main thread. See here for more details.
2). You're not adding the P1
and P2
panels to the JFrame
, so when you display the frame, it doesn't contain anything. Try calling add (P1)
and add (P2)
.
3). You should call frame.pack()
before you call frame.setVisible (true)
.
EDIT:
I rewrote your code (you still have a loong way into making a functional application, but it's a start):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
private JFrame frame;
public Calculator()
{
frame = new JFrame ("Simple Calculator");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout (new BorderLayout ());
JPanel panel = new JPanel(new GridLayout(4, 4));
JPanel p2 = new JPanel(new BorderLayout());
for (int i = 1; i <= 9; i++)
{
panel.add (new JButton ("" + i));
}
panel.add(new JButton (""+0));
panel.add(new JButton ("."));
panel.add(new JButton ("*"));
panel.add(new JButton ("/"));
panel.add(new JButton ("+"));
panel.add(new JButton ("-"));
panel.add(new JButton ("="));
frame.add(panel, BorderLayout.CENTER);
frame.add (new JTextField("Hello world"), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
new Calculator();
}
});
}
}
Upvotes: 0
Reputation: 5374
You are not adding the panels to the Frame. Put add(p1, BorderLayout.NORTH)
and add(p2, BorderLayout.SOUTH)
at the end of your constructor.
public Calculator()
{
//rest of the construtor
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
}
And rename them so they start with a small letter. Capital letters are for class names.
Upvotes: 1
Reputation: 347332
You're not adding anything to the frame...
Try something along the lines of
add(P1, BorderLayout.NORTH);
add(P2, BorderLayout.SOUTH);
For starters...
You might like to have a look at
Upvotes: 2