我在我的GUI的类构造函数中声明了JComboBox对象。我试图做到这一点,因此,当在GUI上按下按钮时,会激活actionPerformed()方法。在该方法中,我试图基于2个JComboBox中的选择进行计算,因此我必须访问每个comboBox中选择的项目。我打算使用(JComboBox var).getSelectedItem()。toString();在我的actionPerformed()方法中调用,但是即使我已经在构造函数中声明了JComboBox变量,也收到“找不到符号”错误。我将代码粘贴到这里:
private JButton calcButton;
private JComboBox listInput;
private JComboBox listOutput;
private JLabel amountLabel;
private JLabel inputTypeLabel;
private JLabel outputTypeLabel;
private JLabel outputLabel;
private JFormattedTextField amountField;
private JTextField outputField;
/* Constructor creates GUI components and adds GUI components
using a GridBagLayout. */
public ConverterFrame() {
// Used to specify GUI component layout
GridBagConstraints layoutConst = null;
// Set frame's title
setTitle("Currency Converter");
// Create labels
amountLabel = new JLabel("Amount to be converted: ");
inputTypeLabel = new JLabel("Select input currency:");
outputTypeLabel = new JLabel("Select output currency:");
outputLabel = new JLabel("Output amount");
// Create button and add action listener
calcButton = new JButton("Convert");
calcButton.addActionListener(this);
// Create JComboBoxs
JComboBox comboBoxInput = new JComboBox();
comboBoxInput.addItem("USD");
comboBoxInput.addItem("EUR");
comboBoxInput.addItem("GBP");
JComboBox comboBoxOutput = new JComboBox();
comboBoxOutput.addItem("USD");
comboBoxOutput.addItem("EUR");
comboBoxOutput.addItem("GBP");
// Create and set-up an input field for numbers (not text)
amountField = new JFormattedTextField(NumberFormat.getNumberInstance());
amountField.setEditable(true);
amountField.setText("0");
amountField.setColumns(15); // Initial width of 10 units
// Create output field for converted dollars
outputField = new JTextField(15);
outputField.setEditable(false);
// Use a GridBagLayout
setLayout(new GridBagLayout());
// Specify component's grid location
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 10, 1);
layoutConst.gridx = 0;
layoutConst.gridy = 0;
add(amountLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 1, 10, 10);
layoutConst.gridx = 1;
layoutConst.gridy = 0;
add(amountField, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 5, 10, 10);
layoutConst.gridx = 2;
layoutConst.gridy = 0;
add(calcButton, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 0, 5, 10);
layoutConst.gridx = 0;
layoutConst.gridy = 1;
add(inputTypeLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(1, 0, 10, 10);
layoutConst.gridx = 0;
layoutConst.gridy = 2;
add(comboBoxInput, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 0, 5, 10);
layoutConst.gridx = 1;
layoutConst.gridy = 1;
add(outputTypeLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(1, 0, 10, 10);
layoutConst.gridx = 1;
layoutConst.gridy = 2;
add(comboBoxOutput, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 1, 10, 0);
layoutConst.gridx = 0;
layoutConst.gridy = 3;
add(outputLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 0, 10, 10);
layoutConst.gridx = 1;
layoutConst.gridy = 3;
add(outputField, layoutConst);
}
/* Method is automatically called when an event
occurs (e.g, Enter key is pressed) */
@Override
public void actionPerformed(ActionEvent event)
{
double initialAmount;
double output;
double exchangeRate;
String inputType;
String outputType;
initialAmount = ((Number) amountField.getValue()).doubleValue();
inputType = comboBoxInput.getSelectedItem().toString();
outputType = comboBoxOutput.getSelectedItem().toString();
if (inputType.equals(outputType))
{
exchangeRate = 1.0;
}
else if (inputType.equals("USD"))
{
if (outputType.equals("EUR"))
exchangeRate = .704225352;
else if (outputType.equals("GBP"))
exchangeRate = .609756097;
}
else if (inputType.equals("EUR"))
{
if (outputType.equals("USD"))
exchangeRate = 1.42;
else if (outputType.equals("GBP"))
exchangeRate = .884955752;
}
else if (inputType.equals("GBP"))
{
if (outputType.equals("USD"))
exchangeRate = 1.64;
else if (outputType.equals("EUR"))
exchangeRate = 1.13;
}
outputField.setText((initialAmount * exchangeRate) + " " + outputType);
}