如何为选中的 JChechbox 赋值?

发布于 2024-11-16 14:54:11 字数 257 浏览 3 评论 0原文

我正在制作一个简单的货币转换器,它采用 JTextField 中的初始值,然后用户单击与所需货币相对应的 JCheckBox ,转换后的值将显示在另一个中JTextField。基本上我想知道是否有任何方法可以为已检查的 JCheckBox 分配一个值,我已经环顾四周,但找不到明确的答案,任何帮助将不胜感激。

例如,如果当前从英镑到欧元的兑换率为 1.12244,则在选中 JCheckBox 时将分配该值,因此原始值将乘以兑换率。

I am making a simple currency convertor which takes the initial value in a JTextField then the user clicks the JCheckBox corresponding to their desired currency, the converted value will then be displayed in another JTextField. Basically i would like to know is there any way of assigning a value to a checked JCheckBoxi have looked around and cannot find a clear answer to this, any help would be appreciated.

For example if the current conversion rate from Sterling to euro is 1.12244 this value would be assigned when the JCheckBox is checked, so the the original value would be multiplied by the conversion rate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

难以启齿的温柔 2024-11-23 14:54:11

认为如果您为 JCheckBox 分配一个 action-listener 并在触发此事件时进行转换,会更容易。要检查 JCheckBox 是否已选中,您可以使用 isSelected() 方法

EDIT

实际上我认为您需要使用 JRadioButton 位于 ButtonGroup 中,就好像您使用的是复选框一样,有可能会选择多个复选框。 这里是如何使用 ButtonGroup 和触发操作的示例在单选按钮上

Think it'd be easier if you assign an action-listener to your JCheckBox and make the conversion on trigger of this event. To check is a JCheckBox is checked or not you can use the isSelected() method

EDIT

Actually i think you need to use JRadioButton's in a ButtonGroup for this, as if you are using a checkbox then there is a chance that more than one is selected. Here is an example of how to do use ButtonGroup and trigger action on the radio button

冷情妓 2024-11-23 14:54:11

这将为您提供复选框的值。

JCheckBox cb = ...;

// Determine status
boolean isSel = cb.isSelected();
if (isSel) {
    // The checkbox is now selected
} else {
    // The checkbox is now deselected
}

您可以更改 JCheckBox 的操作侦听器上的值

// Create an action
Action action = new AbstractAction("CheckBox Label") {
    // This method is called when the button is pressed
    public void actionPerformed(ActionEvent evt) {
        // Perform action
        JCheckBox cb = (JCheckBox)evt.getSource();

        // Determine status
        boolean isSel = cb.isSelected();
        if (isSel) {
            // The checkbox is now selected
        } else {
            // The checkbox is now deselected
        }
    }
};

// Create the checkbox
JCheckBox checkBox = new JCheckBox(action);

This would give you the value of the check box.

JCheckBox cb = ...;

// Determine status
boolean isSel = cb.isSelected();
if (isSel) {
    // The checkbox is now selected
} else {
    // The checkbox is now deselected
}

You can change the value on the action-listener of the JCheckBox

// Create an action
Action action = new AbstractAction("CheckBox Label") {
    // This method is called when the button is pressed
    public void actionPerformed(ActionEvent evt) {
        // Perform action
        JCheckBox cb = (JCheckBox)evt.getSource();

        // Determine status
        boolean isSel = cb.isSelected();
        if (isSel) {
            // The checkbox is now selected
        } else {
            // The checkbox is now deselected
        }
    }
};

// Create the checkbox
JCheckBox checkBox = new JCheckBox(action);
╭ゆ眷念 2024-11-23 14:54:11

// 这是完整的工作代码我希望这会有所帮助在此处输入图像描述

public class CConvertor extends JFrame {        

    private  JLabel result;
    private  JCheckBox pk;
    private  JCheckBox ch;

    public CConvertor(){

        result = new JLabel();
        ch = new JCheckBox();
        pk = new JCheckBox();
        init();
    }

    public void init(){
        setTitle("JCheckBox Test");
        getContentPane().setLayout(new FlowLayout());
        add(result);
        add(new JLabel("        "));        
        add(new JLabel(" China "));
        add(ch);
        add(new JLabel(" Pakistan "));
        add(pk);
        setSize(400,80);
        pk.addActionListener(new ActionListener() {         

            public void actionPerformed(ActionEvent e) {
                ch.setSelected(false);
                result.setText("Pakistan selected");

            }
        });
        ch.addActionListener(new ActionListener() {         
            public void actionPerformed(ActionEvent e) {
                pk.setSelected(false);
                result.setText("China is Selected");

            }
        });

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
    new CConvertor();

    }

}

// this is whole working code i hope this this will helpenter image description here

public class CConvertor extends JFrame {        

    private  JLabel result;
    private  JCheckBox pk;
    private  JCheckBox ch;

    public CConvertor(){

        result = new JLabel();
        ch = new JCheckBox();
        pk = new JCheckBox();
        init();
    }

    public void init(){
        setTitle("JCheckBox Test");
        getContentPane().setLayout(new FlowLayout());
        add(result);
        add(new JLabel("        "));        
        add(new JLabel(" China "));
        add(ch);
        add(new JLabel(" Pakistan "));
        add(pk);
        setSize(400,80);
        pk.addActionListener(new ActionListener() {         

            public void actionPerformed(ActionEvent e) {
                ch.setSelected(false);
                result.setText("Pakistan selected");

            }
        });
        ch.addActionListener(new ActionListener() {         
            public void actionPerformed(ActionEvent e) {
                pk.setSelected(false);
                result.setText("China is Selected");

            }
        });

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
    new CConvertor();

    }

}
酸甜透明夹心 2024-11-23 14:54:11

这可能不是最好的解决方案,但您可以尝试一下。

  1. 获取 JCheckBox 数组。
  2. 确保一次仅选中一个复选框。
  3. 获取货币换算值的数组。
  4. 现在根据所选复选框的索引从数组中获取货币转换值。

您可以按照 @Balanivash 的建议使用 JRadioButton,而不是使用 JCheckBox。更简单和正确的解决方案将使用 JComboBox。[我在这方面与 @Riduidel 在一起。]

This could not be the best solution but you can try this.

  1. Take an array of JCheckBox.
  2. Make sure only one checkbox gets selected at a time.
  3. Take an array of currency conversion value.
  4. Now based on the index of the selected checkbox get the currency conversion value from array.

Instead of using JCheckBox you can use JRadioButton as suggested by @Balanivash. More simpler and proper solution will be using JComboBox.[I'm with @Riduidel in this.]

听,心雨的声音 2024-11-23 14:54:11

我还认为 JCheckBox 不是执行您想要的操作的最佳选择,但是...

为什么不将 JCheckBox 类扩展为CurrencyConverterCheckBox,您可以在其中
传递货币和当前转换值作为参数。
例如:

    public class CurrencyConverterCheckBox extends JCheckBox {
       private String from;
       private String to;
       private double value;

       public CurrencyConverterCheckBox(String from, String to, double value) {
           super();
           this.from = from;
           this.to = to;
           this.value = value;
       }
    }

那么当用户单击复选框时,您将能够进行转换。您还可以在复选框旁边提供标签(美元到欧元)。您还可以在新复选框中提供一种方法来翻转货币并计算另一个方向的乘数。

亲切的问候

I also think that JCheckBox isn't the best option to do what you want, however...

Why don't you extend the JCheckBox class to a CurrencyConverterCheckBox where you can
pass as arguments the currencies and the current value of the conversion.
e.g.:

    public class CurrencyConverterCheckBox extends JCheckBox {
       private String from;
       private String to;
       private double value;

       public CurrencyConverterCheckBox(String from, String to, double value) {
           super();
           this.from = from;
           this.to = to;
           this.value = value;
       }
    }

Then you will be able to do the conversion when the user clicks the checkbox. You can also provide labels next to the checkboxes (USD to EUR). And you can also provide a method in your new checkbox to flip the currencies and calculate the multiplication factor in the other direction.

kind regards

勿挽旧人 2024-11-23 14:54:11

最简单的做法是这样的:

String[] ccys = {"USD", "EUR", "CHF", "JPY"};

public void initUI(){
 ...

 ButtonGroup grp = new ButtonGroup();

 for(String ccy : ccys){
  JCheckBox cb = new JCheckBox(ccy);
  cb.setActionCommand(ccy);
  cb.addActionListener(this);
  grp.add(cb);

  ...(add CheckBox to ui)
 }
}


private double getRate(String ccy){
  ...(retrieve the current conversion rate, f.ex from a map)
}

public void actionPerformed(ActionEvent evt){
  Double rate = getRate(evt.getActionCommand());
  ...(calculation, display)
}

What it's easiest to do is something like this:

String[] ccys = {"USD", "EUR", "CHF", "JPY"};

public void initUI(){
 ...

 ButtonGroup grp = new ButtonGroup();

 for(String ccy : ccys){
  JCheckBox cb = new JCheckBox(ccy);
  cb.setActionCommand(ccy);
  cb.addActionListener(this);
  grp.add(cb);

  ...(add CheckBox to ui)
 }
}


private double getRate(String ccy){
  ...(retrieve the current conversion rate, f.ex from a map)
}

public void actionPerformed(ActionEvent evt){
  Double rate = getRate(evt.getActionCommand());
  ...(calculation, display)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文