Java tehtäviä perusteet vastaus3

Mureakuha

Loikkaa: valikkoon, hakuun

Tehtävä:

Kirjoita ohjelma, jossa on kaksi JCheckBox ja JRadioButton tyylin kontrollia. Näiden tila tutkitaan painettaessa kyseisiä painonappeja ja tulos tulostetaan JLabel-luokan kontrolleihin.

CheckboxFrame.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
 
/**
 * Ikkunaluokka.
 */
public class CheckboxFrame extends JFrame
{
  private JRadioButton radioButton1 = new JRadioButton();
  private JRadioButton radioButton2 = new JRadioButton();
  private JButton radioButton = new JButton();
  private JCheckBox checkbox1 = new JCheckBox();
  private JCheckBox checkbox2 = new JCheckBox();
  private JButton checkboxButton = new JButton();
  private JLabel label1 = new JLabel();
  private JLabel label2 = new JLabel();
  private JLabel label3 = new JLabel();
 
  /**
   * Konstruktori.
   */
  public CheckboxFrame()
  {
    JPanel panel = new JPanel();
    panel.setLayout( new GridBagLayout() );
 
    JPanel contentPane = ( JPanel ) getContentPane();
    Border border = BorderFactory.createEtchedBorder( Color.white, new Color( 134, 134, 134 ) );
    contentPane.setLayout( new GridBagLayout() );
    setResizable( false );
    setSize( new Dimension( 400, 300 ) );
    setTitle( "CheckBox & RadioButton" );
    setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
 
    radioButton1.setSelected( true );
    radioButton1.setText( "RadioButton1" );
    radioButton2.setText( "RadioButton2" );
    radioButton.setText( "Radio" );
    radioButton.addActionListener( new java.awt.event.ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        radioButton_actionPerformed( e );
      }
    } );
 
    checkbox1.setText( "CheckBox1" );
    checkbox2.setText( "CheckBox2" );
    checkboxButton.setText( "Check" );
    checkboxButton.addActionListener( new java.awt.event.ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        checkboxButton_actionPerformed( e );
      }
    } );
 
    label1.setBorder( border );
    label1.setText( " " );
    label2.setBorder( border );
    label2.setText( " " );
    label3.setBorder( border );
    label3.setText( " " );
 
    contentPane.add( panel, new GridBagConstraints( 1, 0, 1, 1, 1.0, 1.0
        , GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 2, 0, 0 ), 0, 201 ) );
    panel.add( radioButton1, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
    panel.add( radioButton2, new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 40, 0, 40 ), 0, 0 ) );
    panel.add( radioButton, new GridBagConstraints( 1, 2, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 20, 0, 0, 0 ), 0, 1 ) );
    panel.add( checkbox1, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0
                                                  , GridBagConstraints.CENTER, GridBagConstraints.NONE,
                                                  new Insets( 0, 0, 0, 0 ), 0, 0 ) );
    panel.add( checkbox2, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0
                                                  , GridBagConstraints.CENTER, GridBagConstraints.NONE,
                                                  new Insets( 0, 40, 0, 40 ), 0, 0 ) );
    panel.add( checkboxButton, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 20, 0, 0, 0 ), 0, 0 ) );
    panel.add( label1, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0
                                               , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                                               new Insets( 10, 0, 0, 0 ), 80, 0 ) );
    panel.add( label2, new GridBagConstraints( 0, 4, 1, 1, 0.0, 0.0
                                               , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                                               new Insets( 10, 0, 0, 0 ), 0, 0 ) );
    panel.add( label3, new GridBagConstraints( 1, 4, 1, 1, 0.0, 0.0
                                               , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                                               new Insets( 10, 0, 0, 0 ), 0, 0 ) );
 
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add( radioButton1 );
    buttonGroup.add( radioButton2 );
  }
 
  /**
   * Radiopainikkeiden tutkinen.
   * @param e tapahtuman tarkempi kuvaus
   */
  void radioButton_actionPerformed( ActionEvent e )
  {
    if ( radioButton1.isSelected() )
      label3.setText( "Radio1 päällä" );
    else
      label3.setText( "Radio2 päällä" );
  }
 
  /**
   * Checkboxien tutkinen.
   * @param e tapahtuman tarkempi kuvaus
   */
  void checkboxButton_actionPerformed( ActionEvent e )
  {
    if ( checkbox1.isSelected() )
      label1.setText( "Check1 päällä" );
    else
      label1.setText( "Check1 pois" );
 
    if ( checkbox2.isSelected() )
      label2.setText( "Check2 päällä" );
    else
      label2.setText( "Check2 pois" );
  }
 
}

CheckboxApp.java

import javax.swing.UIManager;
import java.awt.*;
 
/**
 * Varsinainen ohjelma.
 */
public class CheckboxApp
{
  /**
   * Luo ja rakentaa ohjelman.
   */
  public CheckboxApp()
  {
    CheckboxFrame frame = new CheckboxFrame();
    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 CheckboxApp();
  }
}
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