|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Bannato
Iscritto dal: Jul 2002
Città: Lucca
Messaggi: 966
|
[Java] Entra in un if dove non dovrebbe entrare -.-'
Codice:
import javax.swing.*; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; /* * * Program created and coded by `StErMi * www.tdoportal.it * mail: [email protected] * for bugs or info contact me. * * */ public class interfaccia extends Frame implements ActionListener { int valuta = 0; JTextField importo = new JTextField(""); JLabel conv = new JLabel(""); JFrame f = new JFrame("Euro Convertitore"); JPanel p = new JPanel(); JPanel p2 = new JPanel(); JRadioButton euro = new JRadioButton("€"); JRadioButton lire = new JRadioButton("£"); JButton converti = new JButton("Converti"); JButton riavvia = new JButton ("Riavvia"); ButtonGroup group = new ButtonGroup(); public interfaccia() { group.add(euro); group.add(lire); // setto i layout f.setLayout(new BorderLayout()); f.setSize(250,250); p.setLayout(new GridLayout(4,2,10,10)); f.getContentPane().add(p, "West"); // aggiungo gli oggetti p.add( new JLabel("Importo: ")); p.add(importo); p.add(euro); p.add(lire); p.add(converti); p.add(riavvia); p.add( new JLabel("Convertito: ")); p.add(conv); // azioni -> bottone converti.addActionListener(this); euro.addActionListener(this); lire.addActionListener(this); riavvia.addActionListener(this); // proprietà finistra f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } public static void main(String[] args) throws IOException { interfaccia main = new interfaccia(); } public void actionPerformed (ActionEvent e ) { double val = 0; String v = ""; String bottone = ""; bottone = e.getActionCommand(); if ( euro.isSelected() ); { valuta = 2; } if ( lire.isSelected()) { valuta = 1; } if ( !( euro.isSelected() ) && !( lire.isSelected() ) ) { conv.setText("Scegli una valuta"); valuta = 0; } if ( bottone.equals("Converti") ) { if ( ( valuta != 0 ) && ( importo.getText() != "" ) ) { v = importo.getText(); val = Integer.parseInt(v); if ( valuta == 1 ) { val *= 1936.27; conv.setText( "" + val + " £" ); } else if ( valuta == 2 ) { val /= 1936.27; conv.setText( "" + val + " €" ); } } } if ( bottone.equals("Riavvia") ) { conv.setText(""); importo.setText(""); } } } provate a non immettere nulla nella casella, a selezionare euro o lire e poi a convertire |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jul 2004
Messaggi: 1578
|
Prova a sostituire con:
if ( ( valuta != 0 ) && ( !importo.getText().equals( "" ) ) ) oppure più semplicemente: if ( ( valuta != 0 ) && ( importo.getText().length() > 0 ) ) Non penso che l'implementazione default dell'operatore di uguaglianza faccia uso di equals, per cui lo devi chiamare esplicitamente e non usare != che altrimenti fa un confronto dei riferimenti. Con la seconda versione poi eviti di istanziare ogni volta una stringa inutile da distruggere subito dopo. |
|
|
|
|
|
#3 |
|
Bannato
Iscritto dal: Jul 2002
Città: Lucca
Messaggi: 966
|
grazie mille.
altre tre cose: 1) come faccio a far si che il layout si ridimensioni in automatico in base alla grandezza dei bottoni? come mai sono subito appiccicati ai bordi? 2) come faccio a immetere un double con solo 2 decimali in una textarea? 3) come faccio a rimettere di default il JRadioButton ? ovvero che non siano pigiati? grazie mille |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:56.



















