有 Java 编程相关的问题?

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

java如何使用图像填充边框布局制作JLabel。居中

我得到了一个JFrame并将LayoutManager设置为BorderLayout,然后继续添加带有图像的JLabel。但是,当我调整帧的大小时,JLabel不会调整大小。我没有向North、S、E等添加任何组件。我只是希望标签里的图像能填满整个画面,当然,我的菜单也能保持原样


共 (2) 个答案

  1. # 1 楼答案

    如果这看起来很傲慢,请原谅我,但我没有别的事要说

    我做了一个快速样品

    BorderLayout wide BorderLayout narrow

    请参见图像周围的红线,这是JLabel的边界。如您所见,标签已重新调整大小以填充整个区域

    这是我用来生成示例的代码

    public class LayoutFrame extends JFrame {
    
        public LayoutFrame() throws HeadlessException {
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            Image image = null;
            URL url = getClass().getResource("/layout/issue78.jpg");
            try {
                image = ImageIO.read(url);
            } catch (IOException ex) {
    
                ex.printStackTrace();
    
            }
    
            JLabel label = new JLabel(new ImageIcon(image));
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setVerticalAlignment(JLabel.CENTER);
            label.setBorder(new LineBorder(Color.RED, 4));
    
            setLayout(new BorderLayout());
    
            add(label);
    
        }
    
        public static void main(String[] args) {
    
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
    
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    LayoutFrame frame = new LayoutFrame();
                    frame.setSize(200, 200);
                    frame.setLocationByPlatform(true);
                    frame.setVisible(true);
    
                }
            });
    
        }
    
    }
    

    显然,您需要提供自己的映像;)

    别忘了,标签不会为您扩展内容,如果这是您的目标,您需要实现自己的组件来实现这一点

    如果您仍然有问题,我建议(在没有进一步证据的情况下)您的标签可能不在您认为的容器中,或者容器布局管理器不是您认为的那样

    更新

    我不知道您为什么会遇到组件丢失或菜单问题。是否混合了重量重和重量轻的成分

    带有菜单栏的示例

    With menu bar

    在仔细阅读您的问题之后,我设计了一个简单的调整图像窗格大小的示例。为了提高速度,我依赖于我的库,但是实现自己的代码来代替我的调用应该相当容易

    public class ImagePane extends JPanel {
    
        protected static final Object RESIZE_LOCK = new Object();
    
        private BufferedImage image;
        private BufferedImage scaledImage;
        private Timer resizeTimer;
    
        public ImagePane() {
    
            URL url = getClass().getResource("/layout/issue78.jpg");
            try {
                image = ImageIO.read(url);
            } catch (IOException ex) {
    
                ex.printStackTrace();
    
            }
    
            resizeTimer = new Timer(250, new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
    
                    // Simple thread factory to start a slightly lower 
                    // priority thread.
                    CoreThreadFactory.getUIInstance().execute(new ResizeTask());
    
                }
    
            });
    
            resizeTimer.setCoalesce(true);
            resizeTimer.setRepeats(false);
    
        }
    
        @Override
        public void setBounds(int x, int y, int width, int height) {
    
            super.setBounds(x, y, width, height);
    
            resizeTimer.restart();
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D) g;
            if (scaledImage != null) {
    
                // This simply returns a rectangle that takes into consideration
                //the containers insets
                Rectangle safeBounds = UIUtilities.getSafeBounds(this);
    
                System.out.println("scaledImage = " + scaledImage.getWidth() + "x" + scaledImage.getWidth());
    
                int x = ((safeBounds.width - scaledImage.getWidth()) / 2) + safeBounds.x;
                int y = ((safeBounds.height - scaledImage.getHeight()) / 2) + safeBounds.y;
    
                g2d.drawImage(scaledImage, x, y, this);
    
            }
    
        }
    
        protected class ResizeTask implements Runnable {
    
            @Override
            public void run() {
    
                synchronized (RESIZE_LOCK) {
    
                    if (image != null) {
    
                        int width = getWidth();
                        int height = getHeight();
    
                        System.out.println("width = " + width);
                        System.out.println("height = " + height);
    
                        // A simple divide and conquer resize implementation
                        // this will scale the image so that it will fit within
                        // the supplied bounds
                        scaledImage = ImageUtilities.getScaledInstanceToFit(image, new Dimension(width, height), ImageUtilities.RenderQuality.High);
    
                        System.out.println("scaledImage = " + scaledImage.getWidth() + "x" + scaledImage.getWidth());
    
                        repaint(); // this is one of the few thread safe calls
    
                    }
    
                }
    
            }
    
        }
    
    }
    
  2. # 2 楼答案

    最好的选择是对ImageIcon进行子类化,并覆盖其paintIcon方法,以便使用图形简单地绘制图像。油漆(x、y、宽度、高度…)