我陷入两难境地。我正在尝试制作游戏,并且在设置中有一个按钮可将屏幕设置为全屏并返回默认状态。我这样做的方式是我有一个名为Screen的JFrame类(我知道它更有效地捆绑你的swing组件)和一个名为MainMenu的JPanel类。现在,在MainMenu类中,我有所有基本按钮,什么不是。为了将屏幕设置为全屏,我创建了一个名为isFullscreen的布尔值。当按下名为Fullscreen的Jbutton时,此布尔值会发生变化,因此Screen类可以处理JFrame并以全屏模式重新创建它。
这是我的代码:
的MainMenu
public class MainMenu extends JPanel {
Kingdomcraft kd;
MainMenu mm;
Screen screen;
JButton playSP;
JButton playMP;
JButton settings;
JButton fullscreen;
JButton quit;
JButton createWorld;
JButton addServer;
JSlider sound;
JSlider light;
JList worldList;
JList serverList;
JTextField worldName;
JTextField serverName;
JTextField serverIP;
JButton addNewWorld;
JButton addNewServer;
private Preferences prefs;
private int soundLevel;
private int lightLevel;
public boolean isFullscreen;
public static boolean serverNameFilled = false;
public static boolean serverIPFilled = false;
public void run() {
kd = new Kingdomcraft();
mm = new MainMenu();
screen = new Screen();
playSP = new JButton("Singleplayer");
playMP = new JButton("Multiplayer");
settings = new JButton("Settings");
fullscreen = new JButton("Fullscreen");
quit = new JButton("Quit");
createWorld = new JButton("Create World");
addServer = new JButton("Add Server");
sound = new JSlider();
light = new JSlider();
prefs = Preferences.userNodeForPackage(MainMenu.class);
soundLevel = prefs.getInt("SOUND_LEVEL", 50);
lightLevel = prefs.getInt("LIGHT_LEVEL", 100);
isFullscreen = prefs.getBoolean("FULLSCREEN", false);
worldList = new JList();
serverList = new JList();
worldName = new JTextField();
serverName = new JTextField();
serverIP = new JTextField();
addNewWorld = new JButton("Add");
addNewServer = new JButton("Add");
if (kd.inMainMenu) {
add(playSP);
playSP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(fullscreen);
remove(addServer);
remove(serverName);
remove(serverIP);
remove(addNewServer);
repaint();
add(createWorld);
createWorld.setSize(110, 25);
createWorld.setLocation(playSP.getX() - (playSP.getWidth() / 2) - 5, playSP.getY() + 35);
createWorld.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(worldName);
worldName.setSize(110, 25);
worldName.setLocation(createWorld.getX(), createWorld.getY() + 35);
worldName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(addNewWorld);
addNewWorld.setSize(110, 25);
addNewWorld.setLocation(worldName.getX(), worldName.getY() + 35);
}
});
}
});
}
});
add(playMP);
playMP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(fullscreen);
remove(createWorld);
remove(worldName);
remove(addNewWorld);
repaint();
add(addServer);
addServer.setSize(100, 25);
addServer.setLocation(playMP.getX() - (playMP.getWidth() / 2) - 5, playMP.getY() + 35);
addServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(serverName);
serverName.setSize(100, 25);
serverName.setLocation(addServer.getX(), addServer.getY() + 35);
serverName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.serverNameFilled = true;
}
});
add(serverIP);
serverIP.setSize(100, 25);
serverIP.setLocation(serverName.getX(), serverName.getY() + 35);
serverIP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.serverIPFilled = true;
}
});
if (serverNameFilled && serverIPFilled) {
add(addNewServer);
addNewServer.setSize(100, 25);
addNewServer.setLocation(serverIP.getX(), serverIP.getY() + 35);
addNewServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}
});
}
});
add(settings);
settings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(createWorld);
remove(addServer);
remove(worldName);
remove(serverName);
remove(serverIP);
remove(addNewWorld);
remove(addNewServer);
repaint();
add(sound);
sound.setSize(settings.getWidth(), settings.getHeight());
sound.setLocation(settings.getX() + (settings.getWidth() / 2) + 5, settings.getY() + 35);
sound.setOpaque(false);
sound.setMinimum(0);
sound.setMaximum(100);
sound.setValueIsAdjusting(true);
sound.setValue(soundLevel);
sound.setToolTipText("Audio: " + soundLevel + "%");
sound.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
soundLevel = sound.getValue();
sound.setToolTipText("Audio: " + soundLevel + "%");
prefs.putInt("SOUND_LEVEL", soundLevel);
}
});
add(light);
light.setSize(settings.getWidth(), settings.getHeight());
light.setLocation(sound.getX(), sound.getY() + 35);
light.setOpaque(false);
light.setMinimum(50);
light.setMaximum(150);
light.setValueIsAdjusting(true);
light.setValue(lightLevel);
light.setToolTipText("Brightness: " + lightLevel + "%");
light.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
lightLevel = light.getValue();
light.setToolTipText("Brightness: " + lightLevel + "%");
prefs.putInt("LIGHT_LEVEL", lightLevel);
}
});
add(fullscreen);
fullscreen.setSize(100, settings.getHeight());
fullscreen.setLocation(settings.getX() + (settings.getWidth() / 2) - 50, light.getY() + 35);
fullscreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFullscreen) {
isFullscreen = true;
} else if(isFullscreen) {
isFullscreen = false;
}
prefs.putBoolean("FULLSCREEN", isFullscreen);
screen.dispose();
screen.run();
}
});
}
});
add(quit);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(ABORT);
}
});
}
revalidate();
}
}
Screen类:
public class Screen extends JFrame {
private Kingdomcraft kd;
private MainMenu mm;
private Dimension min = new Dimension(800, 600);
private ImageIcon img;
private GraphicsDevice gd;
private GraphicsEnvironment ge;
public void run() {
kd = new Kingdomcraft();
mm = new MainMenu();
img = new ImageIcon("assets/textures/items/ItemSwordIron.png");
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
add(kd);
setContentPane(mm);
setMinimumSize(min);
setTitle("Kingdomcraft");
setIconImage(img.getImage());
setBackground(Color.BLACK);
setLocation(WIDTH / 2, HEIGHT / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
mm.setOpaque(false);
mm.run();
if (mm.isFullscreen) {
setResizable(false);
gd.setFullScreenWindow(this);
repaint();
}
}
}
虽然您大致只需要从MainMenu类和Screen类中查看此代码:
add(fullscreen);
fullscreen.setSize(100, settings.getHeight());
fullscreen.setLocation(settings.getX() + (settings.getWidth() / 2) - 50, light.getY() + 35);
fullscreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFullscreen) {
isFullscreen = true;
} else if(isFullscreen) {
isFullscreen = false;
}
prefs.putBoolean("FULLSCREEN", isFullscreen);
screen.dispose();
screen.run();
}
我的问题是,无论我尝试搜索多长时间,它都不会处理屏幕类,尽管它会创建一个具有正确屏幕大小的新窗口。我不想要多个窗口。
我尝试了什么:
在Screen类中创建一个方法,该方法处理屏幕并进入JButton并添加screen.end();没运气。请帮帮我......
编辑:
新代码:
屏幕类:
public class Screen {
private MainMenu mm;
private Dimension min = new Dimension(800, 600);
private ImageIcon img;
private GraphicsDevice gd;
private GraphicsEnvironment ge;
public static JFrame frame;
public void run() {
mm = new MainMenu();
img = new ImageIcon("assets/textures/items/ItemSwordIron.png");
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
frame = new JFrame();
frame.add(new Kingdomcraft());
frame.setContentPane(mm);
frame.setMinimumSize(min);
frame.setTitle("Kingdomcraft");
frame.setIconImage(img.getImage());
frame.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
mm.setOpaque(false);
mm.run();
if (mm.isFullscreen) {
frame.setResizable(false);
gd.setFullScreenWindow(frame);
frame.repaint();
}
}
}
的MainMenu:
public class MainMenu extends JPanel {
Screen screen;
JButton playSP;
JButton playMP;
JButton settings;
JButton fullscreen;
JButton quit;
JButton createWorld;
JButton addServer;
JSlider sound;
JSlider light;
JTextField worldName;
JTextField serverName;
JTextField serverIP;
JButton addNewWorld;
JButton addNewServer;
private Preferences prefs;
private int soundLevel;
private int lightLevel;
public boolean isFullscreen;
public static boolean serverNameFilled = false;
public static boolean serverIPFilled = false;
public void run() {
screen = new Screen();
playSP = new JButton("Singleplayer");
playMP = new JButton("Multiplayer");
settings = new JButton("Settings");
fullscreen = new JButton("Fullscreen");
quit = new JButton("Quit");
createWorld = new JButton("Create World");
addServer = new JButton("Add Server");
sound = new JSlider();
light = new JSlider();
prefs = Preferences.userNodeForPackage(MainMenu.class);
soundLevel = prefs.getInt("SOUND_LEVEL", 50);
lightLevel = prefs.getInt("LIGHT_LEVEL", 100);
isFullscreen = prefs.getBoolean("FULLSCREEN", false);
worldName = new JTextField();
serverName = new JTextField();
serverIP = new JTextField();
addNewWorld = new JButton("Add");
addNewServer = new JButton("Add");
if (new Kingdomcraft().inMainMenu) {
add(playSP);
playSP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(fullscreen);
remove(addServer);
remove(serverName);
remove(serverIP);
remove(addNewServer);
repaint();
add(createWorld);
createWorld.setSize(110, 25);
createWorld.setLocation(playSP.getX() - (playSP.getWidth() / 2) - 5, playSP.getY() + 35);
createWorld.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(worldName);
worldName.setSize(110, 25);
worldName.setLocation(createWorld.getX(), createWorld.getY() + 35);
worldName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(addNewWorld);
addNewWorld.setSize(110, 25);
addNewWorld.setLocation(worldName.getX(), worldName.getY() + 35);
}
});
}
});
}
});
add(playMP);
playMP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(fullscreen);
remove(createWorld);
remove(worldName);
remove(addNewWorld);
repaint();
add(addServer);
addServer.setSize(100, 25);
addServer.setLocation(playMP.getX() - (playMP.getWidth() / 2) - 5, playMP.getY() + 35);
addServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(serverName);
serverName.setSize(100, 25);
serverName.setLocation(addServer.getX(), addServer.getY() + 35);
serverName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.serverNameFilled = true;
}
});
add(serverIP);
serverIP.setSize(100, 25);
serverIP.setLocation(serverName.getX(), serverName.getY() + 35);
serverIP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.serverIPFilled = true;
}
});
if (serverNameFilled && serverIPFilled) {
add(addNewServer);
addNewServer.setSize(100, 25);
addNewServer.setLocation(serverIP.getX(), serverIP.getY() + 35);
addNewServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}
});
}
});
add(settings);
settings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(createWorld);
remove(addServer);
remove(worldName);
remove(serverName);
remove(serverIP);
remove(addNewWorld);
remove(addNewServer);
repaint();
add(sound);
sound.setSize(settings.getWidth(), settings.getHeight());
sound.setLocation(settings.getX() + (settings.getWidth() / 2) + 5, settings.getY() + 35);
sound.setOpaque(false);
sound.setMinimum(0);
sound.setMaximum(100);
sound.setValueIsAdjusting(true);
sound.setValue(soundLevel);
sound.setToolTipText("Audio: " + soundLevel + "%");
sound.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
soundLevel = sound.getValue();
sound.setToolTipText("Audio: " + soundLevel + "%");
prefs.putInt("SOUND_LEVEL", soundLevel);
}
});
add(light);
light.setSize(settings.getWidth(), settings.getHeight());
light.setLocation(sound.getX(), sound.getY() + 35);
light.setOpaque(false);
light.setMinimum(50);
light.setMaximum(150);
light.setValueIsAdjusting(true);
light.setValue(lightLevel);
light.setToolTipText("Brightness: " + lightLevel + "%");
light.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
lightLevel = light.getValue();
light.setToolTipText("Brightness: " + lightLevel + "%");
prefs.putInt("LIGHT_LEVEL", lightLevel);
}
});
add(fullscreen);
fullscreen.setSize(100, settings.getHeight());
fullscreen.setLocation(settings.getX() + (settings.getWidth() / 2) - 50, light.getY() + 35);
fullscreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFullscreen) {
isFullscreen = true;
} else if(isFullscreen) {
isFullscreen = false;
}
prefs.putBoolean("FULLSCREEN", isFullscreen);
Screen.frame.dispose();
screen.run();
}
});
}
});
add(quit);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(ABORT);
}
});
}
revalidate();
}
}
答案 0 :(得分:1)
正如this answer中所述,您正在创建多个类的实例,以及一些希望彼此了解彼此的方式......
new Screen(); new Screen()
会为您的班级创建两个不同的实例,每个实例都是自己的JFrame
,它们彼此无关......
从Kingdomcraft
...
private Screen screen;
public Kingdomcraft() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
screen = new Screen(Kingdomcraft.this);
}
});
}
//...
public static void main(String[] args) {
Kingdomcraft kd = new Kingdomcraft();
// This does nothing...
//Screen screen = new Screen();
kd.start();
//screen.run();
}
从此课程中删除所有new Screen()
和new MainMenu()
...
在Screen
...
public class Screen extends JFrame {
private Kingdomcraft kd;
private MainMenu mm;
private Dimension min = new Dimension(800, 600);
private ImageIcon img;
public Screen(Kingdomcraft kingdomcraft) {
kd = kingdomcraft;
mm = new MainMenu(this);
//...
}
public Kingdomcraft getKingdomcraft() {
return kd;
}
从此课程中删除所有new Kingdomcraft()
和new Screen()
。对于内部类,您需要将screen
的引用更改为this
或Screen.this
...
同样,您可以将Kingdomcraft
的引用与屏幕一起传递给MainMenu
,但这只是另一种方式...
MainMenu
public class MainMenu extends JPanel {
Screen screen;
//...
public MainMenu(Screen srn) {
this.screen = srn;
//...
if (screen.getKingdomcraft().inMainMenu) {
您必须从此课程中删除所有new Kingdomcraft()
,new Screen()
和new MainMenu()
...