首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAVA -使用Box.createGlue()将对象定位在屏幕底部?

JAVA -使用Box.createGlue()将对象定位在屏幕底部?
EN

Stack Overflow用户
提问于 2018-06-25 13:16:03
回答 1查看 502关注 0票数 0

我在网上找不到答案,所以我就在这里。

带背景的JFrame

这就是我目前拥有的标志,上面写着“德克萨斯州凯蒂的五旬节”是我正在做的,也是版权的象征。

我希望徽标在底部,版权在右下角。下面是构建框架的所有代码:

代码语言:javascript
复制
//Made by Trey Carey | 6.24.18

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class loginScreen {
    static String versionNumber = new String("1.0"); //Version Number
    static String applicationName = new String("Lower Thirds SDV " + versionNumber); //Application Name

    public static void main(String[] args) throws IOException {
        createLoginWindow();
    }


    public static void createLoginWindow() throws IOException {     
        JFrame mainFrame = new JFrame(applicationName);

        //Images
        BufferedImage loginImage = ImageIO.read(new File ("src/Lower Thirds SDV PNG Elements/Login_BTN.png"));
        JLabel backgroundImage = new JLabel(new ImageIcon("src/Lower Thirds SDV PNG Elements/Main_BKG.png"));
        JLabel logo = new JLabel(new ImageIcon("src/Lower Thirds SDV PNG Elements/POK Logo.png"));
        JLabel copyrightImage = new JLabel(new ImageIcon("src/Lower Thirds SDV PNG Elements/Copyright.png"));

        GridBagLayout gridBagLayout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        mainFrame.add(backgroundImage);
        backgroundImage.setLayout(gridBagLayout);
        //Create Login Button
        JButton loginButton = new JButton(new ImageIcon (loginImage));
        loginButton.setBorder(BorderFactory.createEmptyBorder());

        c.anchor = GridBagConstraints.CENTER;
        c.gridy = 0;
        backgroundImage.add(loginButton, c);
        c.gridy ++;
        backgroundImage.add(copyrightImage, c);
        backgroundImage.add(Box.createGlue(), c);
        c.anchor = GridBagConstraints.PAGE_END;
        c.gridy ++;
        backgroundImage.add(logo, c);


        mainFrame.setResizable(false);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.pack();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
    }
}

任何帮助都将不胜感激!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-25 13:42:15

使用Box.createGlue()将对象定位在屏幕底部?

然后,您需要使用BoxLayout。阅读有关如何使用BoxLayout的Swing教程中的部分,以获得入门的示例。

组件处于中间位置的原因是您使用的是GridBagLayout,除非您指定了权重/y,否则组件将居中。有关这些约束的更多信息,请阅读关于如何使用GridBagLayout的教程。

注意,您从未被迫使用单一的布局管理器。因此,主面板可能使用BoxLayout。然后创建另外两个面板,每个面板使用适当的布局管理器。然后,您可以使用Box.createGlue()将底部面板与顶部面板分开。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51024425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档