因此,我一直在为我的计算机科学课开发货币换算程序。我正在Netbeans中使用GUI Builder来编写程序。这是我的代码:
package my.currencyconversion;
import java.util.*;
import java.io.*;
import javax.swing.*;
/**
*
* @author cory.eheart
*/
public class currencyConversionUI extends javax.swing.JFrame {
/**
* Creates new form currencyConversionUI
*/
public currencyConversionUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtInput = new javax.swing.JTextField();
txtOutput = new javax.swing.JTextField();
cmbInput = new javax.swing.JComboBox<>();
cmbOutput = new javax.swing.JComboBox<>();
btnCancel = new javax.swing.JButton();
btnConvert = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Currency Converter");
jLabel1.setText("Original:");
jLabel2.setText("Converted:");
cmbInput.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "US$", "€", "JP¥", "£", "AU$", "CA$", "SW₣", "CH¥", "₱", "SEK" }));
cmbOutput.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "US$", "€", "JP¥", "£", "AU$", "CA$", "SW₣", "CH¥", "₱", "SEK" }));
btnCancel.setText("Cancel");
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
btnConvert.setText("Convert");
btnConvert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConvertActionPerformed(evt);
}
});
btnConvert.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
btnConvertKeyPressed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(txtInput, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(txtOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmbInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmbOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(btnCancel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnConvert)))
.addContainerGap(23, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmbInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmbOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnCancel)
.addComponent(btnConvert))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void btnConvertActionPerformed(java.awt.event.ActionEvent evt) {
double temp;
String cmbOutput1 = cmbOutput.getSelectedItem().toString();
String input = txtInput.getText();
if (input.length() < 1){
JOptionPane.showMessageDialog(null, "You didn't try to convert anything!");
} else {
temp = Double.parseDouble(txtInput.getText());
if (temp < 0){
JOptionPane.showMessageDialog(null, "No one uses negative currency!");
} else {
switch (cmbInput.getSelectedItem().toString()){
case "US$":
USD(temp, cmbOutput1);
break;
case "/u20AC":
Euro(temp, cmbOutput1);
break;
case "JP/u00A5":
JPYen(temp, cmbOutput1);
break;
case "/u00A3":
Pound(temp, cmbOutput1);
break;
case "AU$":
AUD(temp, cmbOutput1);
break;
case "CA$":
CAD(temp, cmbOutput1);
break;
case "SW/u20A3":
SWF(temp, cmbOutput1);
break;
case "CH/u00A5":
CHYen(temp, cmbOutput1);
break;
case "/u20B1":
Peso(temp, cmbOutput1);
break;
case "SEK":
SEK(temp, cmbOutput1);
break;
}
}
}
}
private void btnConvertKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(currencyConversionUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(currencyConversionUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(currencyConversionUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(currencyConversionUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new currencyConversionUI().setVisible(true);
}
});
}
public void USD(double input, String cmbInput1){
switch(cmbInput1){
case "US$":
txtOutput.setText(String.valueOf(input));
break;
case "/u20AC":
double UStoE = input * .81;
txtOutput.setText(String.valueOf(UStoE));
break;
case "JP/u00A5":
double UStoYen = input * 106.26;
txtOutput.setText(String.valueOf(UStoYen));
break;
case "/u00A3":
double UStoP = input * .71;
txtOutput.setText(String.valueOf(UStoP));
break;
case "AU$":
double UStoAU = input * 1.3;
txtOutput.setText(String.valueOf(UStoAU));
break;
case "CA$":
double UStoCA = input * 1.28;
txtOutput.setText(String.valueOf(UStoCA));
break;
case "SW/u20A3":
double UStoSW = input * .96;
txtOutput.setText(String.valueOf(UStoSW));
break;
case "CH/u00A5":
double UStoCH = input * 6.29;
txtOutput.setText(String.valueOf(UStoCH));
break;
case "/u20B1":
double UStoPE = input * 18.19;
txtOutput.setText(String.valueOf(UStoPE));
break;
case "SEK":
double UStoSEK = input * 8.37;
txtOutput.setText(String.valueOf(UStoSEK));
break;
}
}
public void Euro(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double EtoUS = input * 1.23;
USD(EtoUS, cmbInput1);
}
}
public void JPYen(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double JPtoUS = input * .0094;
USD(JPtoUS, cmbInput1);
}
}
public void Pound(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double PotoUS = input * 1.41;
USD(PotoUS, cmbInput1);
}
}
public void AUD(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double AUtoUS = input * .77;
USD(AUtoUS, cmbInput1);
}
}
public void CAD(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double CAtoUS = input * .78;
USD(CAtoUS, cmbInput1);
}
}
public void SWF(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double SWFtoUS = input * 1.04;
USD(SWFtoUS, cmbInput1);
}
}
public void CHYen(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double CHtoUS = input * .16;
USD(CHtoUS, cmbInput1);
}
}
public void Peso(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double PtoUS = input * .055;
USD(PtoUS, cmbInput1);
}
}
public void SEK(double input, String cmbInput1){
if (cmbInput.getSelectedItem().toString().equals(cmbInput1)){
txtOutput.setText(String.valueOf(input));
} else {
double SEKtoUS = input * 0.12;
USD(SEKtoUS, cmbInput1);
}
}
// Variables declaration - do not modify
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnConvert;
private javax.swing.JComboBox<String> cmbInput;
private javax.swing.JComboBox<String> cmbOutput;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField txtInput;
private javax.swing.JTextField txtOutput;
// End of variables declaration
}
似乎没有办法正确转换双打。当前唯一有效的转换是USD到USD。文本字段保持空白,我也尝试在这些方法中打印行,但没有任何反应。我知道unicode可以用,因为它已经与另一个程序一起测试过。
最佳答案
€的unicode是“ \ u20AC”而不是“ / u20AC”,请改用反斜杠
关于java - Java中的货币换算问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49631811/