在JTextField中更改值侦听器

时间:2015-03-09 07:56:21

标签: java swing listener jtextfield

在我的代码中,如果用户在ComboBox 2中输入值,它会反映在TextField中,但是 如果用户从Combobox1的下拉列表中选择而不更改该值 在ComboBox 2中,值在Textfield中保持不变,它不会改变。我在做错了吗?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.text.Document;

public class D extends JPanel {
    public D() {

        JPanel buttonPanel = new JPanel();
        add(buttonPanel);
        buttonPanel.setLayout(new GridLayout(0, 3, 5, 5));

        JTextField field11 = new JTextField(15);
        field11.setEditable(false);
        buttonPanel.add(field11);

        JTextField field12 = new JTextField(15);
        field12.setEditable(false);
        buttonPanel.add(field12);

        JComboBox comboBox1 = new JComboBox();
        comboBox1.addItem("1");
        comboBox1.addItem("2");
        comboBox1.addItem("3");

        JComboBox comboBox2 = new JComboBox();
        comboBox2.setEditable(true);

        comboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                JComboBox comboBox1 = (JComboBox) event.getSource();
                JComboBox comboBox2 = (JComboBox) event.getSource();

                Object selected = comboBox1.getSelectedItem();
                if (selected.toString().equals("1")) {
                    field11.setText("10");
                    String cb3a = ((String) comboBox2.getSelectedItem());
                    double cb3b = Double.valueOf(cb3a);
                    String cb3aa = field11.getText();
                    double cb3bb = Double.parseDouble(cb3aa);
                    double cb3c = (cb3b * cb3bb);
                    String cb3d = String.valueOf(cb3c);
                    field12.setText(cb3d);
                   }
                else if (selected.toString().equals("2")) {
                    field11.setText("20");
                    String cb33a = ((String) comboBox2.getSelectedItem());
                    double cb33b = Double.valueOf(cb33a);
                    String cb33aa = field11.getText();
                    double cb33bb = Double.parseDouble(cb33aa);
                    double cb33c = (cb33b * cb33bb);
                    String cb33d = String.valueOf(cb33c);
                    field12.setText(cb33d);
                    }
                else if (selected.toString().equals("3")) {
                    field11.setText("30");
                    String cb333a = ((String) comboBox2.getSelectedItem());
                    double cb333b = Double.valueOf(cb333a);
                    String cb333aa = field11.getText();
                    double cb333bb = Double.parseDouble(cb333aa);
                    double cb333c = (cb333b * cb333bb);
                    String cb333d = String.valueOf(cb333c);
                    field12.setText(cb333d);
                  }

            }
        });


        comboBox2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                JComboBox comboBox2 = ((JComboBox) event.getSource());
                Object selected = comboBox1.getSelectedItem();

                if (selected.toString().equals("1")) {
                    String cb3a = ((String) comboBox2.getSelectedItem());
                    double cb3b = Double.valueOf(cb3a);
                    String cb3aa = field11.getText();
                    double cb3bb = Double.parseDouble(cb3aa);
                    double cb3c = (cb3b * cb3bb);

                    String cb3d = String.valueOf(cb3c);

                    field12.setText(cb3d);
                } else if (selected.toString().equals("2")) {
                    String cb33a = ((String) comboBox2.getSelectedItem());
                    double cb33b = Double.valueOf(cb33a);
                    String cb33aa = field11.getText();
                    double cb33bb = Double.parseDouble(cb33aa);
                    double cb33c = (cb33b * cb33bb);
                    String cb33d = String.valueOf(cb33c);
                    field12.setText(cb33d);
                } else if (selected.toString().equals("3")) {
                    String cb333a = ((String) comboBox2.getSelectedItem());
                    double cb333b = Double.valueOf(cb333a);
                    String cb333aa = field11.getText();
                    double cb333bb = Double.parseDouble(cb333aa);
                    double cb333c = (cb333b * cb333bb);
                    String cb333d = String.valueOf(cb333c);
                    field12.setText(cb333d);
                }

            }
        });

        buttonPanel.add(comboBox1);
        buttonPanel.add(comboBox2);

        try {
            InputStream ips = new FileInputStream("test2.txt");
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String line;
            while ((line = br.readLine()) != null) {
                comboBox1.setSelectedItem(line);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        D app = new D();
        JFrame m = new JFrame("D");
        m.getContentPane().add(app);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        m.pack();
        m.setVisible(true);
    }
}

的test2.txt:         1 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您只在comboBox1&#39的ActionListener中设置field11,没有理由更改field12。更改JComboBox时,只会执行相应的ActionListener,而不是两者都执行。

将计算放在单独的方法中,然后在ActionListener中设置两个字段,以便它们都发生变化。

除此之外,您应该向JComboBox添加一个类型参数,例如: JComboBox<Integer>。这样,你将不得不少投。 GUI应该在EDT上初始化。