简体   繁体   English

如果选择了JCheckbox,如何在JLabel中输出?

[英]How to output in JLabel if a JCheckbox is selected?

I am attempting to create a currency conversion program, but the output is not working through a JLabel. 我正在尝试创建货币换算程序,但是输出无法通过JLabel进行。 I have the program so that there are 6 JCheckboxes, each representing a different currency. 我有一个程序,所以有6个JCheckbox,每个JCheckbox代表一种不同的货币。 When the user presses the "Convert" button, any of the selected JCheckboxes output the result of their specific method (in a different class, I don't think that should need to be posted) into a JLabel. 当用户按下“转换”按钮时,任何选定的JCheckbox都将其特定方法的结果(在另一个类中,我认为不需要将该结果)输出到JLabel中。 For some reason, I have a "The method setText(String) in the type JLabel is not applicable for the arguments (JCheckbox)." 由于某种原因,我有一个“类型为JLabel的方法setText(String)不适用于参数(JCheckbox)。” Here is my GUI class: 这是我的GUI类:

import java.awt.EventQueue;

public class ConverterGUI { 公共类ConverterGUI {

private JFrame frame;
public static JTextField textField;
public static JLabel label1, label2, label3, label4, label5, label6;

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

/**
 * Create the application.
 */
public ConverterGUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    final JCheckBox euros = new JCheckBox("Euros");
    euros.setBounds(50, 50, 97, 23);
    frame.getContentPane().add(euros);

    final JCheckBox pounds = new JCheckBox("Pounds");
    pounds.setBounds(50, 76, 97, 23);
    frame.getContentPane().add(pounds);

    final JCheckBox pesos = new JCheckBox("Mexican Pesos");
    pesos.setBounds(50, 102, 97, 23);
    frame.getContentPane().add(pesos);

    final JCheckBox canadians = new JCheckBox("Canadian Dollar");
    canadians.setBounds(50, 128, 107, 23);
    frame.getContentPane().add(canadians);

    final JCheckBox yen = new JCheckBox("Yen (Japan)");
    yen.setBounds(50, 154, 97, 23);
    frame.getContentPane().add(yen);

    final JCheckBox yuan = new JCheckBox("Yuan (China)");
    yuan.setBounds(50, 180, 97, 23);
    frame.getContentPane().add(yuan);

    JLabel lblInputAmericanDollars = new JLabel("Input American Dollars:");
    lblInputAmericanDollars.setBounds(116, 11, 117, 14);
    frame.getContentPane().add(lblInputAmericanDollars);

    JLabel lblSelectCurrencies = new JLabel("Select currencies:");
    lblSelectCurrencies.setBounds(40, 29, 107, 14);
    frame.getContentPane().add(lblSelectCurrencies);

    textField = new JTextField();
    textField.setBounds(231, 8, 86, 20);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    final JLabel label1 = new JLabel("");
    label1.setBounds(163, 54, 46, 14);
    frame.getContentPane().add(label1);

    final JLabel label2 = new JLabel("");
    label2.setBounds(163, 80, 46, 14);
    frame.getContentPane().add(label2);

    final JLabel label3 = new JLabel("");
    label3.setBounds(163, 106, 46, 14);
    frame.getContentPane().add(label3);

    final JLabel label4 = new JLabel("");
    label4.setBounds(163, 132, 46, 14);
    frame.getContentPane().add(label4);

    final JLabel label5 = new JLabel("");
    label5.setBounds(163, 158, 46, 14);
    frame.getContentPane().add(label5);

    final JLabel label6 = new JLabel("");
    label6.setBounds(163, 184, 46, 14);
    frame.getContentPane().add(label6);

    JButton btnConvert = new JButton("Convert");
    btnConvert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(euros.isSelected() == true){
                Converter.euros();
                label1.setText(euros);
            }
            if(pounds.isSelected() == true){
                Converter.pounds();
                label2.setText(pounds);
            }
            if(pesos.isSelected() == true){
                Converter.pesos();
                label3.setText(pesos);
            }
            if(canadians.isSelected() == true){
                Converter.canadians();
                label4.setText(canadians);
            }
            if(yen.isSelected() == true){
                Converter.yen();
                label5.setText(yen);
            }
            if(yuan.isSelected() == true){
                Converter.yuan();
                label6.setText(yuan);
            }
        }
    });
    btnConvert.setBounds(170, 228, 89, 23);
    frame.getContentPane().add(btnConvert);
}
}

Well, of course it doesn't. 好吧,当然不是。 setText takes a String as parameter, not a JCheckBox, which is what you are trying to pass. setText使用String作为参数,而不是JCheckBox,这就是您要传递的参数。

try this: 尝试这个:

if(yuan.isSelected() == true){
                Converter.yuan();
                label6.setText("yuan");
            }

even better, would be: 更好的是:

if(yuan.isSelected()){
                Converter.yuan();
                label6.setText("yuan");
            }

the '== true' is not necessary, since isSelected() itself already returns a boolean expression. 不需要'== true',因为isSelected()本身已经返回了布尔表达式。

You should return the String from the relative methods of Converter Class and then use the code like this 您应该从Converter类的相关方法返回String,然后使用如下代码

if(yuan.isSelected() == true){

            label6.setText(Converter.yuan());
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM