我正在制作一个简单的货币转换器,它采用 a 中的初始值,JTextField
然后用户单击JCheckBox
与他们想要的货币相对应的值,然后转换后的值将显示在另一个中JTextField
。基本上我想知道是否有任何方法可以为JCheckBox
我环顾四周的检查分配一个值,但找不到明确的答案,任何帮助将不胜感激。
例如,如果当前从英镑到欧元的兑换率为 1.12244,则在选中 JCheckBox 时将分配此值,因此原始值将乘以兑换率。
认为如果您action-listener
为您分配一个JCheckBox
并在此事件触发时进行转换会更容易。要检查是否JCheckBox
已检查,您可以使用该isSelected()
方法
编辑
实际上,我认为您需要为此使用JRadioButton
's ButtonGroup
,就好像您使用的是复选框一样,则有可能选择了多个复选框。这是一个如何使用 ButtonGroup 并在单选按钮上触发操作的示例
这将为您提供复选框的值。
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);
这可能不是最好的解决方案,但你可以试试这个。
您可以按照@Balanivash 的建议使用 JRadioButton 而不是使用 JCheckBox。更简单、更合适的解决方案将是使用 JComboBox。[我和 @Riduidel 在一起。]
// 这是完整的工作代码我希望这会有所帮助
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();
}
}
最简单的做法是这样的:
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)
}
我也认为 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;
}
}
然后,当用户单击复选框时,您将能够进行转换。您还可以在复选框旁边提供标签(美元到欧元)。您还可以在新复选框中提供一种方法来翻转货币并计算另一个方向的乘数。
亲切的问候