import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Jcombar extends JFrame {
public Jcombar () {
this.setBounds(0, 0, 500, 500);
this.setTitle("my first title");
//创建面板jpanel;
JPanel jpanel=new JPanel();
//创建下拉式菜单组件对象jb;
JComboBox<String> jb=new JComboBox<>();
//添加项目;
jb.addItem("广西");
jb.addItem("北京");
//把组件加到面板中;
jpanel.add(jb);
//把面板加到窗体中;
this.add(jpanel);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Jcombar j=new Jcombar();//调用无参构造方法
}
}
代码运行结果: