package rxxj;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ComboBoxDemo extends JFrame{
public ComboBoxDemo(){
setBounds(0,0,500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container = getContentPane();
container.setLayout(null);
String[] items = new String[]{"剑圣","剑姬","剑豪"};
ComboBoxModel model = new DefaultComboBoxModel(items);
JComboBox<String> comboBox = new JComboBox<>();
comboBox.setModel(model); //设置下拉选的内容
comboBox.addItem("今天"); //添加下拉选的内容
comboBox.addItem("昨天");
comboBox.addItem("明天");
comboBox.setBounds(0,0,100,20);
comboBox.setEditable(true);//下拉选可以编辑
container.add(comboBox);
setVisible(true);
JButton btn = new JButton("打印");
btn.setBounds(150,0,100,20);
container.add(btn);
JLabel jLabel = new JLabel("输出内容");
jLabel.setBounds(0,50,100,20);
container.add(jLabel);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.err.println(comboBox.getSelectedItem());//打印选中的内容
jLabel.setText((String) comboBox.getSelectedItem());
}
});
}
public static void main(String[] args) {
new ComboBoxDemo();
}
}
图形 下拉框 ComboBox
最新推荐文章于 2024-03-25 10:09:25 发布