2012-08-09 66 views
4

我想要全屏顯示並保持內部順序。如何將JFrame放入FullScreen並自動重新調整內容

我應該如何把JFrame的進入全屏內重新調整一切:圖像,生成圖紙等。(某事像放大它,所以內容會適合屏幕)。

問題是我正在製作全屏應用程序,但我不知道它將在什麼屏幕上顯示。

這將使幀到全屏,但內容不會被重新調整

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

回答

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

     }}); 
    } 

} 
5

取決於它是什麼,你要縮放。

  • 如果繪製使用的Java2D它的圖形,只是弄清楚的東西需要多少按比例放大和使用Graphics2D.scale()來appropiately縮放GFX。

  • 如果它具有Swing佈局,請使用佈局管理器進行自適應佈局。

  • 如果別的東西,詳細說明你的問題

+0

是否有任何選項只是放大內容(不管它是什麼),使其充滿大/小面積?你放大圖片,壁紙等的方式? – Yoda 2012-08-09 13:28:33

+0

那是什麼Graphics2D.scale *的* – Durandal 2012-08-09 13:47:23

+0

但如果將調整按鈕? – Yoda 2012-08-09 13:51:52