如何将JFrame设置为全屏并自动缩放内容

4

我想要全屏显示并保持所有内容的完整性。

如何将JFrame设置为全屏 并且 重新调整内部的所有内容:图像,生成的绘图等(类似于缩放以适应屏幕的内容)。

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

这将使框架进入全屏模式,但内容将不会被重新缩放。

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

5

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

  • 如果是使用Java2D绘制的图形,则只需确定需要缩放的内容量,并使用Graphics2D.scale()适当地缩放图像。

  • 如果涉及Swing布局,请使用布局管理器创建自适应布局。

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


有没有选项只是缩放内容(无论是什么),使其填充更大/更小的区域?就像你缩放图片、壁纸等一样? - Yoda
这就是 Graphics2D.scale 所做的 - Durandal
2
@RobertKilar:根据您的需求使用适当的LayoutManager,而不是使用Absolute Positioning - nIcE cOw

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

        }}); 
    } 

} 

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接