组合框按钮文本字段 Java
如何更改此代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从你的问题中得到的是,当你单击按钮时,你需要显示 JTextField 中三个组合框中的选定值。
这是截图
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.
here is the sceenshot