4

This may be a silly question, and one that can be resolved by changing the Layout I'm using for this, but, when running my calculator program, I'm savvy up until I see that my text area size wont stretch to fit the size of the pane it's in automatically without me setting the size when first creating it, and also, how do I go about eliminating this monstrous gap that BorderLayout.CENTER is taking up between my button panels?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class calculatorProg extends JFrame implements ActionListener
{
    private Container contents;
    private JButton add, subtr, mult, div, pow, pow10, b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
    private JTextArea win;
    private JPanel numArea, win1, arithArea, numPlusArith;
    private JMenu men;
    private JMenuItem menItem;
    JSplitPane calcPane;

    public calculatorProg()
    {
        super("Calculator");
        contents = getContentPane();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        contents.setLayout(new BorderLayout());

        //Arithmetic buttons
        add = new JButton("+");
        subtr = new JButton("-");
        mult = new JButton("x");
        div = new JButton("/");
        pow = new JButton("x^");
        pow10 = new JButton("x^10");

        //Buttons
        b1 = new JButton("1");
        b5 = new JButton("5");
        b2 = new JButton("2");
        b6 = new JButton("6");
        b3 = new JButton("3");
        b7 = new JButton("7");
        b4 = new JButton("4");
        b8 = new JButton("8");
        b9 = new JButton("9");
        b0 = new JButton("0");

        //Creating the panels
        win = new JTextArea(2, 25);
        win.setEditable(false);
        win.setVisible(true);
        win1 = new JPanel();
        win1.setLayout(new FlowLayout(FlowLayout.LEFT));
        win1.add(win);
        numArea = new JPanel(new GridLayout(0, 3));
        arithArea = new JPanel(new GridLayout(3, 3));
        arithArea.setSize(new Dimension(8, 8));
        numPlusArith = new JPanel(new BorderLayout());

        //Adding to num area
        numArea.add(b1);
        numArea.add(b2);
        numArea.add(b3);
        numArea.add(b4);
        numArea.add(b5);
        numArea.add(b6);
        numArea.add(b7);
        numArea.add(b8);
        numArea.add(b9);
        numArea.add(b0);
        arithArea.add(add);
        arithArea.add(subtr);
        arithArea.add(div);
        arithArea.add(mult);
        arithArea.add(pow);
        arithArea.add(pow10);
        numPlusArith.add(arithArea, BorderLayout.LINE_START);
        numPlusArith.add(numArea, BorderLayout.LINE_END);

        //Lay out the panes
        calcPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, win1, numPlusArith);
        calcPane.setOneTouchExpandable(true);
        calcPane.setDividerLocation(50);

        Dimension minimumSize = new Dimension(100, 50);
        win1.setMinimumSize(minimumSize);
        numArea.setMinimumSize(minimumSize);

        //add to contents
        contents.add(calcPane, BorderLayout.CENTER);

        setSize(320, 370);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        calculatorProg cp = new calculatorProg();
    }
}

Someone may make mention about the 0 being off center in the numArea (I read in the Java docs about GridBagLayout but haven't even had half my toenail into those waters, yet.)

EDIT CODE:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class calculatorProg extends JFrame implements ActionListener {
private Container contents;
private JButton add, subtr, mult, div, pow, pow10, clr, plmi, b1, b2, b3, b4, b5,   b6, b7, b8, b9, b0;
private JTextArea win;
private JPanel numArea, win1, arithArea, numPlusArith;
private JMenu men;
private JMenuItem menItem;
JSplitPane calcPane;

public calculatorProg(){
    super("Calculator");
contents = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
contents.setLayout(new BorderLayout());

//Arithmetic buttons
add = new JButton("+");
subtr = new JButton("-");
mult = new JButton("x");
div = new JButton("/");
pow = new JButton("x^");
pow10 = new JButton("x^10");


//Buttons
b1 = new JButton("1"); b5 = new JButton("5");
b2 = new JButton("2"); b6 = new JButton("6");   
b3 = new JButton("3"); b7 = new JButton("7");
b4 = new JButton("4"); b8 = new JButton("8");
b9 = new JButton("9"); b0 = new JButton("0");
clr = new JButton("C"); plmi = new JButton("+/-");


//Creating the panels
win = new JTextArea(2, 25);
win.setLineWrap(true);
win.setWrapStyleWord(true);
win.setEditable(false);
win.setVisible(true);
win1 = new JPanel();
win1.setLayout(new BorderLayout());
win1.add(new JScrollPane(win), BorderLayout.CENTER);
numArea = new JPanel(new GridLayout(0,3));
arithArea = new JPanel(new GridLayout(3, 3));
//arithArea.setSize(new Dimension(8, 8));
numPlusArith = new JPanel(new GridLayout(0, 2, 0, 0));

//Adding to num area
numArea.add(b1); numArea.add(b2);
numArea.add(b3); numArea.add(b4);
numArea.add(b5); numArea.add(b6);
numArea.add(b7); numArea.add(b8);
numArea.add(b9); numArea.add(b0); 
numArea.add(clr); numArea.add(plmi);
arithArea.add(add);
arithArea.add(subtr);
arithArea.add(div);
arithArea.add(mult);
arithArea.add(pow);
arithArea.add(pow10);
numPlusArith.add(arithArea);
numPlusArith.add(numArea);

//Lay out the panes
calcPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, win1, numPlusArith);
calcPane.setOneTouchExpandable(true);
calcPane.setDividerLocation(50);

Dimension minimumSize = new Dimension(100, 50);
win1.setMinimumSize(minimumSize);
numArea.setMinimumSize(minimumSize);

//add to contents
contents.add(calcPane, BorderLayout.CENTER);

pack();
setVisible(true);

}
public static void main(String[] args) {
    calculatorProg cp = new calculatorProg();

}

private double addition(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a + b;

    return finAnswer;
}
private double subtraction(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a + b;

    return finAnswer;
}
private double division(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a / b;

    return finAnswer;
}
private double multiplication(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a * b;
    return finAnswer;
}
private double pow(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = Math.pow(a, b);
    return finAnswer;
}
private double pow10(double a){
    a = 0.0;
    double finAnswer = Math.pow(a, 10);

    return finAnswer;
}
@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

}

As you can see I made the much needed changes. It looks a lot cleaner. There's still a minor gap around the numbers area, but nonetheless it's cleaner. pack() definitely makes a difference. Thanks for all the help guys!

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 1
    You should not create classes with names starting with small letters. You can for example, create the numeric buttons in a loop. Never use `Container` class directly but use `JPanel` instead. Never set the size of components directly but use `pack` method on them. For the layout, create a `GridLayout` more info here at: http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html - And finally an example Calculator tutorial: http://java.about.com/od/Handling-Events/ss/Simple-Calculator-Example-Program.htm – Sri Harsha Chilakapati Sep 15 '13 at 07:53
  • 1
    look at this, http://stackoverflow.com/questions/12835198/positioning-of-components-how-to-place-a-few-buttons-center-screen-same-size/12837118#12837118 I suggest you to try also a `BoxLayout` it's one of the Layout I like most. Look also at: http://stackoverflow.com/questions/12774432/jframe-not-the-right-size-when-its-executed-any-reason-why/12780478#12780478 for an argument on `pack()` – Gianmarco Sep 15 '13 at 08:44
  • More on this "Never use Container class directly but use JPanel instead". That confused me, though I knew, at least a part of me knew, I was messing up using container to store things. Let me know please and thanks for all your help. If I could thumbs up you I'd do it! – jeffrey zachary Sep 15 '13 at 17:48
  • @SriHarshaChilakapati : Also, yes I tried creating the buttons in a loop, but each time it would just throw an error. Messed with it for hours. `JButton[] nums = new JButton[10];` then `for(int i = 0; i < nums.length; i++){ nums[i] = new JButton("" + i); numArea.add(nums[i]);}` was what I used. – jeffrey zachary Sep 15 '13 at 18:39

1 Answers1

1

Thanks for the SSCCE, +1 for that. All that's missing is the actionPerformed method (you were also missing the last } until the recent edit).

In any case, your problem is that you're using FlowLayout for the panel that contains your JTextArea. Change these lines:

win1.setLayout(new FlowLayout(FlowLayout.LEFT));
win1.add(win);

to this:

win1.setLayout(new BorderLayout());
win1.add(new JScrollPane(win), BorderLayout.CENTER);

You may also notice that I wrapped the text area in a scroll pane. This will make it so that any text overflow is scrollable. You should also add in the following lines to turn on line-wrapping:

win = new JTextArea(2, 25);
win.setLineWrap(true);
win.setWrapStyleWord(true);
Brian
  • 17,079
  • 6
  • 43
  • 66
  • Thank you for that information on the text field. With the comments on my post and the information on this, I'll soon learn how to fix this big gap in my calculator. And after I fix the beauty of the program I'll work on the functioning of it. Thank you so much for your help, and all who have answered above me! – jeffrey zachary Sep 15 '13 at 17:46