防止重叠式布局移动背景图片标签 [英] Preventing Overlay Layout from shifting background image label

查看:95
本文介绍了防止重叠式布局移动背景图片标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Overlay Layout的面板的JFrame,因为我想设置一个以图像为背景的JLabel并将东西放在其上方.我将框架的大小设置为等于图像的大小,并且当我仅将其放在合适的位置上时,但是如果在其顶部添加任何内容,则会将图像向右移动我添加的组件的宽度并在右侧将其剪下.即使组件不在移入的区域中,它也会执行此操作.该组件在右侧,并且已正确显示在标签上,但图像仍在移动.

I have a JFrame with a panel on it with Overlay Layout because I want to set a JLabel with an image as the background and put stuff over top of it. I set the size of the frame equal to the size of the image and when I put only that on it fits perfectly, but if I add anything over top of it it shifts the image to the right by the width of the component that I add and cuts it off to the right. It does this even though the component isn't in the region that is shifted over. The component is to the right and its properly displayed over the label but the image is still shifted over.

    import java.awt.*;       
    import java.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.awt.image.*;

    public class MainScreen extends JFrame{  
        JButton button;
        JPanel backgroundPanel, p, panel;

        public MainScreen(ArrayList<Character> c){                  
        JLabel label = new JLabel(new ImageIcon("house.jpg"));
        button = new JButton("Button");

        backgroundPanel = new JPanel();
        p = new JPanel();
        panel = new JPanel();

        p.add(button);  
        panel.add(p);

        panel.setOpaque(false);
        p.setOpaque(false); 

        LayoutManager overlay = new OverlayLayout(backgroundPanel);
        backgroundPanel.setLayout(overlay);

        backgroundPanel.add(panel); 
        backgroundPanel.add(label);        

        add(backgroundPanel);      
        setLocationRelativeTo(null);                 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setSize(1200, 700);
        setVisible(true);   
        }

        public static void main(String[] args){
        MainScreen ms = new MainScreen(new ArrayList<Character>());
        }
    }

推荐答案

但是,如果我在其顶部添加任何内容,则会将图像向右移动所添加组件的宽度,并向右剪切

but if I add anything over top of it it shifts the image to the right by the width of the component that I add and cuts it off to the right

您需要使用两个组件的alignmentX/Y属性来获得所需的布局.

You need to play with the alignmentX/Y properties of the two components to get your desired layout.

首先将它们全部设置为0.5f.

Start by setting them all to 0.5f.

这里有一个演示程序,可让您更改属性以查看效果:

Here is a little demo program that allows you to change the properties to see the effect:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class OverlayLayoutTest extends JPanel
    implements ActionListener
{
    JPanel green;
    JPanel red;
    JLabel greenLabel;
    JLabel redLabel;
    JComboBox  greenAlignmentX;
    JComboBox  greenAlignmentY;
    JComboBox  redAlignmentX;
    JComboBox  redAlignmentY;

    public OverlayLayoutTest()
    {
        setLayout( new BorderLayout(10, 10) );
        add(createNorthPanel(), BorderLayout.NORTH);
        add(createCenterPanel(), BorderLayout.CENTER);
        add(createSouthPanel(), BorderLayout.SOUTH);
    }

    private JPanel createNorthPanel()
    {
        JPanel panel = new JPanel();

        panel.add( new JLabel("Green:") );
        greenLabel = new JLabel();
        panel.add( greenLabel );

        panel.add( new JLabel("Red:") );
        redLabel = new JLabel();
        panel.add( redLabel );

        return panel;
    }

    private JPanel createCenterPanel()
    {

        JPanel panel = new JPanel();
        panel.setLayout( new OverlayLayout(panel) );
        panel.setBackground( Color.ORANGE );
        panel.setPreferredSize( new Dimension(200, 200) );

        red = new JPanel();
        red.setBackground( Color.RED );
        red.setPreferredSize( new Dimension(50, 50) );
        red.setMaximumSize( red.getPreferredSize() );
        red.setMinimumSize( red.getPreferredSize() );
        panel.add( red );

        green = new JPanel();
        green.setBackground( Color.GREEN );
        green.setPreferredSize( new Dimension(100, 100) );
        green.setMaximumSize( green.getPreferredSize() );
        green.setMinimumSize( green.getPreferredSize() );
        panel.add( green );

        JPanel wrap = new JPanel();
        wrap.add( panel );
        return wrap;
    }

    private JPanel createSouthPanel()
    {
        JPanel panel = new JPanel( new GridLayout(1, 0, 10, 10) );

        JPanel green = new JPanel(new GridLayout(0, 2, 5, 5) );
        green.setBorder( new TitledBorder("Green Alignment") );
        green.add( new JLabel("X Alignment:") );
        greenAlignmentX = createComboBox();
        green.add( greenAlignmentX );
        green.add( new JLabel("Y Alignment:") );
        greenAlignmentY = createComboBox();
        green.add( greenAlignmentY );
        panel.add( green );

        JPanel red = new JPanel(new GridLayout(0, 2, 5, 5) );
        red.setBorder( new TitledBorder("Red Alignment") );
        red.add( new JLabel("X Alignment:") );
        redAlignmentX = createComboBox();
        red.add( redAlignmentX );
        red.add( new JLabel("Y Alignment:") );
        redAlignmentY = createComboBox();
        red.add( redAlignmentY );
        panel.add( red );

        JButton reset = new JButton("Reset Alignment");
        reset.addActionListener( this );
        panel.add( reset );


        return panel;
    }

    public void actionPerformed(ActionEvent e)
    {
        green.setAlignmentX( ((Float)greenAlignmentX.getSelectedItem()) );
        green.setAlignmentY( ((Float)greenAlignmentY.getSelectedItem()) );
        red.setAlignmentX( ((Float)redAlignmentX.getSelectedItem()) );
        red.setAlignmentY( ((Float)redAlignmentY.getSelectedItem()) );
        JPanel parent = (JPanel)green.getParent();
        parent.revalidate();
/*
        System.out.print(green.getAlignmentX() + " : ");
        System.out.print(green.getAlignmentY() + " : ");
        System.out.print(red.getAlignmentX() + " : ");
        System.out.print(red.getAlignmentY() + " : ");
        System.out.println();
*/
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                greenLabel.setText( green.getLocation().toString() );
                redLabel.setText( red.getLocation().toString() );
            }
        });

    }

    private JComboBox createComboBox()
    {
        JComboBox<Float> comboBox = new JComboBox<Float>();

        comboBox.addItem( new Float(0f) );
        comboBox.addItem( new Float(0.25f) );
        comboBox.addItem( new Float(0.5f) );
        comboBox.addItem( new Float(0.75f) );
        comboBox.addItem( new Float(1.0f) );
        comboBox.setSelectedItem(0.5f);

        return comboBox;
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("OverlayLayoutTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new OverlayLayoutTest() );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

另一个选择是将JLabel的布局设置为所需的任何内容(BorderLayout,GridBagLayout,FlowLayout).然后,您可以像在任何面板上一样直接将组件添加到标签.您添加的任何组件必须完全包含在标签内.与使用OverlayLayout相比,这将给您带来更大的灵活性.

Another option is to just set the layout of the JLabel to whatever you want (BorderLayout, GridBagLayout, FlowLayout). Then you can add components directly to the label, like you would for any panel. Any component you add must be fully contained within the label. This will give you more flexibility than using the OverlayLayout.

这篇关于防止重叠式布局移动背景图片标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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