Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -


calculator grid

  • Please log in to reply
2 replies to this topic

#1 VakhoQ


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 11 November 2011 - 10:53 PM

GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width. Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.

thats nice;

Posted Image

the code of my calculator;
/* ###########################
Code is Writen By Vakhtang koroghlishvili

############################ */

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

public class main {
public static void main(String[] args) {
new gui();

class gui extends JFrame{

public JTextField answer;

public JButton Butt0;

public JButton Butt1;

public JButton Butt2;

public JButton Butt3;

public JButton Butt4;

public JButton Butt5;

public JButton Butt6;

public JButton Butt7;

public JButton Butt8;

public JButton Butt9;

public JButton ButtGClean;

public JButton ButtGak;

public JButton ButtGamr;

public JButton ButtMin;

public JButton ButtEqual;

public JButton ButtPlus;

public gui(){


JFrame frame = new JFrame ("VakhoQ");


frame.setSize(190, 190);


Dimension frameSize =frame.getSize();

int FrameSizeX = frameSize.width;

int FrameSizeY = frameSize.height;

Dimension ScreenSize =Toolkit.getDefaultToolkit().getScreenSize();

int ScreenSizeX =ScreenSize.width;

int ScreenSizeY =ScreenSize.height;

frame.setLocation(ScreenSizeX/2-FrameSizeX/2, ScreenSizeY/2-FrameSizeY/2 );


Container pane = frame.getContentPane();

pane.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.fill= GridBagConstraints.HORIZONTAL;


gbc.gridx=0; gbc.gridy=0;

answer = new JTextField("0");

pane.add(answer, gbc);

Butt1 = new JButton("1");

Butt2 = new JButton("2");

Butt3 = new JButton("3");

Butt4 = new JButton("4");

Butt5 = new JButton("5");

Butt6 = new JButton("6");

Butt7 = new JButton("7");

Butt8 = new JButton("8");

Butt9 = new JButton("9");

Butt0 = new JButton("0");

ButtEqual = new JButton("=");

ButtPlus = new JButton("+");

ButtMin = new JButton("-");

ButtGamr = new JButton("*");

ButtGak = new JButton("/");

ButtGClean = new JButton("Clean Everything");


gbc.gridx=0; gbc.gridy=1;

pane.add(Butt1, gbc);

gbc.gridx=1; gbc.gridy=1;

pane.add(Butt2, gbc);

gbc.gridx=2; gbc.gridy=1;

pane.add(Butt3, gbc);

gbc.gridx=0; gbc.gridy=2;

pane.add(Butt4, gbc);

gbc.gridx=1; gbc.gridy=2;

pane.add(Butt5, gbc);

gbc.gridx=2; gbc.gridy=2;

pane.add(Butt6, gbc);

gbc.gridx=0; gbc.gridy=3;

pane.add(Butt7, gbc);

gbc.gridx=1; gbc.gridy=3;

pane.add(Butt8, gbc);

gbc.gridx=2; gbc.gridy=3;

pane.add(Butt9, gbc);

gbc.gridx=0; gbc.gridy=4;

pane.add(Butt0, gbc);


gbc.gridx=1; gbc.gridy=4;

pane.add(ButtEqual, gbc);


gbc.gridx=3; gbc.gridy=1;

pane.add(ButtPlus, gbc);

gbc.gridx=3; gbc.gridy=2;

pane.add(ButtMin, gbc);

gbc.gridx=3; gbc.gridy=3;

pane.add(ButtGamr, gbc);

gbc.gridx=3; gbc.gridy=4;

pane.add(ButtGak, gbc);

gbc.gridx=0; gbc.gridy=5;


pane.add(ButtGClean, gbc);




class algo extends gui{

public int keep;

public int value;

public String str;

public String toDo;

public algo(){





ActionListener l = new ActionListener()


public void actionPerformed(ActionEvent e)


if(e.getSource()==Butt1 || e.getSource()==Butt2 || e.getSource()==Butt3 || e.getSource()==Butt4 || e.getSource()==Butt5 || e.getSource()==Butt6 || e.getSource()==Butt6 || e.getSource()==Butt7 || e.getSource()==Butt8 || e.getSource()==Butt9 || e.getSource()==Butt0){

int x = Integer.parseInt(e.getActionCommand());



if(e.getSource()==ButtPlus || e.getSource()==ButtMin || e.getSource()==ButtGak || e.getSource()==ButtGamr){




if(toDo!=null) {





keep=0; value=0;

str=null; toDo=null;






















void Digit(int x){


String StrValue=Integer.toString(value);




void Add(String s)






keep = value;















void compute()



value = keep+value;




value = keep-value;




value = keep*value;




value = keep/value;





Explanations from dowunload.oracle.com:

gridx, gridy
Specify the row and column at the upper left of the component. The leftmost column has address gridx=0 and the top row has address gridy=0. Use GridBagConstraints.RELATIVE (the default value) to specify that the component be placed just to the right of (for gridx) or just below (for gridy) the component that was added to the container just before this component was added. We recommend specifying the gridx and gridy values for each component rather than just using GridBagConstraints.RELATIVE; this tends to result in more predictable layouts.
gridwidth, gridheight
Specify the number of columns (for gridwidth) or rows (for gridheight) in the component's display area. These constraints specify the number of cells the component uses, not the number of pixels it uses. The default value is 1. Use GridBagConstraints.REMAINDER to specify that the component be the last one in its row (for gridwidth) or column (for gridheight). Use GridBagConstraints.RELATIVE to specify that the component be the next to last one in its row (for gridwidth) or column (for gridheight). We recommend specifying the gridwidth and gridheight values for each component rather than just using GridBagConstraints.RELATIVE and GridBagConstraints.REMAINDER; this tends to result in more predictable layouts.
Note: GridBagLayout does not allow components to span multiple rows unless the component is in the leftmost column or you have specified positive gridx and gridy values for the component.

Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component. Valid values (defined as GridBagConstraints constants) include NONE (the default), HORIZONTAL (make the component wide enough to fill its display area horizontally, but do not change its height), VERTICAL (make the component tall enough to fill its display area vertically, but do not change its width), and BOTH (make the component fill its display area entirely).
ipadx, ipady
Specifies the internal padding: how much to add to the size of the component. The default value is zero. The width of the component will be at least its minimum width plus ipadx*2 pixels, since the padding applies to both sides of the component. Similarly, the height of the component will be at least its minimum height plus ipady*2 pixels.
Specifies the external padding of the component -- the minimum amount of space between the component and the edges of its display area. The value is specified as an Insets object. By default, each component has no external padding.
Used when the component is smaller than its display area to determine where (within the area) to place the component. Valid values (defined as GridBagConstraints constants) are CENTER (the default), PAGE_START, PAGE_END, LINE_START, LINE_END, FIRST_LINE_START, FIRST_LINE_END, LAST_LINE_END, and LAST_LINE_START.
Here is a picture of how these values are interpreted in a container that has the default, left-to-right component orientation.

Version note: The PAGE_* and *LINE_* constants were introduced in 1.4. Previous releases require values named after points of the compass. For example, NORTHEAST indicates the top-right part of the display area. We recommend that you use the new constants, instead, since they enable easier localization.

weightx, weighty
Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.

Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row.

see fulll tutorial , how tu use GridBagLayout inside out, on sun's official web page:
How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
  • 0
GNU/Linux Is the Best.

#2 wim DC

wim DC


  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 14 November 2011 - 07:00 AM

A few notes:
You make a lot of violations with the Java naming conventions, like starting classes with a small letter and variables with a capitalized one.
See --> Oracle JAVA naming conventions
You have quite some repetitive code, which in this case is hard to avoid, but you can reduce it.
gbc.gridwidth = 1;
gbc.gridx = 3;
gbc.gridy = 1;
frame.add(ButtPlus, gbc);
Make a method that takes 4 parameters, (width, x, y, object (Button) ), and do it in that method. So this:
         gbc.gridwidth = 1;
        gbc.gridx = 3;
        gbc.gridy = 1;
        frame.add(ButtPlus, gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        frame.add(ButtMin, gbc);

        gbc.gridx = 3;
        gbc.gridy = 3;
        frame.add(ButtGamr, gbc);
  addComponentToFrame(1,3,1 buttPlus);
  addComponentToFrame(1,3,2 buttMin);
  addComponentToFrame(1,3,3 buttGamr);


private void addComponentToFrame(int width, int x, int y, JComponent component){
  GridBagConstraints gbc = new GridBagConstraints();
  gbc.gridwidth = width;
  gbc.gridx = x;
  gbc.gridy = y;
  frame.add(component, gbc);

There's no point in extending JFrame if you then create a new JFrame in your class.. You basically create a JFrame in a JFrame.
To fix this, remove
JFrame frame = new JFrame("VakhoQ");
First line in the constructor becomes:
And everywhere you used "frame", use "this", or "super".

There might be more, but I'll check on that @ home later this day.
  • 0

#3 wim DC

wim DC


  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 14 November 2011 - 11:55 PM

I think I would probably end up with something like this after a refactor for the gui part - without the algorithm.
import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        new Gui();

class Gui extends JFrame {
    public static final int WIDTH = 190;
    public static final int HEIGHT = 190;
    public JTextField answer;
    public JButton[] numberButtons;
    public JButton buttGClean;
    public JButton buttGak;
    public JButton buttGamr;
    public JButton buttMin;
    public JButton buttEqual;
    public JButton buttPlus;

    public Gui() {
        super("Wim DC");



    private void setLook() {
        setSize(WIDTH, HEIGHT);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = screen.width;
        int screenHeight = screen.height;
        setLocation(screenWidth / 2 - WIDTH / 2, screenHeight / 2 - HEIGHT / 2);

        getContentPane().setLayout(new GridBagLayout());

    private void initComponents() {
        answer = new JTextField("0");
        buttEqual = new JButton("=");
        buttPlus = new JButton("+");
        buttMin = new JButton("-");
        buttGamr = new JButton("*");
        buttGak = new JButton("/");
        buttGClean = new JButton("Clean Everything");
        numberButtons = new JButton[10];

        for (int i = 0; i < numberButtons.length; i++) {
            numberButtons[i] = new JButton(i + "");

    private void addcomponents() {
        for (int y = 1; y < 4; y++) {
            for (int x = 0; x < 3; x++) {
                addComponentToFrame(1, x, y, GridBagConstraints.HORIZONTAL, numberButtons[3 * (y - 1) + x + 1]);

        addComponentToFrame(4, 0, 0, GridBagConstraints.HORIZONTAL, answer);
        addComponentToFrame(1, 3, 1, GridBagConstraints.HORIZONTAL, buttPlus);
        addComponentToFrame(1, 3, 2, GridBagConstraints.HORIZONTAL, buttMin);
        addComponentToFrame(1, 3, 3, GridBagConstraints.HORIZONTAL, buttGamr);
        addComponentToFrame(1, 0, 4, GridBagConstraints.HORIZONTAL, numberButtons[0]);
        addComponentToFrame(2, 1, 4, GridBagConstraints.HORIZONTAL, buttEqual);
        addComponentToFrame(1, 3, 4, GridBagConstraints.HORIZONTAL, buttGak);
        addComponentToFrame(4, 0, 5, GridBagConstraints.HORIZONTAL, buttGClean);

    private void addComponentToFrame(int width, int x, int y, int fill, JComponent component) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.gridwidth = width;
        gbc.gridx = x;
        gbc.gridy = y;
        add(component, gbc);

  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download