我在main中定义了我的JFrame,我想这样做,如果我点击一个按钮,按F等,屏幕全屏显示。我知道如何做到这一点,但我不明白如何从主要的setFullScreenWindow获取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(),btw中添加参数。中间有许多复杂而必要的步骤。
答案 0 :(得分:0)
您的问题有点不清楚,但听起来您希望在窗口模式和全屏模式之间切换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();
}
}