This is my first GUI attempt so please excuse it's messiness. This is an assignment using BorderLayout, GridLayout, FlowLayout, so we cannot cheat and use NetBeans or other builders, and I would prefer not to extend myself to using other Layouts such as SpringLayout, or NULL.
I need to set some spacing between components. The core Layout I'm using is BorderLayout which may/may not be correct. It's a Calculator supposed to look exactly the same as XP Calculator. This may just be a design problem but when I try to call any form of this. or BorderLayout. etc, I cannot seem to call setHgap() and setVgap(). I decided to split the top section (txt field and top 4 buttons) into 1 component, place this within BorderLayout.NORTH, the numbers and mathematical buttons in a GridLayout(4,5) and the MC, MR, MS etc buttons in a GridLayout(4,0) So can anyone tell me how I can set the spacing within the BorderLayout used by (this) ?? code is as follows.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener, KeyListener
{
private static final long serialVersionUID = 1L;
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
private JButton bDot, bPlus, bMinus, bMultiply, bDivide, bEquals;
private JButton bBackspace, bCe, bC, bSqrt, bPercent;
private JButton b1X, bPlusSlashMinus, bMC, bMR, bMS, bMPlus;
private JMenuBar menuBar;
private JMenu mEdit, mView, mHelp;
private JMenuItem menuItem;
private JRadioButtonMenuItem rbItem;
private JCheckBoxMenuItem cbItem;
private JTextField txtField;
private JPanel right_p, left_p, mid_p, comb_p;
private Insets ins;
private Dimension dims;
private Color color_red, color_blue;
public Calculator()
{
super("Calculator");
this.setSize(260,255);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setJMenuBar(createMenu());
this.setResizable(false);
this.setFont(new Font("Arial",Font.PLAIN, 2));
this.add(createCombinedTopField(), BorderLayout.NORTH);
this.add(createRightField(), BorderLayout.EAST);
this.add(createLeftField(), BorderLayout.CENTER);
// need to set the gap between components here.... somehow
}
public Component createCombinedTopField()
{
comb_p = new JPanel();
comb_p.setLayout(new BorderLayout());
comb_p.add(createTxtField(), BorderLayout.NORTH);
comb_p.add(createMidField(), BorderLayout.CENTER);
return comb_p;
}
public Component createTxtField()
{
//dims = new Dimension(1,1);
ins = new Insets(3,3,3,3);
txtField = new JTextField();
txtField.setMargin(ins);
//txtField.setMaximumSize(dims);
txtField.setBounds(5, 5, 240, 20);
txtField.setEditable(true);
txtField.setVisible(true);
txtField.setEnabled(true);
txtField.setHorizontalAlignment(JTextField.RIGHT);
return txtField;
}
public Component createRightField()
{
new JPanel();
right_p = new JPanel();
right_p.setLayout(new GridLayout(4,5,2,2));
color_red = new Color((float)1.0,(float)0.1,(float)0.1);
color_blue = new Color((float)0.1,(float)0.1,(float)1.0);
ins = new Insets(2,2,2,2);
dims = new Dimension(35,32);
b1 = new JButton("1");
b1.setForeground(color_blue);
b1.setPreferredSize(dims);
b1.setMargin(ins);
b2 = new JButton("2");
b2.setForeground(color_blue);
b2.setPreferredSize(dims);
b2.setMargin(ins);
b3 = new JButton("3");
b3.setForeground(color_blue);
b3.setPreferredSize(dims);
b3.setMargin(ins);
...
...
...
...
// first line
right_p.add(b7);
right_p.add(b8);
right_p.add(b9);
right_p.add(bDivide);
right_p.add(bSqrt);
// second line
right_p.add(b4);
right_p.add(b5);
right_p.add(b6);
right_p.add(bMultiply);
right_p.add(bPercent);
// third line
right_p.add(b1);
right_p.add(b2);
right_p.add(b3);
right_p.add(bMinus);
right_p.add(b1X);
// forth line
right_p.add(b0);
right_p.add(bPlusSlashMinus);
right_p.add(bDot);
right_p.add(bPlus);
right_p.add(bEquals);
return right_p;
}
public Component createLeftField()
{
left_p = new JPanel();
left_p.setLayout(new GridLayout(4,0,2,2));
ins = new Insets(0,0,0,0);
dims = new Dimension(35,32);
color_red = new Color((float)1.0,(float)0.1,(float)0.1);
left_p.setPreferredSize(dims);
left_p.setMaximumSize(dims);
bMC = new JButton("MC");
bMC.setForeground(color_red);
bMC.setPreferredSize(dims);
bMC.setMargin(ins);
bMR = new JButton("MR");
bMR.setForeground(color_red);
bMR.setPreferredSize(dims);
bMR.setMargin(ins);
bMS = new JButton("MS");
bMS.setForeground(color_red);
bMS.setPreferredSize(dims);
bMS.setMargin(ins);
bMPlus = new JButton("M+");
bMPlus.setForeground(color_red);
bMPlus.setPreferredSize(dims);
bMPlus.setMargin(ins);
left_p.add(bMC);
left_p.add(bMR);
left_p.add(bMS);
left_p.add(bMPlus);
return left_p;
}
public Component createMidField()
{
mid_p = new JPanel();
mid_p.setLayout(new FlowLayout());
ins = new Insets(5,5,5,5);
dims = new Dimension(65,30);
Dimension bev_dim = new Dimension(27,27);
color_red = new Color((float)1.0,(float)0.1,(float)0.1);
JPanel bevel_p = new JPanel();
bevel_p.setPreferredSize(bev_dim);
bevel_p.setBorder(BorderFactory.
createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
bBackspace = new JButton("Backspace");
bBackspace.setForeground(color_red);
bBackspace.setPreferredSize(dims);
bBackspace.setMargin(ins);
bC = new JButton(" C ");
bC.setForeground(color_red);
bC.setPreferredSize(dims);
bC.setMargin(ins);
bCe = new JButton(" CE ");
bCe.setForeground(color_red);
bCe.setPreferredSize(dims);
bCe.setMargin(ins);
mid_p.add(bevel_p);
mid_p.add(bBackspace);
mid_p.add(bCe);
mid_p.add(bC);
return mid_p;
}
public JMenuBar createMenu()
Edited by: Gcampton on Sep 16, 2010 6:35 PM
Edited by: Gcampton on Sep 16, 2010 7:00 PM
Here's what it currently looks like, you can see I need to apply some borders to the outside, and the bottom left buttons:
[http://i52.tinypic.com/2w7p993.jpg]