本文实例讲述了Java Swing组件下拉菜单控件JComboBox用法。分享给大家供大家参考,具体如下:JComboBox是Swing中的下拉菜单控件。它永远只能选中一个项目,然而比单选按钮节省空间。如果使用setEditable设置为t
本文实例讲述了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); }}
--结束END--
本文标题: Java Swing组件下拉菜单控件JComboBox用法示例
本文链接: https://www.lsjlt.com/news/221478.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0