[英]How to put JFrame into FullScreen and automatically rescale content
我想全屏顯示並按順序保存所有內容。
我應該如何將JFrame放入全屏並重新調整內部的所有內容:圖像,生成的圖紙等(例如,將其放大以使內容適合屏幕)。
問題是我正在制作全屏應用,但我不知道它將在什么屏幕上顯示。
這會將幀置於全屏,但內容不會重新調整
frame.dispose();
frame.setUndecorated(true);
frame.setLocation(0, 0);
frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
frame.setVisible(true);
frame.repaint();
取決於您想要擴展的內容。
如果你使用Java2D繪制它的圖形,只需計算出需要擴展多少東西,並使用Graphics2D.scale()來適當地縮放gfx。
如果它具有Swing布局,請使用布局管理器進行自適應布局。
如果是別的,請詳細說明你的問題
如果這真的是你想要做的事情(參見其他答案的警告),那么做起來並不難(但需要一點時間來弄明白)。 基本上,它涉及擴展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();
}});
}
}
幾天前我剛剛使用java全屏應用程序。 看看以下鏈接。 如果這是你的要求,我可以在一定程度上幫助你。
https://docs.google.com/open?id=0B9U-BwYu62ZaeDM3SWZhaTdSYzQ
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.