简体   繁体   English

在全屏上摆动布局边框边距

[英]Swing layout border margins on fullscreen

I have Java Swing application with JFrame using BorderLayout and inside it is a JPanel using CardLayout. 我有使用BorderLayout的JFrame的Java Swing应用程序,并且在其中是使用CardLayout的JPanel。 I am displaying 3 different cards. 我要显示3张不同的卡片。 If I manually set the size of the JFrame, then the content is displayed like I want it. 如果我手动设置JFrame的大小,那么内容将按照我的需要显示。 Label with image is in south east corner. 带有图像的标签在东南角。 在此处输入图片说明

But when I set it to full screen, there is to much margin: 但是,当我将其设置为全屏显示时,会有很大的余量: 在此处输入图片说明

Here is the code with which I set it to full screen: 这是我将其设置为全屏显示的代码:

Frame[] frames = Frame.getFrames();
        JFrame frame =  (JFrame) frames[0];
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        //frame.getContentPane().setPreferredSize( Toolkit.getDefaultToolkit().getScreenSize());
        frame.setUndecorated(true);
        //frame.setSize(600,500);
        frame.setVisible(true);
        frame.setLayout(new BorderLayout());

Cards are build with Netbeans GUI builder and for layout is set "Free Design". 卡是使用Netbeans GUI构建器构建的,并且布局设置为“自由设计”。

Application will be whole time in full screen, where I would like that label with the image is SE corner, like it is on resized window(image example 1). 应用程序将始终处于全屏状态,我希望带有图像的标签位于SE角,就像在调整大小的窗口上一样(图像示例1)。 Do I need to change layout for this or is it something else? 我需要为此更改布局还是其他?

Note that these UIs have a small border around the entire UI. 请注意,这些用户界面在整个用户界面周围都有小边框。 To remove it, comment out the line: 要删除它,请注释掉该行:

ui.setBorder(new EmptyBorder(4,4,4,4));

在此处输入图片说明 在此处输入图片说明

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ImageInSouthEast {

    private JComponent ui = null;

    ImageInSouthEast() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4,4,4,4));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridwidth = 2;
        gbc.weighty = .5;
        gbc.weightx = .5;
        gbc.gridx = 0;
        gbc.gridy = 0;

        // first add the labels
        for (int ii=1; ii<5; ii++) {
            gbc.gridy = ii;
            if (ii==4) {
                gbc.gridwidth = 1;
            }
            JLabel l = new JLabel("Label " + ii);
            l.setFont(l.getFont().deriveFont(50f));
            ui.add(l, gbc);
        }

        // now for the image!
        BufferedImage bi = new BufferedImage(100, 50, BufferedImage.TYPE_INT_RGB);
        JLabel l = new JLabel(new ImageIcon(bi));
        gbc.anchor = GridBagConstraints.LAST_LINE_END;
        gbc.gridx = 2;
        gbc.weighty = 0;
        ui.add(l, gbc);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ImageInSouthEast o = new ImageInSouthEast();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

If you just want to remove the gap between the text then you could just use BoxLayout . 如果只想消除文本之间的间隙,则可以使用BoxLayout

Set the layout by doing this: 通过执行以下操作设置布局:

Container pane = frame.getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
pane.add(Box.createHorizontalGlue());

Adding an element 添加元素

public void add(Component comp, int gap){
    //comp is the component that will be added
    //gap is the extra space after the last component and this
    pane.remove(pane.getComponents().length - 1);
    pane.add(Box.createVerticalStrut(gap));
    pane.add(comp);
    //Obviously pane or frame need to be visible to use this method
}

Add Text by doing this: 通过执行以下操作添加文本:

add(new JLabel(text), 5);

Add the image by doing this: 通过执行以下操作添加图像:

JPanel panel = new JPanel();
panel.add(image, BorderLayout.EAST);
panel.setOpaque(false);
add(Box.createHorizontalGlue(),0);
add(panel,0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM