Java – Convert

package ;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class convertor{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(350, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);

JTextField field = new JTextField(20);
field.setBounds(100, 20, 165, 25);
frame.add(field);

JComboBox<String> fromComb = new JComboBox<>(new String[] { „USD“, „EUR“, „CZK“ });
fromComb.setBounds(100, 50, 80, 25);
frame.add(fromComb);

JComboBox<String> toComb = new JComboBox<>(new String[] { „USD“, „EUR“, „CZK“ });
toComb.setBounds(100, 80, 80, 25);
frame.add(toComb);

JLabel results = new JLabel(„result: „);
results.setBounds(10, 110, 200, 25);
frame.add(results);

JButton convertButton = new JButton(„convert:“);
convertButton.setBounds(10, 140, 100, 25);
frame.add(convertButton);

convertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

double hodnota = Double.parseDouble(field.getText());
String fromCash = (String) fromComb.getSelectedItem();
String toCash = (String) toComb.getSelectedItem();
double result = convert(hodnota, fromCash, toCash);
results.setText(„Result: “ + result + “ “ + toCash);
}
});
frame.setVisible(true);
}

private static double convert(double hodnota, String fromCash, String toCash) {
switch (fromCash) {
case „USD“:
if („EUR“.equals(toCash)) {
return hodnota * 0.85;
} else if („CZK“.equals(toCash)) {
return hodnota * 22.5;
}
break;

}
return hodnota;
}
}

Diskuze

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *