Java Eclipse : ComboBox action button based on item selected (e. g.,温度转换)

标签 java swing combobox

我是Java新手,目前正在使用Java Eclipse进行练习。我在使用组合框时遇到一些麻烦,我不确定如何让程序根据从两个组合框中选择的项目执行某些操作。我有两个用于转换温度的组合框(摄氏度、华氏度、开尔文),并且我设法添加了这些项目。我被困在转换按钮上。我的意图是,如果组合框1 = 摄氏度,并且组合框2 = 华氏度,则使用此计算并将其输出到textField_1 作为华氏度值。您能帮我明确说明如何做到这一点吗?非常感谢您的帮助。太感谢了!我尝试过寻找类似的东西,但我找不到像我的那样的东西,而其他人则使用netbeans并且非常不同。

        JButton btnNewButton = new JButton("Convert");//converts temperature input to output based on the comboBox and comboBox1 selections. 
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {


            String inputTemp = (String)comboBox.getSelectedItem();
            String inputTemp1 = (String)comboBox_1.getSelectedItem();

            double inputTemp, inputTemp1, Celsius, Fahrenheit, Kelvin;
            inputTemp=Double.parseDouble(textField.getText());

            if (inputTemp.equals("Celsius") && inputTemp1.equals("Fahrenheit")) {
                Celsius = inputTemp;
            Fahrenheit = (Celsius*1.8)+32;
            textField_1.setText(Double.toString(Fahrenheit));

        }else{JOptionPane.showMessageDialog(null, "Invalid value.");
            }

        }
    });
    btnNewButton.setFont(new Font("Times New Roman", Font.PLAIN, 13));
    btnNewButton.setBounds(335, 64, 89, 103);
    frame.getContentPane().add(btnNewButton);

    textField_1 = new JTextField();
    textField_1.setBounds(41, 129, 147, 38);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

    JComboBox comboBox = new JComboBox();
    comboBox.setBounds(198, 67, 127, 38);
    frame.getContentPane().add(comboBox);

    comboBox.addItem("Celsius");
    comboBox.addItem("Fahrenheit");
    comboBox.addItem("Kelvin");

    JComboBox comboBox_1 = new JComboBox();
    comboBox_1.setBounds(198, 129, 127, 38);
    frame.getContentPane().add(comboBox_1);

    comboBox_1.addItem("Celsius");
    comboBox_1.addItem("Fahrenheit");
    comboBox_1.addItem("Kelvin");

最佳答案

那么,您应该首先创建一个 Actionlistener。在此 ActionListener 对象中,您可以创建很多 if,它们会给出您搜索的结果。

首先是您的导入。

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

然后制作框架。

public class StringAnswers extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(400, 280);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Colored Animals");
        frame.setContentPane(new MyPanel());
        frame.setVisible(true);
        frame.getContentPane().setBackground(Color.WHITE);
    }

您的面板。

    class MyPanel extends JPanel {
        private JComboBox fromCombo, toCombo;
        private JButton convertBtn;
        private JLabel fromLabel, toLabel;
        private JTextField valueField;

        public MyPanel() {
            convertBtn = new JButton("OK");
            convertBtn.addActionListener(new ConvertHandler());
            add(convertBtn);

            fromLabel = new JLabel();
            add(fromLabel);
            toLabel = new JLabel();
            add(toLabel);

            valueField = new JTextField(10);
            add(valueField);

创建一个数组,以便轻松地将其放入组合框中。布局。

            String[] entries = { "Celsius", "Fahrenheit", "Kelvin" };
            Font displayFont = new Font("Serif", Font.PLAIN, 21);

            fromCombo = new JComboBox(entries);
            fromCombo.setFont(displayFont);
            fromCombo.setVisible(true);
            add(fromCombo);

第二个组合框。

            toCombo = new JComboBox(entries);
            toCombo.setFont(displayFont);
            toCombo.setVisible(true);
            add(toCombo);
        }

按钮处理程序。因此,当按下按钮时,就会发生此操作。

        class ConvertHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                String temperature = (String) fromCombo.getSelectedItem();
                String convertTemp = (String) toCombo.getSelectedItem();

如果在第一个框中选择了摄氏度。

                if (temperature.equals("Celsius")) {

如果在第二个框中选择了华氏度。

                    if (convertTemp.equals("Fahrenheit")) {

然后这个 Action 就会发生。

                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = value * 1.8 + 32;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }

对于每种可能的组合,这种情况都会持续一段时间。

                    if (convertTemp.equals("Kelvin")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = value + 273;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                    if (convertTemp.equals("Celsius")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = value;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                }
                if (temperature.equals("Fahrenheit")) {
                    if (convertTemp.equals("Fahrenheit")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = value;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                    if (convertTemp.equals("Kelvin")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = (value + 459.67) * 5/9;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                    if (convertTemp.equals("Celsius")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = (value - 32) * 5/9;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                }
                if (temperature.equals("Kelvin")) {
                    if (convertTemp.equals("Fahrenheit")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = (value * (9/5)) - 459.67;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                    if (convertTemp.equals("Kelvin")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = value;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                    if (convertTemp.equals("Celsius")) {
                        String valueStr = valuefield.getText();
                        fromLabel.setText(valueStr);
                        double value = Double.parseDouble(valueStr);
                        double conv = value - 273;
                        String convStr = Double.toString(conv);
                        toLabel.setText(convStr);
                    }
                }
            }
        }
    }
}

我已经用很多 if 来做到这一点,可能还有更短的方法,但这是我的方法。我希望它对您有所帮助。

关于Java Eclipse : ComboBox action button based on item selected (e. g.,温度转换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35690533/

相关文章:

Java:帮助我将工作 .jsp 转换为某种 XML 表示法

java - 将集合映射到所有组合的列表中

java - 检索 JTabbedpane 中 Textarea 的内容

java - 如何让按钮按下某个键?

c# - 将多个 ComboBox 绑定(bind)到单个列表 - 问题 : When I select an item, 所有组合框更改

python - 类型错误 : action() takes 1 positional argument but 2 were given

c# - 2 个 ObservableCollections 1 个组合框 WPF

java - JNI 调用返回对象的 Java 函数导致 NoSuchMethodError

java - 使用 GridBagLayout 时组件重叠

Java 用不重复的记录填充数组