具有ActionListener的多个JComboBox

时间:2019-02-25 05:24:01

标签: jframe jpanel actionlistener jtextfield jcombobox

此代码取自我的原始代码,并出于测试目的进行了修改。

问题:为什么在单击JComboBox之后,我无法单击其他JComboBoxes?

目的:单击JComboBox之后,所选内容将复制到JTextField中。

我已经阅读了StackOverflow上的许多其他帖子,并进行了相应的更改,但它们仍未解决问题。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Test implements ActionListener {

JComboBox[] cb;
JTextField[] text = new JTextField[3];
JFrame frame2;

public static void main(String[] args) {
    Test t = new Test();
    t.changeEntry();
}

private void changeEntry() {

    frame2 = new JFrame();
    frame2.setLayout(new BorderLayout());

    Panel p = new Panel();
    p.setLayout(new GridLayout(3, 3));
    initialize(p);
    JTextField url = new JTextField();
    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
    addTextField(p, 0, url);
    addTextField(p, 1, username);
    addPassField(p, 2, password);

    frame2.add(p, "Center");

    frame2.setTitle("Entries");
    frame2.setVisible(true);
    frame2.setSize(500, 500);
    frame2.setLocation(430, 100);
    frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

private void initialize(Panel p) {

    String[] array1 = {"A"};
    String[] array2 = {"B"};
    String[] array3 = {"C"};

    JComboBox aa = new JComboBox<String>(array1);
    JComboBox bb = new JComboBox<String>(array2);
    JComboBox cc = new JComboBox<String>(array3);
    cb = new JComboBox[3];
    cb[0] = aa;
    cb[0].addActionListener(this);
    cb[0].setActionCommand("A");
    cb[1] = bb;
    cb[1].addActionListener(this);
    cb[1].setActionCommand("B");
    cb[2] = cc;
    cb[2].addActionListener(this);
    cb[2].setActionCommand("C");
    p.add(cb[0]);
    p.add(cb[1]);
    p.add(cb[2]);
}

@Override
public void actionPerformed(ActionEvent e) {
    String s = e.getActionCommand();
    if (s.equals("A")) {
        checkSelection(cb[0], 0);
    } else if (s.equals("B")) {
        checkSelection(cb[1], 1);
    } else if (s.equals("C")) {
        checkSelection(cb[2], 2);
    }
}

private void checkSelection(JComboBox cb, int i) {
    String str = (String) cb.getSelectedItem();
    text[i].setText(str);
}

private void addTextField(Container c, int i, JTextField tf) {
    tf.setText("Edit entry here");
    tf.setEditable(true);
    c.add(tf);
    text[i] = tf;
}

private void addPassField(Container c, int i, JPasswordField pf) {
    pf.setText("test");
    pf.setEditable(true);
    c.add(pf);
    text[i] = pf;
}
}

1 个答案:

答案 0 :(得分:0)

我和我的教授查看了一下代码,发现JComboBoxes不喜欢与JTextFields重叠。这是对使错误消失的代码的修改:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Test implements ActionListener {

JComboBox[] cb;
JTextField[] text = new JTextField[3];
JFrame frame2;

public static void main(String[] args) {
    Test t = new Test();
    t.changeEntry();
}

private void changeEntry() {

    frame2 = new JFrame();
    frame2.setLayout(new BorderLayout());

    Panel p = new Panel();
    p.setLayout(new GridLayout(2, 3));
    JTextField url = new JTextField();
    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
    addTextField(p, 0, url);
    addTextField(p, 1, username);
    addPassField(p, 2, password);
    initialize(p);

    frame2.add(p, "Center");

    frame2.setTitle("Entries");
    frame2.setVisible(true);
    frame2.setSize(500, 500);
    frame2.setLocation(430, 100);
    frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

private void initialize(Panel p) {

    String[] array1 = {"A"};
    String[] array2 = {"B"};
    String[] array3 = {"C"};

    JComboBox aa = new JComboBox<String>(array1);
    JComboBox bb = new JComboBox<String>(array2);
    JComboBox cc = new JComboBox<String>(array3);
    cb = new JComboBox[3];
    cb[0] = aa;
    cb[0].addActionListener(this);
    cb[0].setActionCommand("A");
    cb[1] = bb;
    cb[1].addActionListener(this);
    cb[1].setActionCommand("B");
    cb[2] = cc;
    cb[2].addActionListener(this);
    cb[2].setActionCommand("C");
    p.add(cb[0]);
    p.add(cb[1]);
    p.add(cb[2]);
}

@Override
public void actionPerformed(ActionEvent e) {
    String s = e.getActionCommand();
    if (s.equals("A")) {
        checkSelection(cb[0], 0);
    } else if (s.equals("B")) {
        checkSelection(cb[1], 1);
    } else if (s.equals("C")) {
        checkSelection(cb[2], 2);
    }
}

private void checkSelection(JComboBox cb, int i) {
    String str = (String) cb.getSelectedItem();
    text[i].setText(str);
}

private void addTextField(Container c, int i, JTextField tf) {
    tf.setText("Edit entry here");
    tf.setEditable(true);
    c.add(tf);
    text[i] = tf;
}

private void addPassField(Container c, int i, JPasswordField pf) {
    pf.setText("test");
    pf.setEditable(true);
    c.add(pf);
    text[i] = pf;
}
}

因此,对于有此问题的任何人,请查看我的问题和修改之间的区别:

p.setLayout(new GridLayout(3, 3));
initialize(p);
JTextField url = new JTextField();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
addTextField(p, 0, url);
addTextField(p, 1, username);
addPassField(p, 2, password);

p.setLayout(new GridLayout(2, 3));
JTextField url = new JTextField();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
addTextField(p, 0, url);
addTextField(p, 1, username);
addPassField(p, 2, password);
initialize(p);