简体   繁体   English

更改JTextField中的值侦听器

[英]change value listener in JTextField

In my code if the user enters value in ComboBox 2 it reflects in TextField, but if the user selects from the dropdown in Combobox1 without changing the value in ComboBox 2 the value remains the same in Textfield it does not changes.want am i doing wrong? 在我的代码中,如果用户在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 Any help would be appreciated! test2.txt:1任何帮助将不胜感激!

You are only setting field11 in comboBox1's ActionListener, there is no reason for field12 to change. 您仅在comboBox1的ActionListener中设置field11,没有理由更改field12。 Only the respective ActionListener will get executed when a JComboBox is changed, not both. 更改JComboBox时,将仅执行相应的ActionListener,而不是同时执行。

Put your calculation in a separate method and then set both fields in the ActionListener, so both of them change. 将您的计算放在单独的方法中,然后在ActionListener中设置两个字段,因此它们都将更改。

Apart from that, you should add a type parameter to JComboBox, eg JComboBox<Integer> . 除此之外,您应该向JComboBox添加类型参数,例如JComboBox<Integer> That way, you will have to cast less. 这样,您将需要减少投放。 The GUI should be initialized on the EDT. GUI应该在EDT上初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM