有没有办法让我将组合框选择的值添加到1个文本字段中? 即noOfBks。
例如,我有两个组合框,即quantityB1和ebquantitiesCB1。 quantityCB1用于硬拷贝,ebquantitiescb1用于电子书
每个代码都运行正常。 因此,在程序页面的最后,我想要一个文本字段来说明书籍的总数。
我是否可以添加到第一个要执行的ActionListener
totalNoOfBks += currenQuantity;
对于actionlistener quantityCB1和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);
}
}
);
}
}
答案 0 :(得分:4)
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ComboBoxIntegerModel {
private JComboBox comboBoxDouble;
private JComboBox comboBoxInteger;
private JComboBox comboBoxBoolean;
private JComboBox comboBoxIcon;
private Vector<Double> doubleVector = new Vector<Double>();
private Vector<Integer> integerVector = new Vector<Integer>();
private Vector<Boolean> booleanVector = new Vector<Boolean>();
private Vector<Icon> iconVector = new Vector<Icon>();
private Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon")));
private Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon"));
private Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon"));
private Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon"));
public ComboBoxIntegerModel() {
doubleVector.addElement(1.001);
doubleVector.addElement(10.00);
doubleVector.addElement(0.95);
doubleVector.addElement(4.2);
comboBoxDouble = new JComboBox(doubleVector);
integerVector.addElement(1);
integerVector.addElement(2);
integerVector.addElement(3);
integerVector.addElement(4);
comboBoxInteger = new JComboBox(integerVector);
booleanVector.add(Boolean.TRUE);
booleanVector.add(Boolean.FALSE);
comboBoxBoolean = new JComboBox(booleanVector);
iconVector.addElement(icon1);
iconVector.addElement(icon2);
iconVector.addElement(icon3);
iconVector.addElement(icon4);
comboBoxIcon = new JComboBox(iconVector);
JFrame frame = new JFrame("");
frame.setLayout(new GridLayout(2,2,5,5));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(comboBoxDouble);
frame.add(comboBoxInteger);
frame.add(comboBoxBoolean);
frame.add(comboBoxIcon);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ComboBoxIntegerModel cBModel = new ComboBoxIntegerModel();
}
});
}
}
然后你避免从JComboBox
将JFormattedTextField与数字格式化程序一起使用,原因与JComboBox中提到的相同
import java.awt.*;
import java.awt.font.TextAttribute;
import java.math.*;
import java.text.*;
import java.util.Map;
import javax.swing.*;
import javax.swing.JFormattedTextField.*;
import javax.swing.event.*;
import javax.swing.text.InternationalFormatter;
public class DocumentListenerAdapter {
public static void main(String args[]) {
JFrame frame = new JFrame("AbstractTextField Test");
final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
textField1.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.0);
formatter.setMaximum(1000.00);
return formatter;
}
});
final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01));
textField2.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
textField2.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
DocumentEvent.EventType type = documentEvent.getType();
double t1a1 = (((Number) textField2.getValue()).doubleValue());
if (t1a1 > 1000) {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font(attributes));
textField2.setForeground(Color.red);
}
};
SwingUtilities.invokeLater(doRun);
} else {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font("Serif", Font.BOLD, 16));
textField2.setForeground(Color.black);
}
};
SwingUtilities.invokeLater(doRun);
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
private DocumentListenerAdapter() {
}
}