简体   繁体   English

如何将JFrame放入FullScreen并自动重新缩放内容

[英]How to put JFrame into FullScreen and automatically rescale content

I want go full screen and keep everything inside in order. 我想全屏显示并按顺序保存所有内容。

How should i put the JFrame into full screen AND rescale everything inside: images, generated drawings etc.(sth like zooming it up so the content will fit the screen). 我应该如何将JFrame放入全屏重新调整内部的所有内容:图像,生成的图纸等(例如,将其放大以使内容适合屏幕)。

The problem is I am making full screen app, but I don't know on what screen it will be displayed. 问题是我正在制作全屏应用,但我不知道它将在什么屏幕上显示。

This will put the frame into fullscreen, but the content will not be rescaled 这会将帧置于全屏,但内容不会重新调整

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

Depends on what it is that you want to scale. 取决于您想要扩展的内容。

  • If its graphics you draw using Java2D, just figure out how much the stuff needs to be scaled up and use Graphics2D.scale() to scale the gfx appropiately. 如果你使用Java2D绘制它的图形,只需计算出需要扩展多少东西,并使用Graphics2D.scale()来适当地缩放gfx。

  • If its something with a Swing layout, use a Layout manager to make an adaptive layout. 如果它具有Swing布局,请使用布局管理器进行自适应布局。

  • If its something else, elaborate on your problem 如果是别的,请详细说明你的问题

If this really is what you want to do (see warnings from other answers), it's not too hard to do (but takes a little time to figure out). 如果这真的是你想要做的事情(参见其他答案的警告),那么做起来并不难(但需要一点时间来弄明白)。 Basically, it involves extending JPanel , and then overwriting the paint method. 基本上,它涉及扩展JPanel ,然后覆盖paint方法。

Here's a sample that I came up with: 这是我提出的一个示例:

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

        }}); 
    } 

} 

few days back I just worked with an java full screen app. 几天前我刚刚使用java全屏应用程序。 Have a look at the following link. 看看以下链接。 if that was your requirement I can help you to some extent. 如果这是你的要求,我可以在一定程度上帮助你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM