如何在ImageIcon前移动JLabel?

时间:2016-03-12 19:46:30

标签: java swing jlabel imageicon

我认为所有内容都在代码中正确编写,我只需知道如何整理不同的图层。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

class FullSceenToggleAction extends AbstractAction {

    private JFrame frame;
    private GraphicsDevice fullscreenDevice;

    public FullSceenToggleAction(JFrame frame) {
        this(frame, GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
    }

    public FullSceenToggleAction(JFrame frame, GraphicsDevice fullscreenDevice) {
        this.frame = frame;
        this.fullscreenDevice = fullscreenDevice;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        frame.dispose();
        if (frame.isUndecorated()) {
            fullscreenDevice.setFullScreenWindow(null);
            frame.setUndecorated(false);
        } else {
            frame.setUndecorated(true);
            fullscreenDevice.setFullScreenWindow(frame);
        }
        frame.setVisible(true);
        frame.repaint();
    }

}

public class Main {

    public static final void addKeyBinding(JComponent c, String key, final Action action) {
        c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key);
        c.getActionMap().put(key, action);
        c.setFocusable(true);
    }

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Fullscreen Toggle Test");
        Container contentPane = frame.getContentPane();
        contentPane.add(new JLabel("Hey"), BorderLayout.CENTER);
        frame.add(new JLabel(new ImageIcon("C:/Users/SamBr/Pictures/DesktopBackgrounds/image.png")));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(960, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setTitle("Virtual World");
        addKeyBinding(frame.getRootPane(), "F11", new FullSceenToggleAction(frame));
    }

}

当我运行代码时,我看到图像,但在它前面或屏幕上的任何地方没有文字“嘿”我不明白它为什么没有显示或者我怎么能在图像前面看到它。 / p>

2 个答案:

答案 0 :(得分:3)

我告诉你之前的原因 - 你要向contentPane添加两个组件,第二个组件正在取代第一个组件。

请注意,这两行代码会将一个组件添加到相同的容器中,即JFrame的contentPane:

    contentPane.add(new JLabel("Hey"), BorderLayout.CENTER);
    frame.add(new JLabel(
           new ImageIcon("C:/Users/SamBr/Pictures/DesktopBackgrounds/image.png")));

根据the JFrame API

  

为方便起见,重写了此类的add,remove和setLayout方法,以便它们将调用委托给ContentPane的相应方法。

此外,默认情况下,contentPane使用BorderLayout,如果在不使用第二个参数常量的情况下添加组件,则默认情况下会将组件添加到BorderLayout.CENTER位置。

如果你想要两者都显示,那么

  1. 使用与容器的默认BorderLayout
  2. 不同的布局
  3. 保留BorderLayout,但将两个JLabel添加到两个不同的BorderLayout位置,例如BorderLayout.CENTER,BorderLayout.PAGE_START,......
  4. 使用JLayeredPane来保存图层
  5. 在图像绘制JPanel的paintComponent方法中绘制图像,并将“Hey”JLabel添加到此JPanel
  6. ....其他解决方案。

答案 1 :(得分:1)

谁知道你想要做什么。

但是这里有一些示例,根据您的要求在不同位置的标签上显示文字:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class LabelImageText extends JPanel
{
    public LabelImageText()
    {
        JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
        label1.setText( "Easy Way" );
        label1.setHorizontalTextPosition(JLabel.CENTER);
        label1.setVerticalTextPosition(JLabel.CENTER);
        add( label1 );

        //

        JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
        label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
        add( label2 );

        JLabel text = new JLabel( "More Control" );
        text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label2.add( Box.createVerticalGlue() );
        label2.add( text );
        label2.add( Box.createVerticalStrut(10) );

        //

        JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
        label3.setLayout( new GridBagLayout() );
        add( label3 );

        JLabel text3 = new JLabel();
        text3.setText("<html><center>Text<br>over<br>Image<center></html>");
        text3.setLocation(20, 20);
        text3.setSize(text3.getPreferredSize());
        label3.add( text3 );

        //

        JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
        add( label4 );

        JTextPane textPane = new JTextPane();
        textPane.setText("Add some text that will wrap at your preferred width");
        textPane.setEditable( false );
        textPane.setOpaque(false);
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        StyledDocument doc = textPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
        textPane.setBounds(20, 20, 75, 100);
        label4.add( textPane );
    }

    public static class ColorIcon implements Icon
    {
        private Color color;
        private int width;
        private int height;

        public ColorIcon(Color color, int width, int height)
        {
            this.color = color;
            this.width = width;
            this.height = height;
        }

        public int getIconWidth()
        {
            return width;
        }

        public int getIconHeight()
        {
            return height;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }

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

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

简单的解决方案只使用带有图标和文本的单个标签。其他人使用其他组件并利用布局管理器。