gpt4 book ai didi

Java Swing GUI - 在底部添加单个边框

转载 作者:行者123 更新时间:2023-12-01 18:07:00 43 4
gpt4 key购买 nike

我想在窗口底部添加一个边框,我已经尝试使用MatteBorder但边框仅扩展到由我的 GridBagLayout 组成的面板.(如图所示)。我知道这是因为我已经为该面板设置了边框,但当我将其添加到新的子面板,然后将该面板添加到主面板,最终将主面板添加到 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);



}

}

主要

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

最佳答案

您在 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

关于Java Swing GUI - 在底部添加单个边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35634655/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com