Name: joT67522 Date: 12/09/97
On WinNT, JDK1.1.3/4, Swing0.5.1:
1. Run attached sample.
(Sample uses tiled image background; any small .gif will work.
If you want to use it unchanged, use "images/open.gif"
icon which comes with a SwingSet example.)
2. Content panel of the frame uses Y-oriented BoxLayout.
3. Notice a panel with a JLabel "BOX LAYOUT".
Notice another label right below it, titled
"This is some long text".
There is PLENTY of space for both labels to be _center-aligned_
(as requested) horizontally.
Instead, panel is moved left, and label moved right.
BoxLayout does not work as documented (see doc).
They centered in frame, but not against each other.
You can make their width _same_ (by invoking
Dimension d = new Dimension(100, 15);
c1.setMinimalSize(d); c1.setPreferredSize(d); c1.setMaximumSize(d);
c2.setMinimalSize(d); c2.setPreferredSize(d); c2.setMaximumSize(d);
Components should be layed out as
+-------------------+
| |
+-------------------+
+-------------------+
| |
+-------------------+
- that is how you want them to look in any decent UI.
And they layed out as
+-------------------+
| |
+-------------------+
+-------------------+
| |
+-------------------+
That is not how BoxLayout is documented to work.
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.border.*;
public class DecoratedPanel extends JPanel
{
public final static Dimension hpad5 = new Dimension(5,1);
public final static Dimension hpad10 = new Dimension(10,1);
public final static Dimension hpad20 = new Dimension(20,1);
public final static Dimension hpad40 = new Dimension(40,1);
public final static Dimension hpad80 = new Dimension(80,1);
public final static Dimension vpad5 = new Dimension(1,5);
public final static Dimension vpad10 = new Dimension(1,10);
public final static Dimension vpad20 = new Dimension(1,20);
public final static Dimension vpad40 = new Dimension(1,40);
public final static Dimension vpad80 = new Dimension(1,80);
public final static Insets insets0 = new Insets(0,0,0,0);
public final static Insets insets5 = new Insets(5,5,5,5);
public final static Insets insets10 = new Insets(10,10,10,10);
public final static Insets insets15 = new Insets(15,15,15,15);
public final static Insets insets20 = new Insets(20,20,20,20);
public final static Border emptyBorder0 = new EmptyBorder(0,0,0,0);
public final static Border emptyBorder5 = new EmptyBorder(5,5,5,5);
public final static Border emptyBorder10 = new EmptyBorder(10,10,10,10);
public final static Border emptyBorder15 = new EmptyBorder(15,15,15,15);
public final static Border emptyBorder20 = new EmptyBorder(20,20,20,20);
public final static Border raisedBorder = new BevelBorder(
BevelBorder.RAISED);
public final static Border lightLoweredBorder = new BevelBorder(
BevelBorder.LOWERED,
Color.white, Color.gray);
public final static Border loweredBorder = new SoftBevelBorder(
BevelBorder.LOWERED);
public final static Border etchedBorder10 = new CompoundBorder(
new EtchedBorder(),
emptyBorder10);
public final static Border loweredBorder5 = new CompoundBorder(
emptyBorder5,
loweredBorder);
public final static Font defaultFont = new Font("Dialog", Font.PLAIN, 12);
public final static Font defaultBoldFont = new Font("Dialog", Font.BOLD, 12);
public final static Font defaultItalicFont = new Font("Dialog", Font.ITALIC, 12);
public final static Font bigFont = new Font("Helvetica", Font.PLAIN, 18);
public final static Font bigBoldFont = new Font("Helvetica", Font.BOLD, 18);
public final static Font veryBigFont = new Font("Helvetica", Font.PLAIN, 24);
public final static Font veryBigBoldFont = new Font("Helvetica", Font.BOLD, 24);
public DecoratedPanel( )
{
super(true);
URL u = this.getClass().getResource("images/open.gif");
ImageIcon background = new ImageIcon(u);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setLocation(0,0);
setAlignmentY(CENTER_ALIGNMENT);
setAlignmentX(CENTER_ALIGNMENT);
JPanel p;
p = new JPanel();
p.setBorder(raisedBorder);
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(Box.createRigidArea(hpad10));
p.add(new JLabel(background));
p.add(Box.createRigidArea(hpad10));
JLabel l = new JLabel("BOX LAYOUT");
l.setFont(bigBoldFont);
p.add(l);
p.add(Box.createRigidArea(hpad10));
add(p);
JCheckBox ode = new JCheckBox();
ode.setIcon(background);
ode.setFont(defaultBoldFont);
ode.setOpaque(false);
ode.setText("This is some moderately long text");
add( ode );
System.out.println("Object initialized successfully");
setBackgroundImage( background );
_frame = new JFrame();
_frame.setTitle("Buggy, buggy Swing...");
_frame.setBackground(Color.lightGray);
_frame.getContentPane().setLayout(new BorderLayout());
_frame.getContentPane().add("Center", this);
_frame.setJMenuBar(createMenubar());
_frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
_frame.pack();
_frame.setSize(200,300);
_frame.show();
}
public ImageIcon getBackgroundImage()
{
return _background;
}
public void setBackgroundImage(ImageIcon background)
{
_background = background;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (_background != null)
{
Color oldColor = g.getColor();
Dimension sz = getSize();
int tileW = _background.getIconWidth();
int tileH = _background.getIconHeight();
if (tileW < 0 || tileH < 0)
return;
int xpos, ypos, startx, starty;
Graphics cg;
Insets bdr = getInsets();
// Paint tiled background (clip out border)
cg = g.create();
cg.setClip(bdr.left, bdr.top,
sz.width -(bdr.left+bdr.right),
sz.height -(bdr.top+bdr.bottom));
for (ypos = 0; sz.height - ypos > 0; ypos += tileH)
{
for (xpos = 0; sz.width - xpos > 0; xpos += tileW)
{
_background.paintIcon(this, cg, xpos, ypos);
}
}
cg.dispose();
g.setColor(oldColor);
}
}
static protected JMenuBar createMenubar()
{
JMenuBar mb = null;
mb = new JMenuBar();
JMenu m = new JMenu("File");
JMenuItem mi = new JMenuItem("Exit");
mi.setActionCommand("exit");
m.add(mi);
mb.add(m);
return mb;
}
public static void main(String[] args)
{
try
{
DecoratedPanel dp = new DecoratedPanel();
}
catch (Throwable t)
{
System.out.println("Fatal: uncaught exception: " + t);
t.printStackTrace();
}
}
protected ImageIcon _background = null;
protected JMenuBar _menubar = null;
protected JFrame _frame;
}
(Review ID: 21558)
======================================================================