有没有办法让我将选择的组合框的值添加到 1 个文本字段中?即noOfBks。
例如,我有两个组合框,即 quantityCB1 和 ebquantitiesCB1。quantityCB1 用于硬拷贝,ebquantitiescb1 用于电子书
每个代码都运行良好。所以在程序页面的最后,我想有一个文本字段说明书籍的总数。
我可以添加到第一个 ActionListener 中吗
totalNoOfBks += currenQuantity;
对于动作侦听器数量CB1和ebquantitiesCB2?
内部 ebquantitiesCB2 将具有与上述相同的代码,但附加了一个
totalNoOfBks += currenQuantity;
NoOfBks.setText(totalNoOfBks);
下面是我的编码,它工作正常。
public class CataloguePanel extends JPanel {
JPanel catalogue = new JPanel(new GridBagLayout());
//batman cost and fields
String value1 = "15";
String ebvalue1 = "12";
final JTextField result1 = new JTextField();
final JTextField ebresult1 = new JTextField();
//total no. of books field
final JTextField noOfBks = new JTextField();
final int totalBks;
public CataloguePanel(){
JPanel catalogue = new JPanel(new GridBagLayout());
//combobox for batman textfield
JComboBox quantitiesCB1 = new JComboBox(quantities1);
quantitiesCB1.setPreferredSize(new Dimension(125,20));
quantitiesCB1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox combo = (JComboBox)e.getSource();
String currentQuantity = (String)combo.getSelectedItem();
int finalvalue1 = Integer.valueOf(value2);
int finalvalue2 = Integer.valueOf(currentQuantity);
String resultText = String.valueOf(finalvalue1*finalvalue2);
result2.setText("$" + resultText);
}
}
);
JComboBox ebquantitiesCB1 = new JComboBox(quantities1);
ebquantitiesCB1.setPreferredSize(new Dimension(125,20));
ebquantitiesCB1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox combo = (JComboBox)e.getSource();
String currentQuantity = (String)combo.getSelectedItem();
int finalvalue1 = Integer.valueOf(ebvalue2);
int finalvalue2 = Integer.valueOf(currentQuantity);
String resultText = String.valueOf(finalvalue1*finalvalue2);
ebresult2.setText("$" + resultText);
}
}
);
}
}