有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在ImageIcon前面移动JLabel?

我认为代码中的所有内容都是正确的,我只需要知道如何分类不同的层

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));
    }

}

当我运行代码时,我看到了图像,但在图像前面或屏幕上的任何地方都没有文本“嘿”,我不明白为什么它没有显示,或者我如何使它在图像前面可见


共 (2) 个答案

  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();
                }
            });
        }
    }
    

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

  2. # 2 楼答案

    我在之前告诉过你原因,你在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

    As a convenience, the add, remove, and setLayout methods of this class are overridden, so that they delegate calls to the corresponding methods of the ContentPane.

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

    如果希望两者都显示,则

    1. 使用不同于容器默认边界布局的布局
    2. 保留BorderLayout,但将两个JLabel添加到两个不同的BorderLayout位置,例如BorderLayout。中心,边界布局。页面开始
    3. 使用JLayeredPane保存图层
    4. 在image drawing JPanel的paintComponent方法中绘制图像,并将“Hey”JLabel添加到此JPanel
    5. 。。。。其他解决方案