【问题标题】:How to put JFrame into FullScreen and automatically rescale content如何将 JFrame 放入 FullScreen 并自动重新缩放内容
【发布时间】:2012-08-09 13:00:07
【问题描述】:

我想全屏显示并保持里面的一切井井有条。

我应该如何将 JFrame 放入全屏 AND 重新缩放里面的所有内容:图像、生成的绘图等(比如放大它以使内容适合屏幕)。

问题是我正在制作全屏应用,但我不知道它将显示在哪个屏幕上。

这将使框架进入全屏状态,但内容不会重新缩放

   frame.dispose();
   frame.setUndecorated(true);
   frame.setLocation(0, 0);
   frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
   frame.setVisible(true);
   frame.repaint();

【问题讨论】:

    标签: java swing awt fullscreen


    【解决方案1】:

    取决于您想要扩展的是什么。

    • 如果您使用 Java2D 绘制它的图形,只需弄清楚需要放大多少东西,然后使用 Graphics2D.scale() 适当地缩放 gfx。

    • 如果它带有 Swing 布局,请使用布局管理器来制作自适应布局。

    • 如果是其他问题,请详细说明您的问题

    【讨论】:

    • 是否有任何选项可以缩放内容(无论它是什么)以填充更大/更小的区域?你缩放图片、壁纸等的方式?
    • 这就是 Graphics2D.scale 的作用
    • @RobertKilar :根据您的需要使用适当的LayoutManager,而不是使用Absolute Positioning
    【解决方案2】:

    如果这确实是您想要做的(请参阅其他答案中的警告),那么做起来并不难(但需要一点时间才能弄清楚)。基本上,它涉及扩展JPanel,然后覆盖paint 方法。

    这是我想出的一个示例:

    import java.awt.*;
    import java.awt.image.BufferedImage;    
    import javax.swing.*;
    
    
    public class CustomPanel extends JPanel{ 
    
        Component myComponent;
    
        public CustomPanel(){
            super();
            setLayout(null);
        }
    
        /**
         * Only allows one component to be added
         */
        @Override
        public Component add(Component c){
            super.add(c);
            c.setLocation(0, 0);
            c.setSize(c.getPreferredSize());
            myComponent = c;
            return c;
        }
    
        @Override
        public void paint(final Graphics g){
    
            Dimension d = this.getSize();               
            Dimension p = myComponent.getPreferredSize();
    
            // Paints the child component to a image
            BufferedImage newImg = new BufferedImage(p.width, p.height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = newImg.createGraphics();
            super.paint(g2d);
    
            // Resizes the image if necessary
            Image img;
            if(d.height > p.height && d.width > p.width){
                System.out.println("Scaled");
    
                float changePercentage = 0;
                if(d.height/p.height > d.width/p.width){
                    changePercentage = (float)d.width/(float)p.width;
                } else{
                    changePercentage = (float)d.height/(float)p.height;
                }
                System.out.println(changePercentage);
    
                int newHeight = ((Float)(p.height * changePercentage)).intValue();
                int newWidth = ((Float)(p.width * changePercentage)).intValue();
    
                img = newImg.getScaledInstance(newWidth, newHeight, 0);             
            } else{
                System.out.println("Not Scaled");
                img = newImg;
            }
    
            // Paints the image of the child component to the screen.
            g.drawImage(img, 0, 0, null);
        }
    
        public static void main(String[] args) { 
            // TODO Auto-generated method stub 
            SwingUtilities.invokeLater(new Runnable(){public void run(){
    
                JFrame frame = new JFrame("Zoom Panel"); 
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                frame.setSize(300, 200); 
    
                CustomPanel buffer = new CustomPanel();
                JPanel content = new JPanel();
                content.add(new JLabel("Bogus"));
                content.setBackground(Color.red);
                buffer.add(content);
                frame.setContentPane(buffer);
    
                frame.setVisible(true); 
                new CustomPanel();
    
            }}); 
        } 
    
    } 
    

    【讨论】:

      【解决方案3】:

      几天前,我刚刚使用了一个 java 全屏应用程序。看看下面的链接。如果这是您的要求,我可以在一定程度上帮助您。

      https://docs.google.com/open?id=0B9U-BwYu62ZaeDM3SWZhaTdSYzQ

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        • 2016-07-06
        相关资源
        最近更新 更多