Объявлен JComboBox в конструкторе класса, но он не будет перенесен в метод actionPerformed (Event action) - PullRequest
0 голосов
/ 31 октября 2018

Я объявил мои объекты JComboBox в конструкторе моего класса для моего GUI. Я пытаюсь сделать так, чтобы при нажатии кнопки в графическом интерфейсе активировался метод actionPerformed (). В этом методе я пытаюсь сделать расчет на основе выбора в 2 JComboBoxes, поэтому я должен получить доступ к элементу, выбранному в каждом 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...