日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java jframe添加面板_JFrame添加组件的两种方式

發(fā)布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java jframe添加面板_JFrame添加组件的两种方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對JFrame添加組件有兩種方式:1) 用getContentPane()方法獲得JFrame的內容面板,再對其加入組件:frame.getContentPane().add(childCompontent)

常分開來寫

Container container=getContentPanel();(隱式的this.getContentPanel()) ;得到jframe的內容面板

以后只要把容器加到container就可以了。

2) 建立一個JPanel或JDesktopPane之類的中間容器,把組件添加到容器中,用setContentPane()方法把該容器置為JFrame的內容面板:JPanel contentPane = new JPanel();......//把其他組件添加到JPanel中frame.setContentPane(contentPane);//把contentPane對象設置成為frame的內容面板

一般使用JFrame添加組件時,比如frame是JFrame的一個對象,我一般都是直接使用add()方法將組件加入,但是我看了很多例子,他們都是frame.getContentPane().add(),先得到內容面板,然后再添加組件,這兩種方法的區(qū)別是什么,為什么后面那個好像用的多些呢?

網友回答:

有區(qū)別的

當你創(chuàng)建一個JFrame的時候JFrame jf = new JFrame();

在構造方法JFrame()內部會給jf默認添加一個rootPane

所以執(zhí)行完JFrame jf = new JFrame();這句話之后jf上面已經添加了一個默認的rootpanel了

然后你再調用jf.add(panel) 這個時候,panel和rootPane是平級的

理由:1,你可以讀源代碼 ,查看構造方法怎么寫的

2,或者你可以測試一下,分別執(zhí)行

jf.setBackground(Color.blue);

jf.getContentPane().setBackground(Color.black);

這兩句代碼,看看效果(實際上上面一句并不能改變界面的背景色,下面一句才可以,因為rootPane把jf給擋住了,上面一句是改變了jf的背景色,但是你眼睛看到的并不是jf,其實是rootPane.)

另外

jf.getContentPane()==jf.getRootPane().getContentPane()

上面的比較返回的true

所以你調用jf.getContentPane().add(panel) 其實是把panel添加到rootPane上面了 這個時候panel和rootPane就不是平級的了

JFrame java api

An extended version of?java.awt.Frame?that adds support for the JFC/Swing component architecture. You can find task-oriented documentation about using?JFrame?in?The Java Tutorial, in the section?How to Make Frames.

The?JFrame?class is slightly incompatible with?Frame. Like all other JFC/Swing top-level containers, a?JFrame?contains a?JRootPane?as its only child.The?content pane?provided by the root pane should, as a rule, contain all the non-menu components displayed by theJFrame. This is different from the AWT?Frame?case. As a conveniance?add?and its variants,?remove?and?setLayout?have been overridden to forward to the?contentPane?as necessary. This means you can write:

frame.add(child);

And the child will be added to the contentPane. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it. Refer toRootPaneContainerfor details on adding, removing and setting theLayoutManagerof aJFrame.

JFrame?類與?Frame?輕微不兼容。與其他所有 JFC/Swing 頂層容器一樣,JFrame?包含一個?JRootPane?作為其唯一的子容器。根據(jù)規(guī)定,根窗格所提供的內容窗格應該包含?JFrame?所顯示的所有非菜單組件。這不同于 AWT?Frame。為了方便地使用?add?及其變體,已經重寫了?remove?和?setLayout,以在必要時將其轉發(fā)到?contentPane。這意味著可以編寫:

frame.add(child);

子級將被添加到 contentPane。內容窗格始終是非 null 的。試圖將其設置為 null 會導致 JFrame 拋出異常。默認的內容窗格上會設置有 BorderLayout 管理器。有關添加、移除和設置JFrame的LayoutManager的詳細信息,請參閱RootPaneContainer。產生JFrame的兩種方法(不繼承和繼承)

importjavax.swing.JFrame;public classGameFrame {publicGameFrame()

{

JFrame frame=newJFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setTitle("3d tetris");

frame.setSize(500,300);

frame.setLocation(400,400);

frame.setVisible(true);

}public static voidmain(String[] args)

{

GameFrame gameFrame=newGameFrame();

}

}

public class GameFrame extendsJFrame{publicGameFrame()

{super("3d tetris"); //設置標題,不要也可以

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("3d tetris");

setSize(500,300);

setLocation(400,400);

setVisible(true);

}public static voidmain(String[] args)

{

GameFrame gameFrame=newGameFrame();

}

}

總結

以上是生活随笔為你收集整理的java jframe添加面板_JFrame添加组件的两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。