无法将值引用到radiobutton

时间:2016-11-23 13:36:19

标签: java swing

我使用radiobutton编写转换温度代码,但是当我使用actionListener将值打印到标签时,它显示“无法引用封闭范围中定义的非最终局部变量valueto”

有人可以向我展示问题以及如何解决问题吗? (也很抱歉我的英文)

Radiobutton

JRadioButton Cbutton = new JRadioButton("Celsius");
    Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
    Fbutton.setBounds(10, 40, 109, 23);
    contentPane.add(Cbutton);



JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
    Fbutton2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            label.setText(String.valueOf(valueto));
        }
    });

我用它来转换

if(Fbutton.isSelected()&&Cbutton2.isSelected()){
        value=F;
        valueto=(F-32)/1.8;
    }else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
        value=F;
        valueto=(F+459.67)*5/9;
    }else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
        value=C;
        valueto=C*1.8+32;
    }else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
        value=C;
        valueto=C+273.15;
    }else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
        value=K;
        valueto=K-273.15;
    }else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
        value=K;
        valueto=K*9.5-459.67;
    }

编辑:我在类的底部声明了valueto

double value = 0;
double valueto = 0;
double F = 0, C = 0, K = 0;

它要求我将final添加到“double valueto”,但是当我添加它时,“if”中的valueto会变成错误

整个代码

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class CtoKtoF extends JFrame {

private JPanel contentPane;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CtoKtoF frame = new CtoKtoF();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 * @return 
 */

public CtoKtoF() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 303);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    double value = 0 ;
    double valueto = 0;
    double F = 0, C = 0, K = 0;


    Border border = LineBorder.createGrayLineBorder();//make border for Jlabel
    final JLabel label = new JLabel("New label");
    label.setFont(new Font("Tahoma", Font.PLAIN, 12));
    label.setBounds(10, 228, 220, 22);
    label.setBorder(border);
    contentPane.add(label);

    JLabel lblNewLabel = new JLabel("Convert from");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblNewLabel.setBounds(10, 11, 92, 22);
    contentPane.add(lblNewLabel);

    JRadioButton Fbutton = new JRadioButton("Fahrenheit");
    Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
    Fbutton.setBounds(10, 40, 109, 23);
    //Fbutton.setActionCommand("F");
    contentPane.add(Fbutton);

    JRadioButton Cbutton = new JRadioButton("Celcius");
    Cbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
    Cbutton.setBounds(121, 40, 109, 23);
    //Cbutton.setActionCommand("C");
    contentPane.add(Cbutton);

    JRadioButton Kbutton = new JRadioButton("Kelvin");
    Kbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
    Kbutton.setBounds(232, 40, 109, 23);
    //Kbutton.setActionCommand("K");
    contentPane.add(Kbutton);

    JLabel lblEnterNumericTemperature = new JLabel("Enter Numeric Temperature");
    lblEnterNumericTemperature.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblEnterNumericTemperature.setBounds(10, 70, 238, 22);
    contentPane.add(lblEnterNumericTemperature);

    textField = new JTextField();
    textField.setFont(new Font("Tahoma", Font.PLAIN, 12));
    textField.setBounds(10, 103, 220, 22);
    textField.setText(String.valueOf(value));
    contentPane.add(textField);
    textField.setColumns(10);

    JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
    Fbutton2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            label.setText(String.valueOf(valueto));
        }
    });
    Fbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
    Fbutton2.setBounds(10, 165, 109, 23);
    //Fbutton2.setActionCommand("F2");
    contentPane.add(Fbutton2);

    JRadioButton Cbutton2 = new JRadioButton("Celcius");
    Cbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
    Cbutton2.setBounds(121, 165, 109, 23);
    //Cbutton2.setActionCommand("C2");
    contentPane.add(Cbutton2);

    JRadioButton Kbutton2 = new JRadioButton("Kelvin");
    Kbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
    Kbutton2.setBounds(232, 165, 109, 23);
    //Kbutton2.setActionCommand("K2");
    contentPane.add(Kbutton2);

    ButtonGroup group1 = new ButtonGroup();
    group1.add(Fbutton);
    group1.add(Kbutton);
    group1.add(Cbutton);

    ButtonGroup group2 = new ButtonGroup();
    group2.add(Fbutton2);
    group2.add(Cbutton2);
    group2.add(Kbutton2);

    /*Fbutton.addActionListener(this);
    Cbutton.addActionListener(this);
    Kbutton.addActionListener(this);
    Fbutton2.addActionListener(this);
    Cbutton2.addActionListener(this);
    Kbutton2.addActionListener(this);*/

    //boolean checked = Fbutton.getState();

    JLabel lblConvertTo = new JLabel("Convert to");
    lblConvertTo.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblConvertTo.setBounds(10, 136, 92, 22);
    contentPane.add(lblConvertTo);

    JLabel lblComparableTemperatureIs = new JLabel("Comparable Temperature is");
    lblComparableTemperatureIs.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblComparableTemperatureIs.setBounds(10, 195, 238, 22);
    contentPane.add(lblComparableTemperatureIs);


    if(Fbutton.isSelected()&&Cbutton2.isSelected()){
        value=F;
        valueto=(F-32)/1.8;
    }else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
        value=F;
        valueto=(F+459.67)*5/9;
    }else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
        value=C;
        valueto=C*1.8+32;
    }else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
        value=C;
        valueto=C+273.15;
    }else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
        value=K;
        valueto=K-273.15;
    }else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
        value=K;
        valueto=K*9.5-459.67;
    }


}

}

0 个答案:

没有答案