Java Swing GUI - 在底部添加一个边框 [英] Java Swing GUI- adding a single border at the bottom

查看:39
本文介绍了Java Swing GUI - 在底部添加一个边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的窗口底部添加一个边框,我已经尝试使用 MatteBorder 但边框只扩展到由我的 GridBagLayout 组成的面板.(如图).我知道这是因为我已将边框设置为该面板,但是当我将其添加到新的子面板,然后将该面板添加到主面板并最终将主面板添加到 JFrame 时,边框甚至没有出现.我希望边框沿窗口底部延伸.

I want to add a single border at the bottom of my window and I have tried it using the MatteBorder but the border is being extended only up to the panel which consists of my GridBagLayout.(as shown in the picture). I know this is because I have set the border to that panel but the border is not even appearing when I am adding it to a new sub panel and then adding that panel to the main panel and ultimately the main panel to the JFrame. I want the border to extend all along the bottom of the window.

这是我的代码:

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 完成的):

I want the window to look like this(this was done using Windows Paint):

推荐答案

你在你的 JPanel pnl 的底部绘制了一个 MatteBorder 并且它不像窗口宽度,因为您的 JPanel pnl 没有您在此处看到的窗口宽度那么长.



所以你已经定义了另一个 JPanel pnl1 但你没有使用它.

You draw a MatteBorder at the Bottom of your JPanel pnl and it is not as long as the window width, because your JPanel pnl is not as long as the window width as you can see here.



So you already defined another JPanel pnl1 but you did not use it.

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
}

你会得到这个

and you will get this

这篇关于Java Swing GUI - 在底部添加一个边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆