Layout Managere
Vejledende løsninger

 

 

I det følgende er kun medtaget selve frame-klassen, men den skal selvfølgelig instantieres fra en passende main-metode.

 

1
import javax.swing.*;

class Opg1Frame extends JFrame {
  
  public Opg1Frame ( String title ) {
    super( title );
    
    GridBagGUI gui = new GridBagGUI( getContentPane() );
    gui.setFill( 'B' );
    gui.setWeightX( 1 );
    gui.setWeightY( 1 );
    
    // Navn
    gui.setPosition( 0, 0 );
    gui.setSize( 1, 1 );
    gui.setWeightX( 0 );
    gui.setInsets( 15, 15, 2, 5 );
    gui.add( new JLabel( "Navn:" ) );
    
    JTextField navnInput = new JTextField();
    gui.setPosition( 1, 0 );
    gui.setSize( 2, 1 );
    gui.setWeightX( 1 );
    gui.setInsets( 15, 5, 2, 15 );
    gui.add( navnInput );
    
    // Adresse
    gui.setPosition( 0, 1 );
    gui.setSize( 1, 1 );
    gui.setWeightX( 0 );
    gui.setInsets( 2, 15, 2, 5 );
    gui.add( new JLabel( "Adresse:" ) );
    
    JTextField adrInput = new JTextField();
    gui.setPosition( 1, 1 );
    gui.setSize( 2, 1 );
    gui.setWeightX( 1 );
    gui.setInsets( 2, 5, 2, 15 );
    gui.add( adrInput );
    
    // Postnr/By
    gui.setPosition( 0, 2 );
    gui.setSize( 2, 1 );
    gui.setWeightX( 0 );
    gui.setInsets( 2, 15, 15, 5 );
    gui.add( new JLabel( "Postnr/By:" ) );
    
    JTextField byInput = new JTextField();
    gui.setPosition( 1, 2 );
    gui.setSize( 1, 1 );
    gui.setWeightX( 1 );
    gui.setInsets( 2, 5, 15, 0 );
    gui.add( byInput );
    
    gui.setPosition( 2, 2 );
    gui.setSize( 1, 1 );
    gui.add( new JLabel() );
    
    setDefaultCloseOperation( EXIT_ON_CLOSE );
    
    setSize( 300, 150 );
    setVisible( true );
  }
}

 

2
import javax.swing.*;

class Opg2Frame extends JFrame {
  
  public Opg2Frame ( String title ) {
    super( title );
    
    GridBagGUI gui = new GridBagGUI( getContentPane() );
    
    JTextField tf = new JTextField();
    gui.setPosition( 0, 0 );
    gui.setSize( 3, 1 );
    gui.setFill( "HORIZONTAL" );
    gui.setInsets( 15, 15, 2, 15 );
    gui.setWeightX( 1 );
    gui.add( tf );
    
    JButton a = new JButton( "A" );
    gui.setPosition( 0, 1 );
    gui.setSize( 1, 1 );
    gui.setFill( "BOTH" );
    gui.setInsets( 2, 15, 2, 5 );
    gui.setWeight( 1, 1 );
    gui.add( a );
    
    JButton b = new JButton( "B" );
    gui.setPosition( 0, 2 );
    gui.setSize( 1, 1 );
    gui.setFill( "BOTH" );
    gui.setInsets( 2, 15, 15, 5 );
    gui.add( b );
    
    JTextArea ta = new JTextArea();
    gui.setPosition( 1, 1 );
    gui.setSize( 1, 2 );
    gui.setFill( "BOTH" );
    gui.setInsets( 2, 2, 15, 2 );
    gui.setWeightX( 2 );
    gui.add( ta );
    
    JButton c = new JButton( "C" );
    gui.setPosition( 2, 1 );
    gui.setSize( 1, 1 );
    gui.setFill( "BOTH" );
    gui.setInsets( 2, 2, 2, 15 );
    gui.setWeightX( 1 );
    gui.add( c );
    
    JButton d = new JButton( "D" );
    gui.setPosition( 2, 2 );
    gui.setSize( 1, 1 );
    gui.setFill( "BOTH" );
    gui.setInsets( 2, 2, 15, 15 );
    gui.setWeightX( 1 );
    gui.add( d );
    
    setDefaultCloseOperation( EXIT_ON_CLOSE );
    
    setSize( 500, 150 );
    setVisible( true );
  }
}

 

3
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class RadioValutaFrame extends JFrame implements ActionListener {
  
  private JRadioButton USD, GBP, YEN, EURO;
  private JTextField input, output;
  private JPanel panel;
  
  public RadioValutaFrame( String title ) {
    super( title );
    
    ButtonGroup valutaGruppe = new ButtonGroup();
    
    /*
     * Create JRadioButton's
     */
     USD = new JRadioButton( "USD $" );
     GBP = new JRadioButton( "GBP £" );
     YEN = new JRadioButton( "YEN" );
    EURO = new JRadioButton( "EURO" );

    /*
     * add all JRadioButton's to ButtonGroup
     */
    valutaGruppe.add( USD );
    valutaGruppe.add( GBP );
    valutaGruppe.add( YEN );
    valutaGruppe.add( EURO );
    
    /*
     * ActionListener on all JRadioButton's
     */
     USD.addActionListener( this );
     GBP.addActionListener( this );
     YEN.addActionListener( this );
    EURO.addActionListener( this );

    /*
     * Create layout
     */
    panel = new JPanel();
    
    GridBagGUI gui = new GridBagGUI( panel );
    gui.setFill( "HORIZONTAL" );
    gui.setHeight( 1 );

    // Input DKK Label
    JLabel inputLabel = new JLabel( "DKK:", SwingConstants.RIGHT );
    gui.setPosition( 0, 0 );
    gui.setWidth( 1 );
    gui.add( inputLabel );
    
    // Input DKK Tekstfelt
    input = new JTextField();
    gui.setPosition( 1, 0 );
    gui.setWidth( 3 );
    gui.add( input );
    
    // Output EXCH Label
    JLabel outputLabel = new JLabel( "EXCH:", SwingConstants.RIGHT );
    gui.setPosition( 0, 1 );
    gui.setWidth( 1 );
    gui.add( outputLabel );
    
    // Output EXCH Tekstfelt
    output = new JTextField();
    gui.setPosition( 1, 1 );
    gui.setWidth( 3 );
    gui.setInsets( 5, 0, 10, 0 );
    gui.add( output );
    
    // USD $
    gui.setPosition( 0, 3 );
    gui.setWidth( 2 );
    gui.add( USD );
    
    // GBP £
    gui.setPosition( 0, 4 );
    gui.setWidth( 2 );
    gui.add( GBP );
    
    // YEN
    gui.setPosition( 2, 3 );
    gui.setWidth( 1 );
    gui.add( YEN );
    
    // EURO
    gui.setPosition( 2, 4 );
    gui.setWidth( 1 );
    gui.add( EURO );

    panel.setBorder(
      new CompoundBorder(
        new EmptyBorder( 10, 10, 10, 10 ),
        new CompoundBorder(
          new TitledBorder(
            new EtchedBorder( EtchedBorder.LOWERED ), "Valuta" ),
          new EmptyBorder( 10, 10, 5, 10 ) ) ) );
    
    getContentPane().add( panel );
    
    setDefaultCloseOperation( EXIT_ON_CLOSE );
    
    pack();
    setVisible( true );
  }
  
  public void actionPerformed( ActionEvent e ) {
    try {
      double exch;
      
      double dkk = Double.parseDouble( input.getText() );
      
      Object source = e.getSource();
      
      if ( source == USD )
        exch = dkk/7.3714F;
      else if ( source == GBP )
        exch = dkk/11.7914F;
      else if ( source == YEN )
        exch = dkk/0.072120F;
      else
        exch = dkk/7.4392F;
      
      output.setText( "" + exch );
    }
    catch ( NumberFormatException nfe ) {
    }
  }
}

 

4
import javax.swing.*;
import java.awt.event.*;

public class FakturaFrame extends JFrame implements ActionListener {
  private JTextField pris, vægt, fragt, total;
  
  public FakturaFrame( String title ) {
    super( title );
    
    GridBagGUI gui = new GridBagGUI( getContentPane() );
    gui.setFill( "BOTH" );
    gui.setInsets( 3, 3, 3, 3 );
    
    /*
     *  Pris
     */
    gui.setPosition( 0, 0 );
    gui.setSize( 2, 1 );
    gui.add( new JLabel( "Pris:", SwingConstants.RIGHT ) );
    
    pris = new JTextField( 8 );
    pris.addActionListener( this );
    gui.setPosition( 2, 0 );
    gui.setSize( 1, 1 );
    gui.add( pris );
    
    /*
     *  Fragt
     */
    vægt = new JTextField( 8 );
    vægt.addActionListener( this );
    gui.setPosition( 0, 1 );
    gui.add( vægt );
    
    gui.setPosition( 1, 1 );
    gui.setSize( 1, 1 );
    gui.add( new JLabel( "kg. = fragt:" ) );
    
    fragt = new JTextField( 8 );
    fragt.setEditable( false );
    gui.setPosition( 2, 1 );
    gui.add( fragt );

    /*
     *  Total
     */
    gui.setPosition( 0, 2 );
    gui.setSize( 2, 1 );
    gui.add( new JLabel( "I alt:", SwingConstants.RIGHT ) );
    
    total = new JTextField( 8 );
    total.setEditable( false );
    gui.setPosition( 2, 2 );
    gui.setSize( 1, 1 );
    gui.add( total );
    
    setDefaultCloseOperation( EXIT_ON_CLOSE );
    
    pack();
    setVisible( true );
  }
  
  public void actionPerformed( ActionEvent e ) {
    try {
      double vægten = Double.parseDouble( vægt.getText() );

      fragt.setText( "" + (vægten * 5.0) );
    }
    catch ( NumberFormatException ex1 ) {
      fragt.setText( "" );
    }

    try {
      double prisen  = Double.parseDouble( pris.getText() );
      double fragten = Double.parseDouble( fragt.getText() );

      total.setText( "" + (prisen + fragten) );
    }
    catch ( NumberFormatException ex2 ) {
      total.setText( "" );
    }
  }
}