Unreliable Compiling of Very Simple Java Program in Netbeans
I'm a few days into learning Java and I am trying to build a simple
calculator.
When I run the code in Netbeans, perhaps 30% of the time it works great.
The window pops up with my buttons. The other 70% of the time just a blank
JFrame pops up.
In both circumstances Netbeans' Output tab is telling me BUILD SUCCESSFUL
(total time: N seconds)
My code is below - I'm certain it contains a untold numbers of problems
and errors, but specifically I'm trying to understand why the outcome of
executing it is so unreliable.
Thanks in advance,
Ben
import java.awt.Color;
import java.awt.PopupMenu;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Try1 extends JFrame {
public static void main(String[] args) {
//JFrame
JFrame frame = new JFrame();
frame.setSize(400,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Ben's Program");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable ( false );
//JPanel
JPanel panel = new JPanel();
panel.setSize(360,100);
panel.setLocation(20, 20);
panel.setBackground(Color.BLUE);
//JPanel 2
JPanel panel2 = new JPanel();
panel2.setSize(360,420);
panel2.setLocation(20, 140);
panel2.setBackground(Color.RED);
//Button
JButton but1= new JButton("1");
JButton but2= new JButton("2");
JButton but3= new JButton("3");
JButton but4= new JButton("4");
JButton but5= new JButton("5");
JButton but6= new JButton("6");
JButton but7= new JButton("7");
JButton but8= new JButton("8");
JButton but9= new JButton("9");
JButton but0= new JButton("0");
JButton butadd= new JButton("+");
JButton butsub= new JButton("-");
JButton butmulti= new JButton("*");
JButton butdiv= new JButton("/");
JButton buteq= new JButton("=");
JButton butclear= new JButton("C");
//Button Layout
but1.setBounds(10, 81, 89, 23);
but2.setBounds(126, 81, 89, 23);
but3.setBounds(225, 81, 89, 23);
but4.setBounds(10, 115, 89, 23);
but5.setBounds(126, 115, 89, 23);
but6.setBounds(225, 115, 89, 23);
but7.setBounds(10, 149, 89, 23);
but8.setBounds(126, 149, 89, 23);
but9.setBounds(225, 149, 89, 23);
but0.setBounds(126, 183, 89, 23);
butadd.setBounds(126, 215, 89, 23);
butsub.setBounds(10, 215, 89, 23);
butmulti.setBounds(225, 183, 89, 23);
butdiv.setBounds(225, 149, 89, 23);
buteq.setBounds(126, 183, 89, 23);
butclear.setBounds(10, 183, 89, 23);
//Add Panel to Frame
frame.add(panel);
frame.add(panel2);
//Add Buttons to Panel2
panel2.add(but1);
panel2.add(but2);
panel2.add(but3);
panel2.add(but4);
panel2.add(but5);
panel2.add(but6);
panel2.add(but7);
panel2.add(but8);
panel2.add(but9);
panel2.add(butadd);
panel2.add(butsub);
panel2.add(butmulti);
panel2.add(butdiv);
panel2.add(buteq);
panel2.add(butclear);
}
}
No comments:
Post a Comment