一、布局管理器
1、概念
awt
FlowLayout、BorderLayout、CardLayout、GridLayout、GridBagLayout
swing
BoxLayout、ScrollPaneLayout、ViewportLayout、Overlayout
2、BorderLayout
BorderLayout类的构造方法:
BorderLayout()
BorderLayout(int hgap, int vgap)
3、FlowLayout
FlowLayout类的构造方法:
FlowLayout()
FlowLayout(int align)
FlowLayout(int align, int hgap, int vgap)
4、CardLayout
CardLayout类的构造方法:
CardLayout()
CardLayout(int hgap, int vgap)
CardLayout类的方法:
first(Container c)
last(Container c)
next(Container c)
previous(Container c)
5、GridLayout
GridLayout类的构造方法:
GridLayout()
GridLayout(int rows, int cols)
GridLayout(int rows, int cols, int hgap, int vgap)
6、BoxLayout
BoxLayout类的字段:
static int X_AXIS
static int Y_AXIS
BoxLayout类的构造方法:
BoxLayout(Container target, int axis)
BoxLayout类的方法:
getLayoutAlignmentX(Container target)
getLayoutAlignmentY(Container target)
setLayoutAlignmentX(Container target)
setLayoutAlignmentY(Container target)
LayoutContainer(Container target)
Box类的方法:
Box.createHorizontalBox()
Box.createVerticalBox()
Component.createHorizontalGlue()
Component.createVerticalGlue()
Component.createHorizontalStrut()
Component.createVerticalStrut()
二、键盘和鼠标事件处理
1、键盘事件
监听者要完成对事件的响应,就要实现KeyListener接口,或继承KeyAdapter类,实现对类中方法的定义。KeyListener接口中有如下3个事件:
KeyListener接口中有如下3个事件:
KEY_PRESSED
KEY_RELEASED
KEY_TYPED
2、鼠标事件
对MouseEvent事件的响应是实现MouseListen接口或者MouseMotionListener接口,或者是继承MouseApdater类,来实现MouseApdapter提供的方法。
MouseListener接口提供5中方法:
mouseClicked(MouseEvent e)
mouseEntered(MouseEvent e)
mouseExited(MouseEvent e)
mousePressed(MouseEvent e)
mouseReleased(MouseEvent e)
MouseMotionListener接口提供了两种方法:
mouseDragged(MouseEvent e)
mouseMoved(MouseEvent e)
MouseEvent类提供的方法:
int getX()
int getY()
Point getPoint()
Point getClickCount()
三、菜单设计
1、菜单栏、菜单、菜单项的构造方法和方法
JMenuBar类构造方法:
JMenuBar()
JMenu类构造方法:
JMenu()
JMenu(String str)
JMenu(String str, boolean b)
JMenu类方法:
void addSeparator()
JMenuItem类构造方法:
JMenuItem()
JMenuItem(String str)
JMenuItem(Icon icon)
JMenuItem(String str, Icon icon)
JMenuItem(String str, int nmeminic)
2、制作菜单的一般步骤
(1) 创建一个JMenuBar对象并将其加入到JFrame中。
(2) 创建JMenu对象。
(3) 创建JMenuItem对象并将其添加到JMenu对象中。
(4) 将JMenu对象添加到JMenuBar中。
四、对话框设计
JoptionPane
JOptionPane类:
JOptionPane()
JOptionPane(Object msg)
JOptionPane(Object msg, int msgtype)
JOptionPane(Object msg, int msgtype, int optiontype, Icon icon)
JOptionPane(Object msg, int msgtype, int optiontype, Icon icon, Object options[ ])
JOptionPane(Object msg, int msgtype, int optiontype, Icon icon, Object options[ ], Object initialValue)
可以将JOptionPane对话框分为4中类型:
showMessageDialog
showComfirmDialog
showInputDialog
showOptionDialog
这些方法都可以会返回一个整数,表示用户点击了哪些按钮,有效值有YES_OPTION、NO_OPTION、CANCEL_OPTION、OK_OPTION和CLOSED_OPTION。
1、Message Dialog
void showMessageDialog(Component parentComponent, Object msg)
void showMessageDialog(Component parentComponent, Object msg, String str, int msgtype)
void showMessageDialog(Component parentComponent, Object msg, String str, int msgtype, Icon icon)
void showInternalMessageDialog(Component parentComponent, Object msg)
void showInternalMessageDialog(Component parentComponent, Object msg, String str, int msgtype)
void showInternalMessageDialog(Component parentComponent, Object msg, String str, int msgtype Icon icon)
msgtype类型:
JOptionPane.PLAIN_MESSAGE
JOptionPane.INFORMATION_MESSAGE
JOptionPane.ERROR_MESSAGE
JOptionPane.WARNING_MESSAGE
JOptionPane.QUESTION_MESSAGE
2、Input Dialog
void showInputDialog(Object msg)
void showInputDialog(Component parentComponent, Object msg)
void showInputDialog(Component parentComponent, Object msg, String title, int msgtype)
void showInputDialog(Component parentComponent, Object msg,, String title, int msgtype, Icon icon, Object selectValues[], Object initialselectionValue)
3、Confirm Dialog
void showConfirmDialog(Component parentComponent, Object msg)
void showConfirmDialog(Component parentComponent, Object msg, String title, int optiontype)
void showConfirmDialog(Component parentComponent, Object msg, String title, int optiontype, int messagetype)
void showConfirmDialog(Component parentComponent, Object msg, String title, int optiontype, int messagetype, Icon icon)
optiontype类型:
DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION和OK_CANCEL_OPTION
JDialog
JDialog类的构造方法:
JDialog()
JDialog(Dialog owner)
JDialog(Dialog owner, String str)
JDialog(Dialog owner, boolean modal)
JDialog(Dialog owner, String str, boolean modal)
JDialog(Frame owner)
JDialog(Frame owner, String str)
JDialog(Frame owner, boolean modal)
JDialog(Frame owner, String str, boolean modal)
五、窗口
1、JPanel容器
JPanel类的构造方法:
JPanel()
JPanel(boolean idDoubleBuffered)
JPanel(LayoutManager layout)
JPanel(LayoutManager layout, boolean idDoubleBuffered)
2、JScrollPane容器
JScrollPane类的构造方法:
JScrollPane()
JScrollPane(Component component)
JScrollPane(int v, int h)
JScrollPane(Component component, int v, int h)
JScrollPane类的设置滚动条的参数:
HORIZONTAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_NEVER
VERTICAL_SCROLLBAR_NEVER
HORIZONTAL_SCROLLBAR_NEEDED
VERTICAL_SCROLLBAR_NEEDED