I have a simple frame that have a button to toggle whether that frame is full screen or not. I want to have it so that the user only has to press the enter key to toggle the full screen. I am very new to Java. I would like the event listener to be in private FrontEnd()
import java.awt.event.*;
import javax.swing.*;
public class FrontEnd extends JFrame {
private boolean FullScreenMode = false;
private int PrevX, PrevY, PrevWidth, PrevHeight;
public static void main(String[] args) {
FrontEnd frame = new FrontEnd();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480, 320);
frame.setVisible(true);
}
private FrontEnd() {
JPanel contentPane = new JPanel();
setContentPane(contentPane);
// From Here starts the trick
FullScreenEffect effect = new FullScreenEffect();
JButton fullscreenButton = new JButton("Fullscreen Mode");
fullscreenButton.addActionListener(effect);
contentPane.add(fullscreenButton);
fullscreenButton.setVisible(true);
}
private class FullScreenEffect implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
if (!FullScreenMode) {
PrevX = getX();
PrevY = getY();
PrevWidth = getWidth();
PrevHeight = getHeight();
dispose();
setUndecorated(true);
setBounds(0, 0, getToolkit().getScreenSize().width, getToolkit().getScreenSize().height);
setVisible(true);
FullScreenMode = true;
} else {
dispose();
setUndecorated(false);
setBounds(PrevX, PrevY, PrevWidth, PrevHeight);
setVisible(true);
FullScreenMode = false;
}
}
}