Java tehtäviä syventävät vastaus2
Mureakuha
Tehtävä:
Kirjoita ohjelma, joka osaa muuntaa valuuttoja kuvan esittämällä tavalla. '-->'-nappi siirtää Valuutta 1:n edit-kontrolliin kirjoitetun valuuttamäärän Valuutta 2:n edit-kontrolliin valittuun valuuttamuotoon ja päinvastoin. Jos muunnos ei ole mahdollinen palautetaan pelkästään 0.
[muokkaa]
ValuuttaFrame.java
import java.text.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * Ikkunaluokka. */ public class ValuuttaFrame extends JFrame { private JRadioButton radioButton1 = new JRadioButton(); private JRadioButton radioButton2 = new JRadioButton(); private JRadioButton radioButton3 = new JRadioButton(); private JRadioButton radioButton4 = new JRadioButton(); private JRadioButton radioButton5 = new JRadioButton(); private JRadioButton radioButton6 = new JRadioButton(); private JTextField textField1 = new JTextField(); private JTextField textField2 = new JTextField(); /** * Konstruktori. */ public ValuuttaFrame() { JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JButton buttonToRight = new JButton(); JButton buttonToLeft = new JButton(); ButtonGroup buttonGroup1 = new ButtonGroup(); ButtonGroup buttonGroup2 = new ButtonGroup(); GridBagLayout gridBagLayout = new GridBagLayout(); JPanel contentPane = ( JPanel ) getContentPane(); contentPane.setLayout( gridBagLayout ); setSize( new Dimension( 400, 300 ) ); setTitle( "Valuuttamuunnos" ); setDefaultCloseOperation( 3 ); Border border = BorderFactory.createEtchedBorder( Color.white, new Color( 134, 134, 134 ) ); TitledBorder titledBorder1 = new TitledBorder( border, "Valuutta 1" ); panel1.setBorder( titledBorder1 ); panel1.setLayout( gridBagLayout ); TitledBorder titledBorder2 = new TitledBorder( border, "Valuutta 2" ); panel2.setBorder( titledBorder2 ); panel2.setLayout( gridBagLayout ); buttonToRight.setText( "-->" ); buttonToRight.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed( ActionEvent e ) { buttonToRight_actionPerformed( e ); } } ); buttonToLeft.setText( "<--" ); buttonToLeft.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed( ActionEvent e ) { buttonToLeft_actionPerformed( e ); } } ); panel3.setLayout( gridBagLayout ); radioButton1.setSelected( true ); radioButton1.setText( "Markka" ); radioButton2.setText( "Dollari" ); radioButton3.setText( "Euro" ); radioButton4.setSelected( true ); radioButton4.setText( "Markka" ); radioButton5.setText( "Dollari" ); radioButton6.setText( "Euro" ); textField1.setText( "0" ); textField2.setText( "0" ); contentPane.add( panel1, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 50, 50 ) ); panel1.add( radioButton1, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); panel1.add( radioButton2, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); panel1.add( radioButton3, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); panel1.add( textField1, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 50, 0 ) ); contentPane.add( panel2, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 50, 50 ) ); panel2.add( radioButton4, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); panel2.add( radioButton5, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); panel2.add( radioButton6, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); panel2.add( textField2, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 50, 0 ) ); contentPane.add( panel3, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) ); panel3.add( buttonToRight, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 5, 0, 5 ), 0, 0 ) ); panel3.add( buttonToLeft, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 5, 0, 5 ), 0, 0 ) ); buttonGroup1.add( radioButton1 ); buttonGroup1.add( radioButton2 ); buttonGroup1.add( radioButton3 ); buttonGroup2.add( radioButton4 ); buttonGroup2.add( radioButton5 ); buttonGroup2.add( radioButton6 ); } /** * Muunnos oikeaan. * @param e tapahtuman tarkempi kuvaus */ private void buttonToRight_actionPerformed( ActionEvent e ) { double x = 1; if ( radioButton1.isSelected() && radioButton4.isSelected() ) { x = 1.0; } else if ( radioButton1.isSelected() && radioButton5.isSelected() ) { x = 0.147; } else if ( radioButton1.isSelected() && radioButton6.isSelected() ) { x = 0.166; } else if ( radioButton2.isSelected() && radioButton4.isSelected() ) { x = 6.8; } else if ( radioButton2.isSelected() && radioButton5.isSelected() ) { x = 1.0; } else if ( radioButton2.isSelected() && radioButton6.isSelected() ) { x = 1.111; } else if ( radioButton3.isSelected() && radioButton4.isSelected() ) { x = 6.0; } else if ( radioButton3.isSelected() && radioButton5.isSelected() ) { x = 1.111; } else if ( radioButton3.isSelected() && radioButton6.isSelected() ) { x = 1.0; } double d = Double.parseDouble( textField1.getText() ); DecimalFormat muotoilu = new DecimalFormat( "0.00" ); // muotoilu try { Number n = muotoilu.parse( muotoilu.format( d * x ) ); textField2.setText( n.doubleValue() + " " ); } catch ( Exception exc ) {} } /** * Muunnos vasempaan. * @param e tapahtuman tarkempi kuvaus */ private void buttonToLeft_actionPerformed( ActionEvent e ) { double x = 1; if ( radioButton1.isSelected() && radioButton4.isSelected() ) { x = 1.0; } else if ( radioButton1.isSelected() && radioButton5.isSelected() ) { x = 6.8; } else if ( radioButton1.isSelected() && radioButton6.isSelected() ) { x = 6.0; } else if ( radioButton2.isSelected() && radioButton4.isSelected() ) { x = 0.147; } else if ( radioButton2.isSelected() && radioButton5.isSelected() ) { x = 1.0; } else if ( radioButton2.isSelected() && radioButton6.isSelected() ) { x = 0.9; } else if ( radioButton3.isSelected() && radioButton4.isSelected() ) { x = 0.166; } else if ( radioButton3.isSelected() && radioButton5.isSelected() ) { x = 1.111; } else if ( radioButton3.isSelected() && radioButton6.isSelected() ) { x = 1.0; } double d = Double.parseDouble( textField2.getText() ); DecimalFormat muotoilu = new DecimalFormat( "0.00" ); // muotoilu try { Number n = muotoilu.parse( muotoilu.format( d * x ) ); textField1.setText( n.doubleValue() + " " ); } catch ( Exception exc ) {} } }
[muokkaa]
ValuuttaApp.java
import javax.swing.UIManager; import java.awt.*; /** * Varsinainen ohjelma. */ public class ValuuttaApp { /** * Luo ja rakentaa ohjelman. */ public ValuuttaApp() { ValuuttaFrame frame = new ValuuttaFrame(); frame.pack(); // 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 ValuuttaApp(); } }
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ä.
Kopio lisenssistä (englanniksi) löytyy täältä.
Alkuperäinen (c) Petteri Hämäläinen
