package test;
import java.awt.*;
import javax.swing.*;
public class LayoutFrame extends JFrame{
public static void main( String[] args ) {
LayoutFrame frame = new LayoutFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public LayoutFrame(){
setContentPane( createContent() );
}
private Container createContent(){
JPanel parent = new BackgroundPanel( new GridBagLayout() );
Component center = createCenter();
JSeparator line = new JSeparator( JSeparator.HORIZONTAL );
Component footer = createFooter();
parent.add( center, new GridBagConstraints( 0, 0, 1, 1, 1.0, 1000.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets( 20, 100, 5, 100 ), 0, 0 ));
parent.add( line, new GridBagConstraints( 0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets( 0, 0, 0, 0 ), 0, 0 ));
parent.add( footer, new GridBagConstraints( 0, 2, 1, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets( 20, 100, 5, 100 ), 0, 0 ));
return parent;
}
private Component createCenter(){
JPanel parent = new InvisiblePanel( new GridBagLayout() );
JLabel title = new JLabel( "<html><font size=25><u>Sprachauswahl</u></font></html>" );
JLabel blopChoice = new JLabel( "<html><font size=30>O</font></html>" );
JLabel titleChoice = new JLabel( "Waehlen Sie eine Sprache aus der Liste aus" );
JComboBox choice = new JComboBox( new Object[]{ "Deutsch <=> Franzoesisch" } );
choice.setEditable( false );
choice.setSelectedIndex( 0 );
JLabel orLabelChoice = new JLabel( "<html><font size=8>oder</font></html>" );
JLabel blopLoad = new JLabel( "<html><font size=30>O</font></html>" );
JLabel titleLoad = new JLabel( "Laden Sie eine Vokabelliste von der Festplatte" );
JButton buttonLoad = new JButton( "Vokabelliste laden" );
JLabel orLabelLoad = new JLabel( "<html><font size=8>oder</font></html>" );
JLabel blopLabelNew = new JLabel( "<html><font size=30>O</font></html>" );
JLabel titleNew = new JLabel( "Legen Sie eine neue Vokabelliste an" );
JButton buttonNew = new JButton( "Neue Vokabelliste" );
// add
parent.setLayout( new GridBagLayout() );
add( parent, title, 1, 0, 1, 1, false, new Insets( 10, 10, 10, 10 ));
add( parent, blopChoice, 0, 1, 1, 3, 3.0, 3.0, GridBagConstraints.NORTH, new Insets( 20, 20, 20, 20 ));
add( parent, new InvisibleBox( blopChoice, 1, 1.0/3.0), 1, 1, 1, 1, true, new Insets( 20, 5, 5, 5 ));
add( parent, titleChoice, 1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, new Insets( 0, 5, 5, 5 ));
add( parent, choice, 1, 3, 1, 1, false, new Insets( 5, 5, 5, 5 ));
add( parent, orLabelChoice, 0, 4, 1, 1, false, new Insets( 2, 2, 2, 2 ));
add( parent, new InvisibleBox( orLabelChoice, 1, 1 ), 2, 3, 1, 1, false, new Insets( 2, 2, 2, 2 ) );
add( parent, blopLoad, 0, 5, 1, 3, 3.0, 3.0, GridBagConstraints.NORTH, new Insets( 20, 20, 20, 20 ));
add( parent, new InvisibleBox( blopLoad, 1, 1.0/3.0), 1, 5, 1, 1, true, new Insets( 20, 5, 5, 5 ));
add( parent, titleLoad, 1, 6, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, new Insets( 0, 5, 5, 5 ));
add( parent, buttonLoad, 1, 7, 1, 1, false, new Insets( 5, 5, 5, 5 ));
add( parent, orLabelLoad, 0, 8, 1, 1, false, new Insets( 2, 2, 2, 2 ));
add( parent, blopLabelNew, 0, 9, 1, 3, 3.0, 3.0, GridBagConstraints.NORTH, new Insets( 20, 20, 20, 20 ));
add( parent, new InvisibleBox( blopLabelNew, 1, 1.0/3.0), 1, 9, 1, 1, true, new Insets( 20, 5, 5, 5 ));
add( parent, titleNew, 1, 10, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, new Insets( 0, 5, 5, 5 ));
add( parent, buttonNew, 1, 11, 1, 1, false, new Insets( 5, 5, 5, 5 ));
return parent;
}
private Component createFooter(){
JPanel parent = new InvisiblePanel( new GridLayout( 1, 3 ));
JButton start = new JButton( "Abfrage starten" );
JButton stop = new JButton( "Beenden" );
parent.add( new InvisiblePanel( null ) );
JPanel startPanel = new InvisiblePanel( new GridBagLayout() );
add( startPanel, start, 0, 0, 1, 1, false, new Insets( 1, 1, 1, 1 ));
JPanel stopPanel = new InvisiblePanel( new GridBagLayout() );
add( stopPanel, stop, 0, 0, 1, 1, false, new Insets( 1, 1, 1, 1 ));
parent.add( startPanel );
parent.add( stopPanel );
return parent;
}
private void add( Container parent, Component child, int x, int y, int w, int h, boolean center, Insets insets ){
add( parent, child, x, y, w, h, 1.0, 1.0, center ? GridBagConstraints.CENTER : GridBagConstraints.NORTH, insets );
}
private void add( Container parent, Component child, int x, int y, int w, int h, double cw, double ch, int anchor, Insets insets ){
parent.add( child, new GridBagConstraints(
x, y, w, h, cw, ch, anchor,
GridBagConstraints.NONE, insets, 0, 0 ));
}
private static class BackgroundPanel extends JPanel{
public BackgroundPanel( LayoutManager manager ){
super( manager );
}
@Override
protected void paintComponent( Graphics g ) {
Graphics2D g2 = (Graphics2D)g;
g2.setPaint( new GradientPaint( 0, 0, new Color( 100, 255, 100 ),
getWidth(), 0, new Color( 100, 100, 255 )) );
g2.fillRect( 0, 0, getWidth(), getHeight() );
}
}
private static class InvisiblePanel extends JPanel{
public InvisiblePanel( LayoutManager manager ){
super( manager );
setOpaque( false );
}
}
private static class InvisibleBox extends JPanel{
private Component size;
private double fw, fh;
public InvisibleBox( Component size, double fw, double fh ){
this.size = size;
this.fw = fw;
this.fh = fh;
setOpaque( false );
}
@Override
public Dimension getPreferredSize() {
Dimension size = this.size.getPreferredSize();
return new Dimension( (int)(fw * size.width), (int)(fh * size.height));
}
}
}