Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有方法将从所有组合框中选择的值添加到1个文本字段中?_Java_Swing_Add_Jtextfield_Jcombobox - Fatal编程技术网

Java 是否有方法将从所有组合框中选择的值添加到1个文本字段中?

Java 是否有方法将从所有组合框中选择的值添加到1个文本字段中?,java,swing,add,jtextfield,jcombobox,Java,Swing,Add,Jtextfield,Jcombobox,有没有办法将combobox choosen的值添加到1个文本字段中? i、 我是诺夫克斯 例如,我有两个组合框,分别是quantitiesCB1和EBQuantiesCB1。 quantitiesCB1用于硬拷贝,EBQuantiesCB1用于电子书 所有这些代码都运行良好。 因此,在程序页面的末尾,我希望有一个文本字段,说明书籍的总数 我是否能够添加到要执行的第一个ActionListener中 totalNoOfBks += currenQuantity; 对于actionlistene

有没有办法将combobox choosen的值添加到1个文本字段中? i、 我是诺夫克斯

例如,我有两个组合框,分别是quantitiesCB1和EBQuantiesCB1。 quantitiesCB1用于硬拷贝,EBQuantiesCB1用于电子书

所有这些代码都运行良好。 因此,在程序页面的末尾,我希望有一个文本字段,说明书籍的总数

我是否能够添加到要执行的第一个ActionListener中

totalNoOfBks += currenQuantity;
对于actionlistener quantitiesCB1和EBQuantiesCB2

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);
                    }
                }            
        );
    }
}
  • 将整数值或双数值直接添加到ComboBoxModel

  • 将整数值或双数值直接添加到ComboBoxModel


@那是很多需要吸收的东西。让我把它通读一遍!谢谢你的帮助!不客气,请阅读我对您的问题的评论,如果是真的,请尽量避免无用coding@mKorbel不,我不是在给收银机编码,我只是在做一些计算。将组合框中的所有选定整数添加到文本字段。我试图理解你给了我什么,但很难。。。我在学习..@mKorbel这是很多需要吸收的东西。让我把它通读一遍!谢谢你的帮助!不客气,请阅读我对您的问题的评论,如果是真的,请尽量避免无用coding@mKorbel不,我不是在给收银机编码,我只是在做一些计算。将组合框中的所有选定整数添加到文本字段。我试图理解你给了我什么,但很难。。。我在学习..你对收银机编码了吗?,(很抱歉,flamewar)然后我认为你走错了方向,使用JTable和JComboBox作为票据(_portal:-),删除边框(或更改),然后你将在一个JComponent上收集所有内容,使用一个模型,在那里你可以添加/删除行,或者严格地硬编码当前货币的所有可用名称您是否对收银机进行编码?,(很抱歉flamewar)然后我认为您走错了方向,使用JTable和JComboBox作为票据(_portal:-),删除边框(或更改),然后您将在一个JComponent上收集所有内容,使用一个模型,您可以在那里添加/删除行,或严格硬编码当前货币的所有可用名称
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();
            }
        });
    }
}
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() {
    }
}