组合框按钮文本字段Java

时间:2011-03-25 06:36:17

标签: java textfield selectlist

如何更改此代码

 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ComboBox {
    private static final ActionListener Event = null;
    JComboBox combo1;
    JComboBox combo2;
    JComboBox combo3;
    JTextField txt;
    Button boton;

    public static void main(String[] args) {
        ComboBox b = new ComboBox();
    }

    public ComboBox() {
        String course1[] = { "India", "Germany", "America", "Russia" };
        String course2[] = { "India", "Germany", "America", "Russia" };
        String course3[] = { "India", "Germany", "America", "Russia" };
        JFrame frame = new JFrame("Creating a JComboBox Component");
        JPanel panel = new JPanel();
        combo1 = new JComboBox(course1);
        combo2 = new JComboBox(course2);
        combo3 = new JComboBox(course3);
        txt = new JTextField(30);
        boton = new Button( "Boton");
        panel.add(combo1);
        panel.add(combo2);
        panel.add(combo3);
        panel.add(txt);
        panel.add(boton);
        frame.add(panel);

        boton.addActionListener(Event);
        combo1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ie) {
                String str1 = (String) combo1.getSelectedItem();
                String str2 = (String) combo2.getSelectedItem();
                String str3 = (String) combo3.getSelectedItem();
                txt.setText(str1+str2+str3);
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

因此,在文本字段中显示每个组合的值的串联的动作是通过按钮完成的吗?

1 个答案:

答案 0 :(得分:1)

我从您的问题中得到的是,当您单击按钮时,您需要显示JTextField中三个组合框中的选定值。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ComboBox {
    private static final ActionListener Event = null;
    JComboBox combo1;
    JComboBox combo2;
    JComboBox combo3;
    JTextField txt;
    Button boton;

    public static void main(String[] args) {
        ComboBox b = new ComboBox();
    }

    public ComboBox() {
        String course1[] = { "India", "Germany", "America", "Russia" };
        String course2[] = { "India", "Germany", "America", "Russia" };
        String course3[] = { "India", "Germany", "America", "Russia" };
        JFrame frame = new JFrame("Creating a JComboBox Component");
        JPanel panel = new JPanel();
        combo1 = new JComboBox(course1);
        combo2 = new JComboBox(course2);
        combo3 = new JComboBox(course3);
        txt = new JTextField(30);
        boton = new Button( "Boton");
        panel.add(combo1);
        panel.add(combo2);
        panel.add(combo3);
        panel.add(txt);
        panel.add(boton);
        frame.add(panel);

        boton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                    String str1 = (String) combo1.getSelectedItem();
                    String str2 = (String) combo2.getSelectedItem();
                    String str3 = (String) combo3.getSelectedItem();
                    txt.setText(str1+str2+str3);

            }
        });
        combo1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ie) {

            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

这是sceenshot enter image description here