Swing/菜单包含jMenuBar、jMenuItem和jMenu三个类。JMenuBar是放置JMenu的容器。
JMenuItem是AbstractButton的直接子类,它本身又有3个子类。
Ø JMenu
Ø JCheckBoxMenuItem
Ø JRadioButtonMenuItem
JMenuBar是菜单类,JMenu是菜单中的一列,JMenuItem是列中的一项。
JMenuBar类需要另外两个类和接口来辅助其工作,这两个接口是:
SingleSelectionModel :接口跟踪当前选定的菜单。
LookAndFeel :负责绘制菜单栏,并对在其中发生的事件做出响应。
JMenu类 :可以用来做列同时也可以用来做弹出菜单。
JMenuItem组件通常是JMenu或JPopupMenu中的选项的标题。JMenuItem的外观可以修改。
Object |
Component |
Container |
JComponent |
JMenuBar |
JSeparator |
AbstractButton |
JPopupMenu |
JMenu |
JCheckboxMenuItem |
JRadioButtonMenuItem |
JMenuItem |
JPopupMenu类
右键弹出菜单。
构造方法:
public JPopupMenu():此构造方法将创建JPopupMenu。
public JPopupMenu(String label) :此构造方法将创建具有指定标题的JPopupMenu。
方法:
JMenuItem add(JMenuItem menuItem); 将指定菜单项添加至菜单末尾。
JMenuItem add(String s) 使用指定文本新建菜单项,并将其添加至菜单末尾
void show(Component c, int x,int y):在组件c的坐标空间的(x,y)位置显示弹出式菜单
boolean isPopupTrigger() :确定是否将鼠标事件看作是当前平台的弹出式触发事件。
JCheckBoxMenuItem类
这个类是JMenuItem类的子类,它有助于实现复选框,默认情况下可为其设置选定标记或非选定标记。
JRadioButtonMenuItem类
是一个独特的Swing菜单组件,这是因为一次只能选择一个菜单项。但是,与复选框一样,它们也可以有文本字符串和图标,并且无论选定或取消选定都会显示其当前状态。
JCheckBoxMenuItem类和JRadioButtonMenuItem类都添加至JMenuItem.这可在设计的时候完成。这些类的代码与状态变化相关联。因此对这组件做任何改变都会产生ItemEvent事件。
对话框
Java中的对话框是一个容器,它充当父容器的子容器。父容器可以是JFrame和JPanel等。每个对话框都依赖于Frame容器。如果Frame被销毁,则对话框也会被销毁。
JOptionPane类可用于显示对话框。
常用构造方法:
JOptionPane()
JOptionPane(Object message)
JOptionPane(Objec message,int messageType)
JOptionPane(Object message,int messageType,int optionType)
JOptionPane(Object message,int messageType,int optionType,Icon icon)
Object message : 该参数指定在其主要区域应显示的对话框。如果显示字符串,则它作为带指定文本的标签出现。“/n”字符可用于显示多行文本。
int optionType : 用于指定将出现在对话框底部的按钮类型。
int messageType :决定即将显示在对话框中的消息类型。
Icon icon : 待显示的图标。
JOptionPane类可用于显示各种图标。
JOptionPane类最常用的方式是showXxxDialog方法,这个方法分showMessageDialog()和showOptionDialog()。二者最根本的区别在于:showOptionDialog()方法可显示带有各种按钮的自定义对话框(各按钮上写有自定义文本),并可包含标准文本消息或组件集合。showOptionDialog()显示一条消息并得到用户在一组选项中的选择。showMessageDialog()显示一消息并等待用户单击OK。
Statc void showMessageDialog(Component parentComponent , Object message)
此方法显示标题为“消息”的信息/消息对话框
Static void showMessageDialog(Component parentComponent , Object message , String title , int messageType) :此方法显示使用(取决于messageType参数的)默认图标来显示信息的对话框。
Static void showMessageDialog(Component parentComponent , Object message , String title , int messageType , Icon icon) :此方法显示用于显示消息和指定所有参数的对话框
Static int showOptionDialog(Component parentComponent , Object message , String title , int optionType , int messageType , Icon icon , Object[]options , Object initialValue)
此方法显示具有指定图标的对话框,其中初始选项由initialValue参数决定,而选项的类型由optionType参数决定。
JFileChooser :可用于打开按钮;例:fcFile.showOpenDialog(this);