本文实例讲述了Java Swing组件下拉菜单控件JComboBox用法。分享给大家供大家参考,具体如下:
JComboBox是Swing中的下拉菜单控件。它永远只能选中一个项目,然而比单选按钮节省空间。如果使用setEditable设置为true则内部选项的文本可以编辑,因此这种组件被称为组合框。注意,对选项的编辑只会影响当前项,而不会改变列表内容。可以使用addItem方法来添加选项列表,或者使用insertItemAt在任何位置插入选项;然而如果有大量选项需要添加,这种方法是非常笨重的,可以先构造一个DefaultComboBoxModel,使用addElement加载选项,最后在调用JComboBox的setModel方法。
常用方法如下:
示例代码:
JComboBoxBasicUseDemo.java
package awtDemo;import java.awt.BorderLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.event.PopupMenuEvent;import javax.swing.event.PopupMenuListener;public class JComboBoxBasicUseDemo extends JFrame{ private static final long serialVersionUID = -8161981948004677531L; int DEFAULT_WIDTH = 600; int DEFAULT_HEIGHT = 400; private JLabel label; private JComboBox<String> faceCombo; private static final int FONTSIZE = 12; @SuppressWarnings("unused") public JComboBoxBasicUseDemo(){ setTitle("JComboBoxDemo - www.jb51.net"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); //添加label label = new JLabel("欢迎来到编程网 www.jb51.net"); label.setFont(new Font("Serif",Font.PLAIN, FONTSIZE)); add(label, BorderLayout.CENTER); //actionListener ActionListener actionListener = new ActionListener(){ public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JComboBox){ @SuppressWarnings({ "unchecked", "rawtypes" }) JComboBox<String> comboBox = (JComboBox)e.getSource(); String fontName = comboBox.getSelectedItem().toString(); label.setFont(new Font(fontName, Font.PLAIN, FONTSIZE)); System.out.printf("%s%n", "actionPerformed called"); } } }; //popupMenuListener PopupMenuListener popupMenuListener = new PopupMenuListener() { @Override public void popupMenuCanceled(PopupMenuEvent e) { System.out.println("下拉菜单取消"); } @SuppressWarnings("unchecked") @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { System.out.println("下拉菜单合上"); JComboBox<String> source = (JComboBox<String>)e.getSource(); } @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { System.out.println("下拉菜单弹出"); } }; //itemListener ItemListener itemListener = new ItemListener() { @Override public void itemStateChanged(ItemEvent arg0) { // TODO Auto-generated method stub if(ItemEvent.SELECTED == arg0.getStateChange()){ String selectedItem = arg0.getItem().toString(); label.setFont(new Font(selectedItem, Font.PLAIN, FONTSIZE)); System.out.printf("new selected item : %s%n",selectedItem); } if(ItemEvent.DESELECTED == arg0.getStateChange()){ String selectedItem = arg0.getItem().toString(); System.out.printf("deselected item : %s%n",selectedItem); } } }; //添加一个JComboBox faceCombo = new JComboBox<String>(); faceCombo.setEditable(true); //faceCombo.addActionListener(actionListener); //faceCombo.addPopupMenuListener(popupMenuListener); faceCombo.addItemListener(itemListener); faceCombo.setEnabled(true); faceCombo.addItem("Serif"); faceCombo.addItem("SansSerif"); faceCombo.addItem("Monospaced"); faceCombo.addItem("Dialog"); faceCombo.addItem("DialogInput"); add(faceCombo, BorderLayout.SOUTH); } public static void main(String[] args) { // TODO Auto-generated method stub //创建窗体并指定标题 JComboBoxBasicUseDemo frame = new JComboBoxBasicUseDemo(); //关闭窗体后退出程序 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //自动适配所有控件大小 //frame.pack(); //设置窗体位置在屏幕中央 frame.setLocationRelativeTo(null); //显示窗体 frame.setVisible(true); }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容猜你喜欢
AI推送时光机Java Swing组件下拉菜单控件JComboBox用法示例
后端开发2023-05-30
java Swing组件setBounds()简单用法实例分析
后端开发2023-05-30
咦!没有更多了?去看看其它编程学习网 内容吧