import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
class text2 extends JFrame{
public text2() {
// TODO Auto-generated constructor stub
setBounds(400, 100, 400, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container = getContentPane();
container.setLayout(null);
//向下拉列表添加数据
// JComboBox<String> comboBox = new JComboBox<>();
// comboBox.addItem("身份证");
// comboBox.addItem("学生证");
// comboBox.addItem("工作证");
// String string[] = {"1","2","3"};
// JComboBox<String> comboBox = new JComboBox<>(string);
String string[] = {"身份证","学生证","工作证"};
JComboBox<String> comboBox = new JComboBox<>();
ComboBoxModel comboBoxModel = new DefaultComboBoxModel<>(string);//创建下拉列表模型
comboBox.setModel(comboBoxModel);//向列表中添加数据模型
JButton jButton = new JButton("打印");
jButton.setBounds(120, 5, 100, 30);//设置按钮分布大小
container.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println(comboBox.getSelectedIndex());//获取选中的索引
System.out.println(comboBox.getSelectedItem());//获取选中的项
}
});
comboBox.setEditable(true);//设置可以编辑
comboBox.setBounds(10, 10, 100, 21);
container.add(comboBox);
setVisible(true);
}
public static void main(String[] args) {
new text2();
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有