我想在窗口底部添加一個邊框,我已經使用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);
}
}
我希望窗口看起來像這樣(這樣做使用Windows Paint):
你沒有提供任何圖像我們與測試。您還需要將圖像放入Java項目中,以便可以將它們導出到JAR文件中。 –
我現在不能測試它,但是你可以在你的框架中添加一個BorderLayout,並在'CENTER'位置設置pnl,然後創建一個空的'JPanel',爲它添加一個上邊框並將其設置爲'南'位置。或者,您可以查看具有「WebStatusBar」組件的「WebLAF」,該組件可以完全滿足您的需求。 – StepTNT