我想在窗口底部添加一个边框,我使用MatteBorder
进行了尝试,但边框仅扩展到由GridBagLayout
组成的面板。(如图所示)。我知道这是因为我已经将边框设置为该面板但是当我将其添加到新的子面板然后将该面板添加到主面板并最终将主面板添加到{{1}时,边框甚至不会出现}。
我希望边框一直延伸到窗口的底部。
这是我的代码:
JFrame
主要
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);
}
}
我希望窗口看起来像这样(这是使用Windows Paint完成的):
答案 0 :(得分:4)
您在MatteBorder
的底部绘制JPanel pnl
并且它不像窗口宽度那么长,因为您的JPanel pnl
不像窗口宽度那么长见这里。
所以你已经定义了另一个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
}