0

The aim was to arrange components so as to resemble a standard calculator setup. I put a border layout with two panels, so that one panel consists of a long text box and the another one contains buttons in the grid form. Yet the output is a simple grid layout with the textbox followed by all the buttons composing of grid. Is it due to odd no. of buttons? or is it about insufficient parameters?

public class Calc extends Applet {

    Button add, sub, mul, div, sin, cos, tan, eq, cl;
    Button bt[] = new Button[10];
    TextField t1;
    String s, s1, num1, num2, s3, s4;
    int a, b, c;
    double d;

    public void init() {
        Panel p = new Panel();
        Panel e = new Panel();
        setLayout(new BorderLayout());
        t1 = new TextField(10);
        e.add(t1);
        e.setLayout(new GridLayout(1, 1));
        add("North", e);
        for (int i = 0; i < 10; i++) {
            bt[i] = new Button(String.valueOf(i));
            p.add(bt[i]);
            bt[i].addActionListener(this);
        }
        add = new Button("ADD");
        sub = new Button("SUB");
        mul = new Button("MUL");
        div = new Button("DIV");
        sin = new Button("SIN");
        cos = new Button("COS");
        tan = new Button("TAN");
        eq = new Button("EQU");
        cl = new Button("CLR");
        p.add(add);
        p.add(sub);
        p.add(mul);
        p.add(div);
        p.add(sin);
        p.add(cos);
        p.add(tan);
        p.add(eq);
        p.add(cl);
        p.add(t1);
        p.setLayout(new GridLayout(4, 5));
        add("South", p);
    }
}

I am unable to figure out where is it going wrong. Please suggest.

sprinter
  • 27,148
  • 6
  • 47
  • 78
Henodi
  • 75
  • 1
  • 1
  • 10
  • **The text field is being added to *both* internal panels.** That is the core of the observed problem, but the code has many ways in which it could be improved. To know the best way to improve one part of the code I need to know:- should the text field fill the entire width of the upper part of the GUI, or be the width specified by the number of columns? – Andrew Thompson Sep 16 '17 at 02:57
  • General tips/questions: 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). 3) Why use AWT? .. – Andrew Thompson Sep 16 '17 at 02:58
  • .. See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 4) Don't use 'magic numbers' like `"North"` to add components, there are constants defined for these constraints. The validity of those constants can be checked by the compiler. – Andrew Thompson Sep 16 '17 at 02:59
  • @AndrewThompson Actually I am a begginer in java, so this assignment was given to check how much I can do with applets. So it is mandatory to use it. – Henodi Sep 24 '17 at 08:06
  • And about filling the entire width with the field or specifying fixed col. length, either of them will do. I tried replacing "North" by predefined constants like BorderLayout.PAGE_START but it didn't help much. – Henodi Sep 24 '17 at 08:12
  • 1) *"So it is mandatory to use it."* So refer the instructor to that page I linked. 2) *"I tried replacing "North" by predefined constants like BorderLayout.PAGE_START but it didn't help much."* It helps with compile time checking. 3) So [this layout](https://i.stack.imgur.com/eT0gb.png) would be acceptable? 4) See also [this layout](http://stackoverflow.com/a/7441804/418556) (which uses Swing and is not an applet, but the layout principles are the important part). – Andrew Thompson Sep 24 '17 at 09:08
  • The first layout (one that uses applet) is exactly what I want it to look like. Please suggest the changes I should make to get this layout. – Henodi Sep 24 '17 at 16:49

0 Answers0