简体   繁体   中英

JComboBox return index value

I have been trying to write a game recently, and I've been coding the GUI. Since I'm a new programmer, and want to keep it simple, I want to use only a resolution selector (fullscreen maybe later). I have the CB, I have an action listener to return the value, and a button to substitute the new resolution measurements. However, every time I run the code, I try to change the resolution and nothing happens.

Anyone have any ideas?

And also, I was wondering how to make it so that there is a stock resolution on first run, but it then saves your selected resolution.

Thank you!

Jack

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);
    }

}

you should call setSize() after you change the resolution .and then call setVisible(true) for main frame. if you want to hide optionscreen window call setVisible(false) or dispose() method.

apply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resX = tempResX;
                resY = tempResY;
                main.setSize(resX, resY);// apply resolution 
                main.setVisible(true);
                //optionsScreen.setVisible(false); // hide optionscreen  frame

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM