Java tehtäviä perusteet vastaus1

Mureakuha

Loikkaa: valikkoon, hakuun

Tehtävä:

Kirjoita ohjelma, joka tulostaa lomakkeelle vasempaan reunaan, keskelle pystysuunnassa ruudun ja ikkunan koot. Tietojen täytyy pysyä näkyvissä koko ajan vaikka lomakkeen paikka tai koko muuttuisikin.

RuudunkokoFrame.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
/**
 * Ikkunaluokka.
 */
public class RuudunkokoFrame extends JFrame
{
 
  /**
   * Konstruktori.
   */
  public RuudunkokoFrame()
  {
    JPanel contentPane = ( JPanel ) getContentPane();
    contentPane.setLayout( new BorderLayout() );
    setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
    setSize( new Dimension( 400, 300 ) );
    setTitle( "Ruudun koon tarkastelu" );
  }
 
  /**
   * Ikkunan piirtometodi.
   * @param grfx piirtokonteksti
   */
  public void paint( Graphics grfx )
  {
    super.paint( grfx );
 
    Dimension frameSize = getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 
    // asettaa fontin
    Font font = new Font( "SansSerif", Font.BOLD, 13 );
    grfx.setColor( Color.black );
    grfx.setFont( font );
 
    // pirtää tekstit
    String text1 = screenSize.width + " ruudun leveys";
    String text2 = screenSize.height + " ruudun korkeus";
    String text3 = frameSize.width + " ikkunan leveys";
    String text4 = frameSize.height + " ikkunan korkeus";
 
    grfx.drawString( text1, 10, frameSize.height / 2 - 20 );
    grfx.drawString( text2, 10, frameSize.height / 2 );
    grfx.drawString( text3, 10, frameSize.height / 2 + 20 );
    grfx.drawString( text4, 10, frameSize.height / 2 + 40 );
  }
 
}

RuudunkokoApp.java

import javax.swing.UIManager;
import java.awt.*;
 
/**
 * Varsinainen ohjelma.
 */
public class RuudunkokoApp
{
 
  /**
   * Luo ja rakentaa ohjelman.
   */
  public RuudunkokoApp()
  {
    RuudunkokoFrame frame = new RuudunkokoFrame();
    frame.validate();
 
    // keskittää ohjelman ruudun keskelle
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if ( frameSize.height > screenSize.height )
      frameSize.height = screenSize.height;
 
    if ( frameSize.width > screenSize.width )
      frameSize.width = screenSize.width;
 
    frame.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );
 
    // näyttää ohjelman
    frame.setVisible( true );
  }
 
  /**
   * Main-metodi.
   * @param args käynnistysparametrit
   */
  public static void main( String[] args )
  {
    try
    {
      UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
    }
    catch ( Exception e )
    {
      e.printStackTrace();
    }
    new RuudunkokoApp();
  }
}
Tämän dokumentin kopiointi, levittäminen sekä muokkaaminen on sallittua GNU Free Documentation Licensen version 1.2 tai uudemman Free Software Foundationin julkaiseman version mukaisesti, ilman muuttumattomuuslauseketta tai kansitekstejä. Tätä koskee vastuuvapaus.
Kopio lisenssistä (englanniksi) löytyy täältä.

Alkuperäinen (c) Petteri Hämäläinen

Henkilökohtaiset työkalut