Java基础学习笔记 第二部分 part 3

边界类(可以在任何JComponent类的对象上设置边界,不过通常用于组合相关用户界面组件集的JPanel上设置标题边界)
Border是所有类型边界的接口
注意:所有的边界类和接口都组合在程序包javax.swing.border内
边界和图标可以共享
——TitleBorder类
常用属性:

*                title指定边界的标题、
*                titleColor指定标题的颜色
*                titleFont指定标题的字体
*                titleJustification为左中右标题对齐指定 Border.LEFT ,Border.CENTER或Border.RIGHT
*                titlePosition是六个值(Border.ABOVE_TOP ,Border.TOP ,Border.BELOW_TOP ,Border.ABOVE_BOTTOM ,Border.BOTTOM ,Border.BELOW_BOTTOM)

                                                          上边界线之上            上边界线上          下边界线上      上边界线下                           下边界线上             下边界线下

*                border是TitleBorder中用于创建复合边界的属性,在边界内还可以有边界

——BevelBorder类(是可以凹入或凸出的3D外观的边界)
构造函数:public BevelBorder(int bevelType) bevelType类型(BevelBorder.LOWERED或BevelBorder.RAISED)
——EtchedBorder类(是可以刻入或刻出的蚀刻边界)
属性:etchType 有两个取值LOWERED和RAISED
构造函数:可以使用它的默认构造函数构造带有凹入边界的EtchedBorder
——LineBorder类(是任意粗细的单色线条边界)
构造函数:public LineBorder(Color c,int thickness)
——MatteBorder类(是衬有图标图像的不光滑边界)
构造函数:public MatteBorder(Icon titleIcon)
——EmptyBorder类(是有边界空间,但是没有内容的边界)
构造函数:public EmptyBorder(int top,int left,int bottom,int right)
——BorderFactory类(在javax.swing.BorderFactory中,它包含创建边界的静态方法)
静态方法:
public static TitledBorder createTitledBorder(String title)
public static Border createLoweredBevelBorder()
public static Border createRaisedBevelBorder()
public static Border createLineBorder(Color color)
public static Border createLineBorder(Color color,int thickness)
public static Border createEtchedBorder()
public static Border createEtchedBorder(Color highlight,Color shadow)
public static Border createEmptyBorder()
public static Border createEmptyBorder(int top,int left,int bottom,int right)
public static MatteBorder createMatteBorder(int top,int left,int bottom,int right,Icon titleIcon)
public static Border createCompoundBorder(Border outsideBorder,Border insideBorder)
eg.创建蚀刻边界: Border border=BorderFactory.createEtchedBorder();


JOptionPane类
——消息对话框 MessageDialog显示消息并等待用户单击OK
创建方法:
public static void showMessageDialog(Component parentComponent,Object message)
public static void showMessageDialog(Component parentComponent,Object message,String title,int messageType)
public static void showMessageDialog(Component parentComponent,Object message,String title,int messageType ,Icon icon)

      parentComponent可以是任意组件或者为空,message是对象,但是通常使用字符串,这两个参数必须指定。
      title对话框的题目
      messageType是以下常量中的一个:
           JOptionPane.ERROR_MESSAGE
           JOptionPane.INFORMATION_MESSAGE
           JOptionPane.PLAIN_MESSAGE
           JOptionPane.WARNING_MESSAGE
           JOptionPane.QUESTION_MESSAGE
           默认情况下,msssageType为第二个,除第三个,其他都有对应的图标,也可以在icon参数中提供自己的图标
      参数message是对象,如果为GUI组件,显示该组件,如果为非GUI组件,显示代表该对象的字符串

——确认对话框 ConfirmDialog显示问题并请求如OK或Cancel等确认信息
创建方法:
public static int showConfirmDialog(Component parentComponent,Object message)
public static int showConfirmDialog(Component parentComponent,Object message,String title,int optionType)
public static int showConfirmDialog(Component parentComponent,Object message,String title,int optionType,int messageType)
public static int showConfirmDialog(Component parentComponent,Object message,String title,int optionType,int messageType,Icon icon)

      optionType可能的值:
           JOptionPane.YES_NO_OPTION
           JOptionPane.YES_NO_CANCEL_OPTION
           JOptionPane.OK_CANCEL_OPTION

——输入对话框 InputDialog显示问题并从文本域,组合框或列表获得用户的输入
可选值可以列入数组,其中一个可以指定为初始选择值。如果在创建输入对话框时,没有指定可选值,就使用文本域接受输入。
如果指定了少于20个的选择值,那么,在输入对话框内显示组合框。如果指定了20个或以上的选择值,那么,在输入对话框内使用列表
创建方法:
public static String showInputDialog(Object message)
public static String showInputDialog(Component parentComponent,Object message)
public static String showInputDialog(Component parentComponent,Object message,String title,int messageType)
public static String showInputDialog(Component parentComponent,Object message,int messageType,Icon icon,Object[] selectionValues,Object initialSelectionValue )
前三种使用文本输入,返回输入的字符串
——选项对话框 OptionDialog显示问题并从一组选项中获得用户的应答
创建方法:
public static int showOptionDialog(Component parentComponent,Object message,String title,int optionType,int messageType,Icon icon,Object[] options,Object intialValue)
使用options参数指定按钮,intialValue参数允许指定一个接收初始焦点的按钮


菜单类
创建菜单
(1)创建菜单栏,并使用setJMenuBar方法将它与框架相关联(JMenuBar是用于承载菜单的顶级菜单组件)

JFrame frame=new JFrame();
frame.setSize(300,200);
frame.setVisible(true);
JMenuBar jmb=new JMenuBar();
frame.setJMenuBar(jmb);

(2)创建菜单并将它们与菜单栏相关联

JMenu menu=new JMenu("title");
jmb.add(menu);

(3)创建菜单项并将它们添加到菜单

menu.add(new JMenuItem("new"));
menu.addSeparator();  //此方法添加一个分割条

a.创建子菜单项:可以在菜单内嵌套菜单,嵌入的菜单就成为子菜单
b.创建复选框菜单项:可以向JMenu添加JCheckBoxMenuItem。JCheckBoxMenuItem是JMenuItem的子类,用于向JMenuItem添加一个布尔状态,当它为真时,显示一个复选框
c.创建单选按钮菜单项:可以使用JRadioButtonMenuItem类将单选按钮添加到菜单中。如果在菜单中有许多互斥的选项,使用此方法通常很有效

(4)菜单项产生ActionEvent事件,程序必须实现actionRerformed处理程序

public void actionPerformed(ActionEvent e){
     String actionCommand=e.getActionCommand();
     if(e.getSource() instanceOf JMenuItem)
          if("new".equals(actionCommand))
               respondToNew();
}

图像图标,键盘记忆键和键盘加速键
菜单类有icon和mnemonic属性
方法:setIcon(ImageIcon object);
setMnemonic(char character);
JMenuItem的构造函数:
public JMenuItem(String label,Icon icon);
public JMenuItem(String label,int mnemonic); //mnemonic为键盘记忆键
默认情况下,文本在图标的右侧,使用setHorizontalTextPosition(SwingConstants.LEFT)将文本设置到图标的左侧
键盘记忆键只允许从当前打开的菜单选择菜单项
键盘加速键允许通过按下Ctrl和加速键直接选择菜单项

JMenuItem jmi=new JMenuItem("label");
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
//setAccelerator方法使用了KeyStroke对象,KeyStroke类中的静态方法getKeyStroke创建了按键的实例,VK_O代表键O的一个常量,而CTRL_MASK是表明Ctrl键与按键相关联的一个常量

JScrollBar类
构造函数:
public JScrollBar()构造一个新的垂直滚动条
public JScrollBar(int orientation)以指定的方向(JScrollBar.HORIZONTAL或JScrollBar.VERTICAL)构造新的滚动条
public JScrollBar(int orientation,int value,int visible,int minimum,int maximum)以指定的方向,初始值,滑块的可见大小以及最小值和最大值构造新的滚动条
属性:
orientation指定水平或垂直风格,JScrollBar.HORIZONTAL(0)表示水平,而JScrollBar.VERTICAL(1)表示垂直
maximum指定当滑块到达水平风格滚动条的右端,或者到达垂直风格的低端,滚动条所能代表的最大值
minimum与上面的相反
visibleAmount指定滚动条滑块的相对宽度
value代表滚动条的当前值。SetValue方法的同时,同步设置滚动条的minimum,maximum,visible amount和value属性值
blockIncrement是当用户激活滚动条的块增加(减少)区时,增加(减少)的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值