1
我一直在試圖寫一個遊戲最近,我一直在編寫GUI。由於我是一名新程序員,並且希望保持簡單,所以我只想使用分辨率選擇器(全屏或稍後)。我有CB,我有一個動作監聽器來返回值,還有一個按鈕來替代新的分辨率測量值。但是,每次運行代碼時,我都會嘗試更改分辨率並且什麼都不會發生。JComboBox返回索引值
任何人有任何想法?
此外,我想知道如何使它在首次運行時有一個股票決議,但它然後保存您選擇的決議。
謝謝!
傑克
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
public class GUI_Test{
private JPanel window;
private JPanel settings;
private JFrame main;
private JFrame optionsScreen;
private JLabel headerLabel;
private JLabel optionsLabel;
private JLabel resolution;
private JComboBox resolutonOption;
String[] resolutionOptions = new String[] {
"640x480", "1024x768", "1366x768", "1600x900", "1920x1080"
};
int tempResX, tempResY, res;
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int resX = gd.getDisplayMode().getWidth();
int resY = gd.getDisplayMode().getHeight();
public GUI_Test(){
prepareGUI();
}
public static void main(String[] args) {
GUI_Test GUI = new GUI_Test();
GUI.showGUIDemo();
}
private void showGUIDemo() {
JButton exit = new JButton("EXIT");
JButton options = new JButton("OPTIONS");
JButton back = new JButton("BACK");
JButton apply = new JButton("APPLY");
JComboBox <String> resolutionOption = new JComboBox<>(resolutionOptions);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
options.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
optionsScreen.setVisible(true);
settings.setVisible(true);
window.setVisible(false);
main.setVisible(false);
}
});
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
optionsScreen.setVisible(false);
settings.setVisible(false);
window.setVisible(true);
main.setVisible(true);
}
});
resolutionOption.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
res = resolutionOption.getSelectedIndex();
switch (res) {
case 0:
tempResX = 640;
tempResY = 480;
break;
case 1:
tempResX = 1024;
tempResY = 768;
break;
case 2:
tempResX = 1366;
tempResY = 768;
break;
case 3:
tempResX = 1600;
tempResY = 900;
break;
case 4:
tempResX = 1920;
tempResY = 1080;
break;
}
}
});
apply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resX = tempResX;
resY = tempResY;
}
});
window.add(exit);
window.add(options);
settings.add(back);
settings.add(resolutionOption);
settings.add(apply);
main.setVisible(true);
}
private void prepareGUI() {
main = new JFrame("Basic GUI");
main.setSize(resX, resY);
main.setLayout(new GridLayout(3,1));
main.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
optionsScreen = new JFrame("Options");
optionsScreen.setSize(resX, resY);
optionsScreen.setLayout(new GridLayout(4,1));
optionsScreen.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
headerLabel = new JLabel("AGame", JLabel.CENTER);
headerLabel.setSize(100, 50);
optionsLabel = new JLabel("Settings", JLabel.CENTER);
optionsLabel.setSize(100, 50);
resolution = new JLabel("Resolution", JLabel.CENTER);
window = new JPanel();
window.setLayout(new FlowLayout());
settings = new JPanel();
settings.setLayout(new FlowLayout());
main.add(headerLabel);
main.add(window);
main.setVisible(true);
optionsScreen.add(optionsLabel);
optionsScreen.add(settings);
optionsScreen.add(resolution);
optionsScreen.setVisible(false);
settings.setVisible(false);
}
}
你應該使'optionsScreen'成爲JPanel/JDialog而不是JFrame。對於JPanel,一個選項是使用[CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)。順便說一句,你不需要努力添加一個windowlistener到每一幀,以確保一切都關閉,而不是你可以調用'frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);' –