examples
Class Example10
java.lang.Object
examples.Example10
public class Example10
- extends java.lang.Object
This example creates the
GUI proposed as a challenge problem for layout managers.
Note that the code requires that the static methods of the class EasyCell
be imported using the import static pagelayout.EasyCell.*
statement.
public static void createGUI()
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container container=frame.getContentPane();
JLabel lastName=new JLabel("Last Name");
JTextField lastNameE=new JTextField("Martian",20);
JLabel firstName=new JLabel("First Name");
JTextField firstNameE=new JTextField("Marvin",20);
JLabel phone=new JLabel("Phone");
JTextField phoneE=new JTextField("805-123-4567",20);
JLabel email=new JLabel("Email");
JTextField emailE=new JTextField("marvin@wb.com",20);
JLabel address1=new JLabel("Address1");
JTextField address1E=new JTextField("1001001010110 Martian Way ",30);
JLabel address2=new JLabel("Addres2");
JTextField address2E=new JTextField("Suite 101101011 ",30);
JLabel city=new JLabel("City");
JTextField cityE=new JTextField("Ventura ",20);
JLabel state=new JLabel("State");
JTextField stateE=new JTextField("CA ",20);
JLabel country=new JLabel("Country");
JTextField countryE=new JTextField("USA",20);
JLabel postal=new JLabel("Postal Code");
JTextField postalE=new JTextField("93001 ",20);
JButton b1=new JButton("New");
JButton b2=new JButton("Delete");
JButton b3=new JButton("Edit");
JButton b4=new JButton("Save");
JButton b5=new JButton("Cancel");
JList list=new JList(new StringListModel());
list.setBorder(BorderFactory.createLoweredBevelBorder());
JScrollPane scroll=new JScrollPane(list);
CellGrid grid=null;
Row buttons=null;
//Create the top level row.
Row topLevel =
row(scroll, // JList on left
column(grid=grid(
lastName,lastNameE,firstName,firstNameE, eol(),//Line 1
phone,phoneE,
row(none,center,email,emailE),
span(),eol(),//Line 2
address1,address1E,span(),span(),eol(),//Line 3
address2,address2E,span(),span(),eol(),//Line 4
city,cityE,eol(),//Line 5
state,stateE,postal,postalE,eol(),//Line 6
country,countryE),//Line 7
buttons=row(justified,center,b1,b2,b3,b4,b5)));
// Alignment of the cells in the grid.
grid.setAlignments(
new int[][]{{right,none,left,none}},
new int[][]{{center,center,center,center}});
buttons.linkWidth(b5,1,b1,b2,b3,b4);
// Create the layout.
topLevel.createLayout(container);
frame.pack();
frame.setSize(frame.getPreferredSize());
frame.show();
}
public static class StringListModel implements ListModel
{
public void addListDataListener (ListDataListener l){}
String[] names={
"Bunny Buggs",
"Cat, Sylvester ",
"Coyote, Willie E. ",
"Devil, Tasmanian",
"Duck, Daffy",
"Fudd, Elmer",
"LePew, Pepe",
"Martian, MArvin"
};
public Object getElementAt(int index)
{
if(index<names.length)return names[index];
return new String(" ");
}
public int getSize(){ return 30;}
public void removeListDataListener (ListDataListener l){}
}
public static void main(String[] args)
{
createGUI();
}
Method Summary |
static void |
createGUI()
|
static void |
main(java.lang.String[] args)
|
static void |
run()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Example10
public Example10()
createGUI
public static void createGUI()
main
public static void main(java.lang.String[] args)
run
public static void run()