Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
173 views3 pages

Design Calculator Using GridBagLayout.

This Java code defines a CalculatorWithGBL class that uses a GridBagLayout to create a graphical user interface (GUI) for a calculator application. It initializes the UI by adding buttons for numbers, operators, and functions to a JPanel container using a GridBagConstraints object to position the components in a grid. The main method sets up and displays the calculator UI on a JFrame.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views3 pages

Design Calculator Using GridBagLayout.

This Java code defines a CalculatorWithGBL class that uses a GridBagLayout to create a graphical user interface (GUI) for a calculator application. It initializes the UI by adding buttons for numbers, operators, and functions to a JPanel container using a GridBagConstraints object to position the components in a grid. The main method sets up and displays the calculator UI on a JFrame.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.awt.

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

public class CalculatorWithGBL {

CalculatorWithGBL() {
initUI();
}

public void initUI() {


if (ui != null) {
return;
}

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() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
CalculatorWithGBL o = new CalculatorWithGBL();

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


f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}

private JComponent ui = null;


private GridBagConstraints gbc = new GridBagConstraints();
}
Output:

You might also like