Hot questions for Using Applets in layout manager

Top Java Programmings / Applets / layout manager


I am writing a program that is meant to use a BorderLayout with two button on the West and East side of the window. Somehow, there is a large gap in the center. Is there any way that I can eliminate this gap and have the two buttons be tangent with each other? Below I have attached my code. Any help is appreciated :).

import java.applet.*;
import java.awt.*;

public class HON extends Applet {

Button p1;
Button p2;
BorderLayout layout;

public void init() {
    layout = new BorderLayout();

    p1 = new Button("text");
    p2 = new Button("text");

    add(p1, BorderLayout.WEST);
    add(p2, BorderLayout.EAST);


public void stop() {



BorderLayout is doing exactly what the name implies - putting stuff on the border. This is the reason for the gap in the center. If you want to have something that has 2 buttons side by side, I would recommend the GridLayout for its simplicity. Code would go something like this:

GridLayout layout = new GridLayout(1,2); // Or (2,1), depending on how you want orientation
JPanel pane = new JPanel();
pane.add(leftButton); // Where leftButton is the JButton (or other swing component) on the left
pane.add(rightButton); // Same goes for the right JButton
// Then add your JPanel to the Frame and all that jazz below.

This should do what you want if I understand your question correctly. Notice also that I am using Swing components because they are still maintained by Java. Leave a comment/question if you need any other help with this.

EDIT: Notice in the comments that MadProgrammer suggested using the GridBagLayout. This is more powerful/versatile than plain vanilla GridLayout, but also is a bit harder to learn, so you can sort of take your pick as to which you want to do.


I need to create a java applet of a calculator with 2 bottoms on the top that take the whole row and under that a text area with a scroll pane and also below that 12 bottoms 4x4. Now I need to make it on an applet not on a frame which is where I am having trouble with because my GridBagLayout is not working and my buttons wont expand.

The applet I need to make.

Write the Java applet which implements a four operation calculator,The calculator will work as follows: 1. The scrollable text area will show each digit as entered. 2. Decimal numbers are allowed, and you must ensure that only one decimal symbol is entered for each number (you decide what the behaviour of the calculator should be if the user breaks this rule, but it must be controlled by your program) 3. When an operator (‘+’, ‘-‘, ‘/’, or ‘*’) is clicked, the operator should be displayed after the last digit entered, and subsequent numbers should be displayed on a new line 4. After entering a sequence of firstnumber operator secondnumber the user can click: a. ‘=’, in which case the calculator must display: i. the ‘=’ symbol after the last digit of the secondnumber ii. the result of the operation, on a new line iii. anything else entered after the ‘=’ symbol is part of a new calculation, and must be displayed on a separate line For example, the user clicks “123.45+456.2=1”. The screen should look like this: 123.45+ ← entered by user 456.2= ← entered by user 579.65  calculated & displayed by your program 1 ←beginning of a new calculation, entered by user b. any new operator, in which case the calculator must assume an implicit ‘=’ and: i. display an ‘=’ sign after the last number ii. display the result of the previous operation on a new line, followed by the new operator iii. treat the result as the first number of a new calculation, and display the new operator right after it iv. allow the user to enter a new number For example, the user clicks “123.45+456.2/5=”. The calculator interpretation is that the user wants the first two numbers added, and the sum divided by 5. The screen should look like this: 123.45+ ← entered by user 456.2= 456.2 entered by user, ‘=’ added by the program 579.65/ 579.65 calculated by program, ‘/’ entered by user 5= new calculation, entered by user 115.93 ←calculated by your program 5. When the user enters two or more consecutive operators, write an error message in the text area stating that operator ‘x’ has been ignored. The message should be on a separate line, and everything else following should be on a new line. 6. When the user clicks “Clear Last” the calculator should delete the last character displayed on the screen from the screen. Further behaviour of your calculator

    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.ScrollPane;
    import java.awt.TextArea;

    public class MarksProc extends Applet implements ActionListener{

      TextField t1, t2;
      Label l3,l4;
      JButton b1, b2, b3;
      String best ="";
      TextArea ta;
      ScrollPane scroll;
      Button b[]=new Button[16];
      String cmd[]={"+","-","*","/","=","C"};
      int sum=0, count=0, oldmark=0;
      public void init(){

        Panel p1 = new Panel(new GridBagLayout());
        p1.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        b1 = new JButton("Clear Last");
         c.fill = GridBagConstraints.BOTH;
         c.gridx = 0;
         c.gridy = 0;
         c.ipady = 40;
        // p1.add(b1,c);

         b2 = new JButton("Clear All");
         c.fill = GridBagConstraints.HORIZONTAL;
         c.weightx = 0.5;
         c.gridx = 1;
         c.gridy = 0; 
       // p1.add(b2,c);


        //add(b1, "North");

        Panel p2 = new Panel();
        p2.setLayout(new GridBagLayout());
        c.fill = GridBagConstraints.HORIZONTAL;
         c.weightx = 0.5;
         c.gridx = 0;
         c.gridy = 1; 
        ta = new TextArea();
       // scroll = new ScrollPane(ta);
       //  scroll.setBounds(10, 11, 455, 249);  
       // scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);




        Panel p3=new Panel();

        p3.setLayout(new GridLayout(4,4));

        for(int i=0;i<16;i++)



            b[i]=new Button(String.valueOf(i));


            b[i]=new Button(cmd[i%10]);

          b[i].setFont(new Font("Arial",Font.BOLD,80));



         // setSize(800, 900);


      public void actionPerformed(ActionEvent e){


Note: I factored out adding the component, to a separate method partly to enforce specifying each of the layout constraints that might change.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class CalculatorWithGBL {

    CalculatorWithGBL() {

    public void initUI() {
        if (ui != null) {

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH; // fill cell
        gbc.weightx = .5; //expand to fill container size
        gbc.weighty = .5; //  "    "   "      "       "   

        addComponentToContainer(new JButton("Clear Last"), 0, 0, 2);
        addComponentToContainer(new JButton("Clear All"), 2, 0, 2);

        addComponentToContainer(new JScrollPane(new JTextArea(4, 20)), 0, 1, 4);

        addComponentToContainer(new JButton("7"), 0, 2, 1);
        addComponentToContainer(new JButton("8"), 1, 2, 1);
        addComponentToContainer(new JButton("9"), 2, 2, 1);
        addComponentToContainer(new JButton("+"), 3, 2, 1);

        addComponentToContainer(new JButton("4"), 0, 3, 1);
        addComponentToContainer(new JButton("5"), 1, 3, 1);
        addComponentToContainer(new JButton("6"), 2, 3, 1);
        addComponentToContainer(new JButton("-"), 3, 3, 1);

        addComponentToContainer(new JButton("1"), 0, 4, 1);
        addComponentToContainer(new JButton("2"), 1, 4, 1);
        addComponentToContainer(new JButton("3"), 2, 4, 1);
        addComponentToContainer(new JButton("*"), 3, 4, 1);

        addComponentToContainer(new JButton("0"), 0, 5, 1);
        addComponentToContainer(new JButton("."), 1, 5, 1);
        addComponentToContainer(new JButton("="), 2, 5, 1);
        addComponentToContainer(new JButton("/"), 3, 5, 1);

    private final void addComponentToContainer(
            Component c, int gridx, int gridy, int gridwidth) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;

        ui.add(c, gbc);

    public JComponent getUI() {
        return ui;

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                try {
                } catch (Exception useDefault) {
                CalculatorWithGBL o = new CalculatorWithGBL();

                JFrame f = new JFrame("Calculator with GBL");



    private JComponent ui = null;
    private GridBagConstraints gbc = new GridBagConstraints();