JComboBox,ActionListener,我如何真正使用它们?

时间:2022-10-31 10:51:06

Im currently learning java and stuck onto JComboBox. I have a feel things that I am trying out and hitting the wall for the pass 4 hours.

我目前正在学习java并坚持使用JComboBox。我有一种感觉,我正在尝试的东西,并通过墙4小时通过。

I am trying to let a user select 1-10 from a ComboBox. How do I get the value of the combobox? The value of the combo box is equivalent to quantity.

我试图让用户从ComboBox中选择1-10。我如何获得组合框的价值?组合框的值等于数量。

So I have another value which is maybe $10. If the user choose quantity 2.

所以我有另一个值可能是10美元。如果用户选择数量2。

I want to get the value of what the user choose, then take the value of $10 and times it by 2.

我希望获得用户选择的值,然后取10美元的值并将其乘以2。

The result which is $20 will be displayed on the JTextField.

$ 20的结果将显示在JTextField上。

Please help :(

请帮忙 :(

public class Panel extends JPanel {

    public Panel(){
        JPanel test = new JPanel(new GridBagLayout());

        String[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};
        JComboBox quantitiesCB = new JComboBox(quantities1);
        quantitiesCB.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        JComboBox combo = (JComboBox)e.getSource();
                        String currentQuantity = (String)combo.getSelectedItem();
                    }
                }            
        );

        JTextField result = new JTextField();

        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(640,480));
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.NORTH;
        add(quantitiesCB, gbc);
    } 
}

3 个解决方案

#1


3  

Just few changes:

只有一些变化:

public class Panel extends JPanel {

    public Panel(){
        JPanel test = new JPanel(new GridBagLayout());
        String value = "10";
        final JTextField result = new JTextField();

        String[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};
        JComboBox quantitiesCB = new JComboBox(quantities1);
        quantitiesCB.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        JComboBox combo = (JComboBox)e.getSource();
                        String currentQuantity = (String)combo.getSelectedItem();
                        int value1 = Integer.valueOf(value);
                        int value2 = Integer.valueOf(currentQuantity);

                        String resultText = String.valueOf(value1*value2);
                        result.setText("$" + resultText);
                    }
                }            
        );

        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(640,480));
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.NORTH;
        add(quantitiesCB, gbc);
    } 
}

#2


3  

I am guessing that you are looking for a way to convert a String to an Integer. That can be done with Integer.valueOf.

我猜你正在寻找一种将String转换为整数的方法。这可以使用Integer.valueOf完成。

Below is a very small/basic demo code that works (but I don't know exactly what you are looking for):

下面是一个非常小/基本的演示代码(但我不确切知道你在寻找什么):

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Panel extends JPanel {

    private JTextField result;
    private JTextField amount;

    public Panel() {
        setLayout(new GridBagLayout());
        String[] quantities1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
        JLabel dollar = new JLabel("$");
        amount = new JTextField(3);
        JComboBox quantitiesCB = new JComboBox(quantities1);
        quantitiesCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox combo = (JComboBox) e.getSource();
                String currentQuantity = (String) combo.getSelectedItem();
                int a;
                int q;
                try {
                    a = Integer.valueOf(amount.getText());
                    q = Integer.valueOf(currentQuantity);
                    result.setText("$" + String.valueOf(a * q));
                } catch (NumberFormatException e1) {
                    e1.printStackTrace();
                    // Some invalid number
                }
            }
        });
        JLabel equal = new JLabel("=");
        result = new JTextField(5);
        JLabel quantity = new JLabel("Quantity:");
        GridBagConstraints gbc = new GridBagConstraints();
        add(dollar, gbc);
        add(amount, gbc);
        add(quantity, gbc);
        add(quantitiesCB, gbc);
        add(equal, gbc);
        add(result, gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Panel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

#3


1  

You need to make the fixed price and the JTextField final, in order to pass them to your event handler.

您需要将固定价格和JTextField设为final,以便将它们传递给您的事件处理程序。

Besides, why do you use String for integers? You can just use:

此外,为什么使用String作为整数?你可以使用:

final int FixedPrice = 10;
final JTextField result = new JTextField();
int[] quantities1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
JComboBox quantitiesCB = new JComboBox(quantities1);
quantitiesCB.addActionListener(
    new ActionListener(){
        public void actionPerformed(ActionEvent e){
            JComboBox combo = (JComboBox)e.getSource();
            int currentQuantity = (Integer)combo.getSelectedItem();
            result.setText("$" + String.valueOf(currentQuantity * FixedPrice));
        }
    }            
);

#1


3  

Just few changes:

只有一些变化:

public class Panel extends JPanel {

    public Panel(){
        JPanel test = new JPanel(new GridBagLayout());
        String value = "10";
        final JTextField result = new JTextField();

        String[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};
        JComboBox quantitiesCB = new JComboBox(quantities1);
        quantitiesCB.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        JComboBox combo = (JComboBox)e.getSource();
                        String currentQuantity = (String)combo.getSelectedItem();
                        int value1 = Integer.valueOf(value);
                        int value2 = Integer.valueOf(currentQuantity);

                        String resultText = String.valueOf(value1*value2);
                        result.setText("$" + resultText);
                    }
                }            
        );

        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(640,480));
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.NORTH;
        add(quantitiesCB, gbc);
    } 
}

#2


3  

I am guessing that you are looking for a way to convert a String to an Integer. That can be done with Integer.valueOf.

我猜你正在寻找一种将String转换为整数的方法。这可以使用Integer.valueOf完成。

Below is a very small/basic demo code that works (but I don't know exactly what you are looking for):

下面是一个非常小/基本的演示代码(但我不确切知道你在寻找什么):

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Panel extends JPanel {

    private JTextField result;
    private JTextField amount;

    public Panel() {
        setLayout(new GridBagLayout());
        String[] quantities1 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
        JLabel dollar = new JLabel("$");
        amount = new JTextField(3);
        JComboBox quantitiesCB = new JComboBox(quantities1);
        quantitiesCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox combo = (JComboBox) e.getSource();
                String currentQuantity = (String) combo.getSelectedItem();
                int a;
                int q;
                try {
                    a = Integer.valueOf(amount.getText());
                    q = Integer.valueOf(currentQuantity);
                    result.setText("$" + String.valueOf(a * q));
                } catch (NumberFormatException e1) {
                    e1.printStackTrace();
                    // Some invalid number
                }
            }
        });
        JLabel equal = new JLabel("=");
        result = new JTextField(5);
        JLabel quantity = new JLabel("Quantity:");
        GridBagConstraints gbc = new GridBagConstraints();
        add(dollar, gbc);
        add(amount, gbc);
        add(quantity, gbc);
        add(quantitiesCB, gbc);
        add(equal, gbc);
        add(result, gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Panel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

#3


1  

You need to make the fixed price and the JTextField final, in order to pass them to your event handler.

您需要将固定价格和JTextField设为final,以便将它们传递给您的事件处理程序。

Besides, why do you use String for integers? You can just use:

此外,为什么使用String作为整数?你可以使用:

final int FixedPrice = 10;
final JTextField result = new JTextField();
int[] quantities1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
JComboBox quantitiesCB = new JComboBox(quantities1);
quantitiesCB.addActionListener(
    new ActionListener(){
        public void actionPerformed(ActionEvent e){
            JComboBox combo = (JComboBox)e.getSource();
            int currentQuantity = (Integer)combo.getSelectedItem();
            result.setText("$" + String.valueOf(currentQuantity * FixedPrice));
        }
    }            
);