I am tryin to figure out how to use these stupid JButtons, sorry for calling them stupid if I offend anyone ;-). I can not find any nice clean small examples. I have copy pasted my code below. What I am tryin to do is set up an ATM program..For the life of me I can not understand my teacher or the overwhelming examples I have found on the internet. Can someone PLEASE give me an example of associating buttons with functions when they're pressed using the GridBagLayout manager. Or if you have time could you throw in the code below post it with the additions on how to do it? I'm completely stuck right now. I only need examples with a couple of buttons on my layout if you don't mind. I have no way to pay the person that can help me but I'll be really grateful. thanks
// file name -- GridBag2.java
// type -- window application
// classes used -- JFrame, JButton, JLabel, JTextField
// JPasswordField, GridBag2, GridBag2GUI
// This program demonstrates how to use GridBag layout manager.
// The program consists of three classes:
// GridBag2 -- frame
// GridBag2GUI - graphical user interface
import javax.swing.*;
import java.awt.*;
public class GridBag2 extends JFrame
{
private GridBag2GUI gui;
public static void main(String args[])
{
GridBag2 window;
window = new GridBag2("Demonstration of GridBag Layout"
window.setSize(400, 400);
window.setVisible(true);
} // method main
// ==============================================================
// purpose -- initialize the GUI components and listeners
// preconditions -- none
// postconditions -- the GUI components and listeners are
// instantiated
// ==============================================================
public GridBag2(String heading)
{
super(heading);
gui = new GridBag2GUI(this);
gui.init();
} // constructor
} // class GridBag2
class GridBag2GUI
{
// GUI components
private GridBag2 frame;
private JLabel name_label;
private JTextArea name_field;
private JLabel password_label;
private JPasswordField password_field;
private JButton a,b,c,d,e,f,g,h,i,j,k,l;
private Container container;
// layout manager and constraint
private GridBagLayout layout;
private GridBagConstraints constraints;
// associate each component with a listener
// private GridBag2Listener done_listener;
// ==============================================================
// purpose -- Instantiate listeners and associate with frame
// preconditions -- frame has been instantiated
// postconditions -- frame is associated with this GUI object
// and listeners are instantiated
// ==============================================================
public GridBag2GUI(GridBag2 frame)
{
// instantiate GUI components
this.frame = frame;
name_label = new JLabel("User Name:"
name_field = new JTextArea(7,10);
password_label = new JLabel("Password:"
password_field = new JPasswordField(20);
a = new JButton("1"
b = new JButton("2"
c = new JButton("3"
d = new JButton("4"
e = new JButton("5"
f = new JButton("6"
g = new JButton("7"
h = new JButton("8"
i = new JButton("9"
j = new JButton("Y"
k = new JButton("0"
l = new JButton("N"
// obtain a content pane
container = frame.getContentPane();
} // constructor
// ==============================================================
// purpose -- Instantiate GUI components and add them to the
// frame
// preconditions -- listener has been instantiated
// postconditions -- GUI components are instantiated and added
// to the frame
// ==============================================================
public void init()
{
// instantiate layout manager and constraints
layout = new GridBagLayout();
constraints = new GridBagConstraints();
container.setLayout(layout);
// OK button
addConstraints(constraints, 0, 0, 1, 1, 0, 0);
layout.setConstraints(a, constraints);
container.add(a);
addConstraints(constraints, 1, 0, 1, 1, 0, 0);
layout.setConstraints(b, constraints);
container.add(b);
addConstraints(constraints, 2, 0, 1, 1, 0, 0);
layout.setConstraints(c, constraints);
container.add(c);
addConstraints(constraints, 0, 1, 1, 1, 0, 0);
layout.setConstraints(d, constraints);
container.add(d);
addConstraints(constraints, 1, 1, 1, 1, 0, 0);
layout.setConstraints(e, constraints);
container.add(e);
addConstraints(constraints, 2, 1, 1, 1, 0, 0);
layout.setConstraints(f, constraints);
container.add(f);
addConstraints(constraints, 0, 2, 1, 1, 0, 0);
layout.setConstraints(g, constraints);
container.add(g);
addConstraints(constraints, 1, 2, 1, 1, 0, 0);
layout.setConstraints(h, constraints);
container.add(h);
addConstraints(constraints, 2, 2, 1, 1, 0, 0);
layout.setConstraints(i, constraints);
container.add(i);
addConstraints(constraints, 0, 3, 1, 1, 0, 0);
layout.setConstraints(j, constraints);
container.add(j);
addConstraints(constraints, 1, 3, 1, 1, 0, 0);
layout.setConstraints(k, constraints);
container.add(k);
addConstraints(constraints, 2, 3, 1, 1, 0, 0);
layout.setConstraints(l, constraints);
container.add(l);
addConstraints(constraints, 3, 0, 6, 6, 0, 0);
layout.setConstraints(name_field, constraints);
container.add(name_field);
/*
// Name field
// Name Label
addConstraints(constraints, 0, 1, 1, 1, 10, 100);
layout.setConstraints(name_label, constraints);
container.add(name_label);
// Name field
addConstraints(constraints, 1, 1, 1, 0, 90, 100);
layout.setConstraints(name_field, constraints);
container.add(name_field);
// password Label
addConstraints(constraints, 2, 1, 1, 1, 0, 100);
layout.setConstraints(password_label, constraints);
container.add(password_label);
// password field
addConstraints(constraints, 3, 1, 1, 1, 0, 100);
layout.setConstraints(password_field, constraints);
container.add(password_field);
*/
} // method init
// ==============================================================
// purpose -- Place the component in a specific cell, set up
// its width and height, and the weighting ratios
// preconditions -- values for cell placement, size and ratios
// are properly passed to the method
// postconditions -- the constraints of cell placement, size
// and ratios of the component are updated
// ==============================================================
public void addConstraints(GridBagConstraints constraints,
int gridx,
int gridy,
int gridwidth,
int gridheight,
int weightx,
int weighty)
{
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.weightx = weightx;
constraints.weighty = weighty;
} // method addConstraints
} // class GridBag2GUI
// file name -- GridBag2.java
// type -- window application
// classes used -- JFrame, JButton, JLabel, JTextField
// JPasswordField, GridBag2, GridBag2GUI
// This program demonstrates how to use GridBag layout manager.
// The program consists of three classes:
// GridBag2 -- frame
// GridBag2GUI - graphical user interface
import javax.swing.*;
import java.awt.*;
public class GridBag2 extends JFrame
{
private GridBag2GUI gui;
public static void main(String args[])
{
GridBag2 window;
window = new GridBag2("Demonstration of GridBag Layout"
window.setSize(400, 400);
window.setVisible(true);
} // method main
// ==============================================================
// purpose -- initialize the GUI components and listeners
// preconditions -- none
// postconditions -- the GUI components and listeners are
// instantiated
// ==============================================================
public GridBag2(String heading)
{
super(heading);
gui = new GridBag2GUI(this);
gui.init();
} // constructor
} // class GridBag2
class GridBag2GUI
{
// GUI components
private GridBag2 frame;
private JLabel name_label;
private JTextArea name_field;
private JLabel password_label;
private JPasswordField password_field;
private JButton a,b,c,d,e,f,g,h,i,j,k,l;
private Container container;
// layout manager and constraint
private GridBagLayout layout;
private GridBagConstraints constraints;
// associate each component with a listener
// private GridBag2Listener done_listener;
// ==============================================================
// purpose -- Instantiate listeners and associate with frame
// preconditions -- frame has been instantiated
// postconditions -- frame is associated with this GUI object
// and listeners are instantiated
// ==============================================================
public GridBag2GUI(GridBag2 frame)
{
// instantiate GUI components
this.frame = frame;
name_label = new JLabel("User Name:"
name_field = new JTextArea(7,10);
password_label = new JLabel("Password:"
password_field = new JPasswordField(20);
a = new JButton("1"
b = new JButton("2"
c = new JButton("3"
d = new JButton("4"
e = new JButton("5"
f = new JButton("6"
g = new JButton("7"
h = new JButton("8"
i = new JButton("9"
j = new JButton("Y"
k = new JButton("0"
l = new JButton("N"
// obtain a content pane
container = frame.getContentPane();
} // constructor
// ==============================================================
// purpose -- Instantiate GUI components and add them to the
// frame
// preconditions -- listener has been instantiated
// postconditions -- GUI components are instantiated and added
// to the frame
// ==============================================================
public void init()
{
// instantiate layout manager and constraints
layout = new GridBagLayout();
constraints = new GridBagConstraints();
container.setLayout(layout);
// OK button
addConstraints(constraints, 0, 0, 1, 1, 0, 0);
layout.setConstraints(a, constraints);
container.add(a);
addConstraints(constraints, 1, 0, 1, 1, 0, 0);
layout.setConstraints(b, constraints);
container.add(b);
addConstraints(constraints, 2, 0, 1, 1, 0, 0);
layout.setConstraints(c, constraints);
container.add(c);
addConstraints(constraints, 0, 1, 1, 1, 0, 0);
layout.setConstraints(d, constraints);
container.add(d);
addConstraints(constraints, 1, 1, 1, 1, 0, 0);
layout.setConstraints(e, constraints);
container.add(e);
addConstraints(constraints, 2, 1, 1, 1, 0, 0);
layout.setConstraints(f, constraints);
container.add(f);
addConstraints(constraints, 0, 2, 1, 1, 0, 0);
layout.setConstraints(g, constraints);
container.add(g);
addConstraints(constraints, 1, 2, 1, 1, 0, 0);
layout.setConstraints(h, constraints);
container.add(h);
addConstraints(constraints, 2, 2, 1, 1, 0, 0);
layout.setConstraints(i, constraints);
container.add(i);
addConstraints(constraints, 0, 3, 1, 1, 0, 0);
layout.setConstraints(j, constraints);
container.add(j);
addConstraints(constraints, 1, 3, 1, 1, 0, 0);
layout.setConstraints(k, constraints);
container.add(k);
addConstraints(constraints, 2, 3, 1, 1, 0, 0);
layout.setConstraints(l, constraints);
container.add(l);
addConstraints(constraints, 3, 0, 6, 6, 0, 0);
layout.setConstraints(name_field, constraints);
container.add(name_field);
/*
// Name field
// Name Label
addConstraints(constraints, 0, 1, 1, 1, 10, 100);
layout.setConstraints(name_label, constraints);
container.add(name_label);
// Name field
addConstraints(constraints, 1, 1, 1, 0, 90, 100);
layout.setConstraints(name_field, constraints);
container.add(name_field);
// password Label
addConstraints(constraints, 2, 1, 1, 1, 0, 100);
layout.setConstraints(password_label, constraints);
container.add(password_label);
// password field
addConstraints(constraints, 3, 1, 1, 1, 0, 100);
layout.setConstraints(password_field, constraints);
container.add(password_field);
*/
} // method init
// ==============================================================
// purpose -- Place the component in a specific cell, set up
// its width and height, and the weighting ratios
// preconditions -- values for cell placement, size and ratios
// are properly passed to the method
// postconditions -- the constraints of cell placement, size
// and ratios of the component are updated
// ==============================================================
public void addConstraints(GridBagConstraints constraints,
int gridx,
int gridy,
int gridwidth,
int gridheight,
int weightx,
int weighty)
{
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.weightx = weightx;
constraints.weighty = weighty;
} // method addConstraints
} // class GridBag2GUI