Java tehtäviä perusteet vastaus5

Mureakuha

Loikkaa: valikkoon, hakuun

Tehtävä:

Kirjoita ohjelma, jossa on JList- ja JComboBox-luokan kontrollit ja painonapit joilla tutkitaan näistä kontrolleista valitut tekstit. Kontrolleissa on kahdeksan tekstiriviä. Valitun rivin teksti tulostetaan JLabel-luokan kontrolliin nappia painettaessa.

ListComboFrame.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
 
/**
 * Ikkunaluokka.
 */
public class ListComboFrame extends JFrame
{
  private String data[] = { "yksi", "kaksi", "kolme", "neljä", "viisi", "kuusi", "seitsemän", "kahdeksan" };
  private JComboBox comboBox = new JComboBox( data );
  private JList list = new JList( data );
  private JLabel label = new JLabel();
 
  /**
   * Konstruktori.
   */
  public ListComboFrame()
  {
    JScrollPane scrollPane = new JScrollPane();
    JButton buttonCombobox = new JButton();
    JButton buttonList = new JButton();
 
    JPanel contentPane = ( JPanel ) getContentPane();
    contentPane.setLayout( new GridBagLayout() );
    setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
    setResizable( false );
    setSize( new Dimension( 400, 300 ) );
    setTitle( "ListBox & ComboBox" );
 
    buttonCombobox.setText( "Tutki ComboBox" );
    buttonCombobox.addActionListener( new java.awt.event.ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        buttonCombobox_actionPerformed( e );
      }
    } );
 
    buttonList.setText( "Tutki List" );
    buttonList.addActionListener( new java.awt.event.ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        buttonList_actionPerformed( e );
      }
    } );
 
    list.setSelectedIndex( 0 );
    list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    list.setVisibleRowCount( 5 );
    label.setBorder( BorderFactory.createEtchedBorder() );
    label.setText( "Valitse" );
 
    comboBox.setMaximumRowCount( 5 );
 
    contentPane.add( comboBox, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 20, 0 ), 0, 0 ) );
    contentPane.add( buttonCombobox,
                     new GridBagConstraints( 2, 1, GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER, 0.0, 0.0
                     , GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets( 0, 20, 0, 0 ), 0, 0 ) );
    contentPane.add( label, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0
        , GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
    contentPane.add( buttonList, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL, new Insets( 0, 20, 0, 0 ), 0, 0 ) );
    contentPane.add( scrollPane, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets( 0, 0, 20, 0 ), 80, 0 ) );
    scrollPane.getViewport().add( list, null );
  }
 
  /**
   * Listan tutkiminen.
   * @param e tapahtuman tarkempi kuvaus
   */
  private void buttonList_actionPerformed( ActionEvent e )
  {
    label.setText( list.getSelectedValue().toString() );
  }
 
  /**
   * Comboboxin tutkiminen.
   * @param e tapahtuman tarkempi kuvaus
   */
  private void buttonCombobox_actionPerformed( ActionEvent e )
  {
    label.setText( comboBox.getSelectedItem().toString() );
  }
 
}

ListComboApp.java

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