写了Java这么久,居然发现想手写一个带网格袋布局的JFrame,还不记得怎么写,写了这么多代码真不敢说记得所有细节。
幸好,只要记清楚概念就能快速开发。首先,明确一下3种容器类的差别和用途:
| No. | 区别 |
|---|---|
| 1 |
Panel
JPanel用于放置其他控件,也包含其他panels。 |
| 2 |
Frame
JFrame 是包含 title and a border的第一层级的容器,其中通过布局设置JPanel或其他控件的位置。 |
| 3 |
Window
JWindow是不包含 title and a border的第一层级的容器。
|
JFrame构造函数
| S.N. | Description |
|---|---|
| 1 |
JFrame()
Constructs a new frame that is initially invisible. |
| 2 |
JFrame(GraphicsConfiguration gc)
Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title. |
| 3 |
JFrame(String title) 带标题
Creates a new, initially invisible Frame with the specified title. |
| 4 |
JFrame(String title, GraphicsConfiguration gc)
Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device. |
Class methods
S.N. Method & Description
1 protected void addImpl(Component comp, Object constraints, int index)
Adds the specified child Component. 添加子控件
2 protected JRootPane createRootPane()
Called by the constructor methods to create the default rootPane.
3 protected void frameInit()
Called by the constructors to init the JFrame properly.
4 AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JFrame.
5 Container getContentPane() 返回容器Pane,经常使用
Returns the contentPane object for this frame.
6 int getDefaultCloseOperation()
Returns the operation that occurs when the user initiates a "close" on this frame.
7 Component getGlassPane()
Returns the glassPane object for this frame.
8 Graphics getGraphics()
Creates a graphics context for this component.
9 JMenuBar getJMenuBar() 返回菜单
Returns the menubar set on this frame.
10 JLayeredPane getLayeredPane()
Returns the layeredPane object for this frame.
11 JRootPane getRootPane() 返回根容器,经常使用
Returns the rootPane object for this frame.
12 TransferHandler getTransferHandler()
Gets the transferHandler property.
13 static boolean isDefaultLookAndFeelDecorated()
Returns true if newly created JFrames should have their Window decorations provided by the current look and feel.
14 protected boolean isRootPaneCheckingEnabled()
Returns whether calls to add and setLayout are forwarded to the contentPane.
15 protected String paramString()
Returns a string representation of this JFrame.
16 protected void processWindowEvent(WindowEvent e)
Processes window events occurring on this component.
17 void remove(Component comp)
Removes the specified component from the container.
18 void repaint(long time, int x, int y, int width, int height)
Repaints the specified rectangle of this component within time milliseconds.
19 void setContentPane(Container contentPane)
Sets the contentPane property.
20 void setDefaultCloseOperation(int operation)
Sets the operation that will happen by default when the user initiates a "close" on this frame.
21 static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
Provides a hint as to whether or not newly created JFrames should have their Window decorations (such as borders, widgets to close the window, title...) provided by the current look and feel.
22 void setGlassPane(Component glassPane)
Sets the glassPane property.
23 void setIconImage(Image image) 设置图标
Sets the image to be displayed as the icon for this window.
24 void setJMenuBar(JMenuBar menubar) 设置菜单
Sets the menubar for this frame.
25 void setLayeredPane(JLayeredPane layeredPane)
Sets the layeredPane property.
26 void setLayout(LayoutManager manager) 设置布局
Sets the LayoutManager.
27 protected void setRootPane(JRootPane root)
Sets the rootPane property.
28 protected void setRootPaneCheckingEnabled(boolean enabled)
Sets whether calls to add and setLayout are forwarded to the contentPane.
29 void setTransferHandler(TransferHandler newHandler)
Sets the transferHandler property, which is a mechanism to support transfer of data into this component.
30 void update(Graphics g)
Just calls paint(g).