最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JFrame中添加和設(shè)置JPanel的方法實(shí)例解析

 更新時(shí)間:2018年01月15日 11:08:02   作者:輕雨漫步  
這篇文章主要介紹了JFrame中添加和設(shè)置JPanel的方法實(shí)例解析,具有一定借鑒價(jià)值

Swing 程序用JFrame 對(duì)象實(shí)現(xiàn)了它們的窗口。JFrame 類是AWT Frame 類的一個(gè)子類。它還加入了一些Swing 所獨(dú)有的特性。與 Frame 的使用十分相似。唯一的區(qū)別在于,你不能將組件加入到JFrame中。你可以或者將組件加入到JFrame 的content pane(內(nèi)容面板) 中,或者提供一個(gè)新的content pane(內(nèi)容面板)。

面板與頂層容器的不同點(diǎn):面板不能獨(dú)立存在,必須被添加到其他容器內(nèi)部(面板可以嵌套)。

  JFrame 有一個(gè) Content Pane,窗口能顯示的所有組件都是添加在這個(gè) Content Pane 中。JFrame 提供了兩個(gè)方法: getContentPane 和 setContentPane 就是用于獲取和設(shè)置其 Content Pane 的?! ?/p>

對(duì)JFrame添加組件有兩種方式:

  1)用 getContentPane ()方法獲得JFrame的內(nèi)容面板,再對(duì)其加入組件:frame. getContentPane ().add(childComponent)

  2)建立一個(gè)Jpanel或JDesktopPane之類的中間容器,把組件添加到容器中,用setContentPane()方法把該容器置為JFrame的內(nèi)容面板:

    JPanel contentPane = new JPanel();
    ……//把其它組件添加到Jpanel中;
    frame.setContentPane(contentPane);
    //把contentPane對(duì)象設(shè)置成為frame的內(nèi)容面板 

實(shí)例程序:

import java.awt.*; 
import javax.swing.*; 
 
public class JFrameWithPanel { 
   
  public static void main(String[] args) { 
     
    JFrame frame = new JFrame("Frame With Panel"); 
    Container contentPane = frame.getContentPane(); 
    contentPane.setBackground(Color.CYAN); // 將JFrame實(shí)例背景設(shè)置為藍(lán)綠色 
    JPanel panel = new JPanel(); // 創(chuàng)建一個(gè)JPanel的實(shí)例 
    panel.setBackground(Color.yellow); // 將JPanel的實(shí)例背景設(shè)置為黃色 
    JButton button = new JButton("Press me"); 
    panel.add(button); // 將JButton實(shí)例添加到JPanel中 
 
    contentPane.add(panel, BorderLayout.SOUTH); // 將JPanel實(shí)例添加到JFrame的南側(cè) 
    frame.setSize(300, 200); 
    frame.setVisible(true); 
  } 
} 

截圖:

總結(jié)

以上就是本文關(guān)于JFrame中添加和設(shè)置JPanel的方法實(shí)例解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

开原市| 宜宾县| 德令哈市| 临西县| 四会市| 江陵县| 牙克石市| 利津县| 左权县| 宝山区| 黄骅市| 南城县| 红桥区| 桦川县| 玉门市| 岚皋县| 开化县| 台安县| 佳木斯市| 怀柔区| 大余县| 武陟县| 清涧县| 茌平县| 常山县| 清水县| 称多县| 砀山县| 客服| 开封县| 顺平县| 藁城市| 临沭县| 嘉黎县| 古田县| 洪湖市| 黄龙县| 阳朔县| 五常市| 清镇市| 合肥市|