swing - GUI based java calculator: Adding window to a container exception -
swing - GUI based java calculator: Adding window to a container exception -
this question has reply here:
exception : adding window container how solve it? 3 answersi'm working on java swing gui-based calculator. i'm having problem running code test did far. when compile it, returns next exception.
exception in thread "main" java.lang.illegalargumentexception: adding window container @ java.awt.container.checknotawindow(container.java:429) @ java.awt.container.addimpl(container.java:1037) @ javax.swing.jlayeredpane.addimpl(jlayeredpane.java:212) @ java.awt.container.add(container.java:925) @ javax.swing.jrootpane.setcontentpane(jrootpane.java:608) @ javax.swing.jframe.setcontentpane(jframe.java:671) @ calculator_project.calculator_project.main(calculator_project.java:184) java result: 1 build successful (total time: 4 seconds)
here's code:
package calculator_project; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class calculator_project extends jframe { private jbutton add, subtract, multiply, divide, left_paren, right_paren, zero, one, two, three, four, five, six,seven, eight, nine; private jtextfield textfield; public calculator_project(){ jpanel panel = new jpanel(); gridbaglayout layout = new gridbaglayout(); gridbagconstraints constraint = new gridbagconstraints(); panel.setlayout(layout); textfield = new jtextfield(10); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 0; constraint.gridy = 0; panel.add(textfield); add together = new jbutton("+"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 3; constraint.gridy = 1; panel.add(add,constraint); subtract = new jbutton("-"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 3; constraint.gridy = 2; panel.add(subtract,constraint); multiply = new jbutton("*"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 3; constraint.gridy = 3; panel.add(multiply,constraint); split = new jbutton("/"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 3; constraint.gridy = 4; panel.add(divide,constraint); left_paren = new jbutton("("); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 0; constraint.gridy = 4; panel.add(left_paren,constraint); right_paren = new jbutton(")"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 2; constraint.gridy = 4; panel.add(right_paren,constraint); 0 = new jbutton("0"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 1; constraint.gridy = 4; panel.add(zero,constraint); 1 = new jbutton("1"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 0; constraint.gridy = 3; panel.add(one,constraint); 2 = new jbutton("2"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 1; constraint.gridy = 3; panel.add(two,constraint); 3 = new jbutton("3"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 2; constraint.gridy = 3; panel.add(three,constraint); 4 = new jbutton("4"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 0; constraint.gridy = 2; panel.add(four,constraint); 5 = new jbutton("5"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 1; constraint.gridy = 2; panel.add(five,constraint); 6 = new jbutton("6"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 2; constraint.gridy = 2; panel.add(six,constraint); 7 = new jbutton("7"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 0; constraint.gridy = 1; panel.add(seven,constraint); 8 = new jbutton("8"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 1; constraint.gridy = 1; panel.add(eight,constraint); 9 = new jbutton("9"); constraint.fill= gridbagconstraints.horizontal; constraint.gridx = 2; constraint.gridy = 1; panel.add(nine,constraint); event e = new event(); add.addactionlistener(e); subtract.addactionlistener(e); multiply.addactionlistener(e); divide.addactionlistener(e); left_paren.addactionlistener(e); right_paren.addactionlistener(e); zero.addactionlistener(e); one.addactionlistener(e); two.addactionlistener(e); three.addactionlistener(e); four.addactionlistener(e); five.addactionlistener(e); six.addactionlistener(e); seven.addactionlistener(e); eight.addactionlistener(e); nine.addactionlistener(e); } public class event implements actionlistener { public void actionperformed (actionevent e){ double number; try{ number= double.parsedouble(textfield.gettext()); } grab (numberformatexception ee){ textfield.settext("illegal entry!"); textfield.setforeground(color.red); return; } } } public static void main(string[] args) { jframe frame = new jframe("calculator"); calculator_project panel = new calculator_project(); frame.setcontentpane(panel); frame.setsize(300,150); frame.setvisible(true); } }
how resolve it?
you trying add together jframe in jframe , that's why seeing error.
calculator_project panel = new calculator_project(); frame.setcontentpane(panel);
either calculator_project
class should extend jpanel
. , can add together jframe
. or straight create calculator_project
class' instance visible.
java swing jframe
Comments
Post a Comment