The key problem we are trying to solve is to keep the UI attractive and useful for a variety of window sizes and shapes. If we do not allow the user to resize the window (or resize it for different screen sizes), we can simply specify the exact location and size for each component (we will discuss how later).
setLayout(Layout l)
The flow layout is the default layout for JPanel. The flow layout arranges the widgets in rows, according to the order of addition. Components are added to each row until no-more can fit, then another row is started. Rows are filled from top to bottom. The components within a row are center aligned by default. When the frame window is resized, the flow latout recomputes the position of the components based on this algorithm.
GridLayout grid = new GridLayout(3,4);
setLayout(grid);
create...
methods on Box rather than constructors.
Box[] boxes = new Box[3];
boxes[0] = Box.createHorizontalBox();
boxes[1] = Box.createHorizontalBox();
boxes[2] = Box.createHorizontalBox();
// add buttons and glue
for(int i=0;i<11;i++){
boxes[i%3].add(buttons[i]);
if(i%2 == 1)
boxes[i%3].add(Box.createHorizontalGlue());
}
// stack hboxes in vbox with a 50 pixel strut and glue
vbox.add(boxes[0]);
vbox.add(Box.createVerticalStrut(50));
vbox.add(boxes[1]);
vbox.add(Box.createVerticalGlue());
vbox.add(boxes[2]);
// utility method to create and set up constraints
public GridBagConstraints makeConstraints(int x, int y, int w, int h){
GridBagConstraints c = new GridBagConstraints();
c.weightx = 100;
c.weighty = 100;
c.gridx = x;
c.gridy = y;
c.gridwidth = w;
c.gridheight = h;
c.fill = GridBagConstraints.BOTH;
return(c);
}
null and positioning and sizing
each component by hand using the
setBounds(int x, int y, int w, int h) method on the components.
java.io
package my.package.name;must be the first non-comment line of the .java file.
package line are put into a default
unnamed package.
java.io.FileInputStream. To save typing, the
import line, makes a public classes in a package
available for reference by their internal name. This is the meaning of the
lines
import java.io.*; import javax.swing.*;that we have used on many of our programs.
public is their definition.
By default, classes are given package scope.
package org.aduni.javacourse.lecture14;That ought to be unique!
org.aduni.javacourse.lecture14.LayoutTest in the file
LayoutTest.class in the
directory org/aduni/javacourse/lecture14 relative to
the CLASSPATH (or current directory).
jar command which has
command line arguments similar to UNIX tar. If I had just compiled my
LayoutTest program in the current directory (but specified package
in the sources),
and wanted to create a JAR file. I would create the needed subdirectories,
then
cp *class org/aduni/javacourse/lecture14 jar cvf LayoutTest.jar org/aduni/javacourse/lecture14/*.classThis will create LayoutTest.jar with all of my classes having the proper relative pathnames.
java -classpath LayoutTest.jar LayoutTestshould runt the LayoutTest program from within the JAR file.
Manifest-Version: 1.0 Main-Class: org.aduni.javacourse.lecture14.LayoutTest
cp *class org/aduni/javacourse/lecture14 jar cvfm LayoutTest.jar manifest.mf org/aduni/javacourse/lecture14/*.classNote: the order of the 'f' and 'm' tags must be the same as the outputfile and manifest file arguments
java -jar LayoutTest.jar