java佈局 BorLayout佈局

3BorderLayout邊界佈局管理器

 

BorderLayout把容器分爲東、南、西、北、中五個區域,每個組件將佔據一個區域。這五個區域分別被命名爲NORTHWESTEASTSOUTHCENTER,五個區域使用五個靜態常量來標示。

常量

說明

public static final String NORTH=”North”

內容面板北邊

public static final String WEST=”West”

內容面板西邊

public static final String EAST=”East”

內容面板東邊

public static final String CENTER=”Center”

內容面板中間

public static final String SOUTH=”South”

內容面板南邊




案例1:完成如下圖所示的窗口設計

 

 

 

public class SwingLayoutDemo {    

private JFrame mainFrame;    

private JLabel headerLabel;    

private JLabel statusLabel;    

private JPanel controlPanel;    

private JLabel msglabel;     

public SwingLayoutDemo(){       

prepareGUI();    

}     

public static void main(String[] args){

SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();             

swingLayoutDemo.showBorderLayoutDemo();           

}

private void prepareGUI(){       

      mainFrame = new JFrame("Java SWING Examples");      

       mainFrame.setSize(400,400);

mainFrame.setLayout(new GridLayout(3, 1));        

headerLabel = new JLabel("",JLabel.CENTER );       

statusLabel = new JLabel("",JLabel.CENTER);                

statusLabel.setSize(350,100);       

mainFrame.addWindowListener(new WindowAdapter() {

          public void windowClosing(WindowEvent windowEvent){

          System.exit(0);          

}               

});

controlPanel = new JPanel();       

controlPanel.setLayout(new FlowLayout());        

mainFrame.add(headerLabel);       

mainFrame.add(controlPanel);       

mainFrame.add(statusLabel);       

mainFrame.setVisible(true);      

}     

private void showBorderLayoutDemo(){

headerLabel.setText("Layout in action: BorderLayout");

JPanel panel = new JPanel();       

panel.setBackground(Color.darkGray);       

panel.setSize(300,300);       

BorderLayout layout = new BorderLayout();       

layout.setHgap(10);       

layout.setVgap(10);       

panel.setLayout(layout);                   

panel.add(new JButton("Center"),BorderLayout.CENTER);       

panel.add(new JButton("Line Start"),BorderLayout.LINE_START);        

panel.add(new JButton("Line End"),BorderLayout.LINE_END);       

panel.add(new JButton("East"),BorderLayout.EAST);          

panel.add(new JButton("West"),BorderLayout.WEST);       

 panel.add(new JButton("North"),BorderLayout.NORTH);        

panel.add(new JButton("South"),BorderLayout.SOUTH);         

controlPanel.add(panel);        

mainFrame.setVisible(true);      

} }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章