组合框按钮文本字段 Java

发布于 2024-10-26 01:34:35 字数 1670 浏览 0 评论 0原文

如何更改此代码

 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);
    }
}

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

How to changue this code

 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);
    }
}

So the action of displaying in a textfield the concatenation of the values of each combo is done by a button??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-11-02 01:34:35

我从你的问题中得到的是,当你单击按钮时,你需要显示 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);
    }
}

这是截图
在此处输入图像描述

what i got from your question is that when you click on the button you need to display the selected value from the three combo-boxes in a 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);
    }
}

here is the sceenshot
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文