2016-02-25 85 views
0

我想在窗口底部添加一個邊框,我已經使用MatteBorder嘗試了它,但邊框只能擴展到由我組成的面板GridBagLayout。(如圖所示)。我知道這是因爲我已將邊框設置爲該面板,但是當我將其添加到新的子面板時,邊框甚至不會出現,然後將該面板添加到主面板並最終將主面板添加到JFrame。 我希望邊框沿着窗口的底部延伸。Java Swing GUI-在底部添加一個邊框

這裏是我的代碼:

public class Admin_hs extends JFrame { 

    JButton bking_btn= new JButton("Bookings"); 
    JButton fd_btn= new JButton("Financial Data"); 
    JButton ctm_btn= new JButton("Customers"); 
    JButton room_btn= new JButton("Rooms"); 
    JButton adc_btn= new JButton("Additional Costs"); 
    JButton endb_btn= new JButton("Ending Bookings"); 

    //Images 
    JLabel bking_img= new JLabel(); 
    JLabel fd_img= new JLabel(); 
    JLabel ctm_img= new JLabel(); 
    JLabel room_img= new JLabel(); 
    JLabel adc_img= new JLabel(); 
    JLabel endb_img= new JLabel(); 

    ///Panels 

    JPanel pnl1= new JPanel(); 
    JPanel pnl= new JPanel(); 

    ///Constructors 

    public Admin_hs(){ 
     this.setTitle("Welcome Admin!"); 
     this.setLayout(new GridBagLayout()); 

     ///Setting a layout 


     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth= GridBagConstraints.REMAINDER; 
     gbc.fill= gbc.HORIZONTAL; 

     pnl.setLayout(new GridBagLayout()); 

     GridBagConstraints gc= new GridBagConstraints(); 

     ///First Column of Grid 


     gc.insets = new Insets(6, 6, 6, 6); 
     gc.anchor = GridBagConstraints.WEST; 
     gc.weightx = 0.5; 
     gc.weighty = 0.5; 

     gc.gridx = 0; 
     gc.gridy = 0; 

     pnl.add(bking_btn, gc); 

     gc.gridx = 0; 
     gc.gridy = 1; 
     pnl.add(fd_btn, gc); 

     gc.gridx = 0; 
     gc.gridy = 2; 
     pnl.add(ctm_btn, gc); 

     gc.gridx = 0; 
     gc.gridy = 3; 
     pnl.add(room_btn, gc); 

     gc.gridx = 0; 
     gc.gridy = 4; 
     pnl.add(adc_btn, gc); 

     gc.gridx = 0; 
     gc.gridy = 5; 
     pnl.add(endb_btn, gc); 


     /////second column of grid 



     gc.anchor = GridBagConstraints.WEST; 
     gc.gridx = 1; 
     gc.gridy = 0; 
     bking_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/booking.jpg")); 
     pnl.add(bking_img, gc); 

     gc.gridx = 1; 
     gc.gridy = 1; 
     fd_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/fd.jpg")); 
     pnl.add(fd_img, gc); 

     gc.gridx = 1; 
     gc.gridy = 2; 
     ctm_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/guest.jpg")); 
     pnl.add(ctm_img, gc); 

     gc.gridx = 1; 
     gc.gridy = 3; 
     room_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/room.jpg")); 
     pnl.add(room_img, gc); 

     gc.gridx = 1; 
     gc.gridy = 4; 
     adc_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/adc.jpg")); 
     pnl.add(adc_img, gc); 

     gc.gridx = 1; 
     gc.gridy = 5; 
     endb_img.setIcon(new ImageIcon("C:/Users/Diksha/Desktop/OOSD Assignment/icons/60-60/endb.png")); 
     pnl.add(endb_img, gc); 

     pnl.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.BLACK)); 

     this.add(pnl); 



    } 

} 

主要

public class Admin_main { 

public static void main(String[] args) { 

    Admin_hs adm= new Admin_hs(); 

    adm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    adm.pack(); 
    adm.setVisible(true); 
    adm.setSize(780,520); 

    } 

} 

目前,它看起來像這樣: enter image description here

我希望窗口看起來像這樣(這樣做使用Windows Paint):

enter image description here

+0

你沒有提供任何圖像我們與測試。您還需要將圖像放入Java項目中,以便可以將它們導出到JAR文件中。 –

+2

我現在不能測試它,但是你可以在你的框架中添加一個BorderLayout,並在'CENTER'位置設置pnl,然後創建一個空的'JPanel',爲它添加一個上邊框並將其設置爲'南'位置。或者,您可以查看具有「WebStatusBar」組件的「WebLAF」,該組件可以完全滿足您的需求。 – StepTNT

回答

4

你在你的JPanel pnl的底部畫一個MatteBorder,這是不是隻要窗口寬度,因爲你的JPanel pnl是不是隻要在窗口的寬度,你可以在這裏看到。

enter image description here

所以你已經定義的另一個JPanel pnl1但你沒有使用它。

public Admin_hs(){ 
    this.setTitle("Welcome Admin!"); 
    this.setLayout(new GridBagLayout()); 

     ///Setting a layout 
    pnl1.setLayout(new GridBagLayout()); // ADD THIS LINE 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridwidth= GridBagConstraints.REMAINDER; 
    gbc.fill= GridBagConstraints.HORIZONTAL; 
    gbc.weightx = 1;      // ADD THIS LINE so it uses all the existing window size in x direction 
    pnl1.add(pnl);       // ADD THIS LINE 
    [...] 
    // CHANGE THIS LINE to use the bottom border of your pnl1 
    pnl1.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.BLACK)); 
    this.add(pnl1,gbc);     // CHANGE THIS LINE to add pnl1 and not pnl to your main window 
} 

,你會得到這個

enter image description here

+0

你可以向我解釋一下'gbc.gridwidth = GridBagConstraints.REMAINDER;' 'gbc.fill = GridBagConstraints.HORIZONTAL;' 'gbc.weightx = 1;'do?我對佈局很陌生,所以理解GridBagLayout – Tia

+0

您應該學習GridBagConstraints [Documentation](https://docs.oracle.com/javase/7/docs/api/java/awt/GridBagConstraints.html)和[Tutorials ](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)。簡而言之:'gridwidth = REMAINDER'(一行中的單元格數量,REMAINDER->唯一的組件是最後一個),'fill = HORIZONTAL'(如何填充空間,如果需要更大,則需要:NO,VERT,HORIZ,BOTH), 'weightx = 1'(唯一的組件獲得水平現有空間的100%) – ArcticLord

+0

非常感謝!這非常有幫助。 – Tia