Code:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
public Calculator() {
this.initialize();
}
private void initialize() {
setTitle("Calculator");
JMenuBar menuBar = new JMenuBar();
JMenu menuA = new JMenu("menuA");
menuA.add(new JMenuItem("menuItemA"));
menuA.add(new JMenuItem("menuItemB"));
menuBar.add(menuA);
JMenu menuB = new JMenu("menuB");
menuB.add(new JRadioButtonMenuItem("menuItemA"));
menuB.add(new JRadioButtonMenuItem("menuItemB"));
menuB.addSeparator();
menuB.add(new JMenuItem("menuItemC"));
menuBar.add(menuB);
JMenu menuC = new JMenu("menuC");
menuC.add(new JMenuItem("menuItemA"));
menuC.addSeparator();
menuC.add(new JMenuItem("menuItemB"));
menuBar.add(menuC);
setJMenuBar(menuBar);
Container cPane = getContentPane();
cPane.setLayout(new GridBagLayout());
//Common layout constraints for all components.
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(2, 1, 2, 1);
c.weightx = .1;
//Add the display area.
JTextField display = new JTextField("0.");
display.setEditable(false);
// display.setEnabled(false);
display.setHorizontalAlignment(JTextField.RIGHT);
setConstraints(c, 0, 0, 6, 1);
cPane.add(display, c);
//Common only for the buttons.
c.weighty = .1;
//Add the first row of buttons.
setConstraints(c, 0, 1, 1, 1);
cPane.add(new JLabel(), c);
GridLayout layout = new GridLayout(0, 3);
layout.setHgap(2);
JPanel bcecPanel = new JPanel(layout);
bcecPanel.add(new JButton());
bcecPanel.add(new JButton());
bcecPanel.add(new JButton());
setConstraints(c, 1, 1, 5, 1);
cPane.add(bcecPanel, c);
//Add the second row of buttons.
setConstraints(c, 0, 2, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 1, 2, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 2, 2, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 3, 2, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 4, 2, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 5, 2, 1, 1);
cPane.add(new JButton(), c);
//Add the third row of buttons.
setConstraints(c, 0, 3, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 1, 3, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 2, 3, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 3, 3, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 4, 3, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 5, 3, 1, 1);
cPane.add(new JButton(), c);
//Add the fourth row of buttons.
setConstraints(c, 0, 4, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 1, 4, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 2, 4, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 3, 4, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 4, 4, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 5, 4, 1, 1);
cPane.add(new JButton(), c);
//Add the fifth row of buttons.
setConstraints(c, 0, 5, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 1, 5, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 2, 5, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 3, 5, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 4, 5, 1, 1);
cPane.add(new JButton(), c);
setConstraints(c, 5, 5, 1, 1);
cPane.add(new JButton(), c);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(280, 220);
setResizable(false);
setVisible(true);
}
/**
* Sets the layout constraints to the specified parameters.
*
* @param c - the layout constraints
* @param gridx - the desired x position for the component
* @param gridy - the desired y position for the component
* @param gridwidth - the column span of the component
* @param gridheight - the no. of row span of the component
*/
private void setConstraints(
GridBagConstraints c,
int gridx,
int gridy,
int gridwidth,
int gridheight) {
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
}
public static void main(String[] args) {
new Calculator();
}
}
try to read check these links sad bay.. makatabang ni..