[英]How do you get JFrame from main to make fullscreen?
我在main中定義了我的JFrame,因此我想這樣做,因此,如果單擊按鈕,按F等,屏幕將變為全屏。 我了解如何執行此操作,但不了解如何從setFullScreenWindow的main中獲取JFrame實例,例如如何通過使用getter來獲取播放器的x。
這是我的代碼示例:
private boolean fullscreen = false;
public static void main(String args[]){
Game game = new Game();
//Set size of game (not shown)
JFrame frame = new JFrame(game.TITLE);
frame.add(game);
//JFrame setup stuff (not shown)
game.start();
}
public void setFullScreen(JFrame frame){
if(isFullScreenSupported){
if(!fullscreen){
frame.setUndecorate(true);
gd.setFullScreenWindow(frame);
frame.validate;
} else{
gd.setFullScreenWindow(null);
}
}
}
我無法將參數添加到start(),順便說一句。 兩者之間有許多復雜且必要的步驟。
您的問題尚不清楚,但是聽起來您想在窗口和全屏模式之間切換JFrame。 這是一個獨立的示例,說明了如何執行此操作:
package example.stackoverflow;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class FullScreenExample
{
static class GameFrame extends JFrame
{
private static final long serialVersionUID = 5386744421065461862L;
private static final GraphicsDevice gd = (GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())[0];
private static final boolean FULLSCREEN_SUPPORTED = gd.isFullScreenSupported();
private static final String MAKE_FULLSCREEN_TEXT = "Make Full Screen";
private static final String MAKE_WINDOWED_TEXT = "Make Windowed";
private static final int WINDOWED_WIDTH = 400;
private static final int WINDOWED_HEIGHT = 300;
private final JButton fsButton = new JButton(MAKE_FULLSCREEN_TEXT);
private boolean isFullscreen = false;
public GameFrame(String title)
{
super(title);
setSize(WINDOWED_WIDTH, WINDOWED_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
}
public void initComponents()
{
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
if(FULLSCREEN_SUPPORTED)
{
fsButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
toggleFullscreen();
}
});
add(fsButton);
}
else
{
add(new JLabel("Fullscreen mode is not supported on this device"));
}
}
public void toggleFullscreen()
{
isFullscreen = !isFullscreen;
setVisible(false);
dispose();
setUndecorated(isFullscreen);
if(isFullscreen)
{
fsButton.setText(MAKE_WINDOWED_TEXT);
gd.setFullScreenWindow(this);
validate();
}
else
{
fsButton.setText(MAKE_FULLSCREEN_TEXT);
gd.setFullScreenWindow(null);
setVisible(true);
}
}
}
static class Game
{
private GameFrame gameFrame;
public Game(String title)
{
gameFrame = new GameFrame(title);
}
public void start()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
getGameFrame().setVisible(true);
}
});
}
public GameFrame getGameFrame()
{
return gameFrame;
}
}
public static void main(String[] args)
{
Game g = new Game("Foo");
g.start();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.