繁体   English   中英

JComboBox返回索引值

[英]JComboBox return index value

我最近一直在尝试编写游戏,并且一直在编写GUI。 由于我是一名新程序员,并且希望保持简单,因此我只想使用分辨率选择器(可能稍后使用全屏显示)。 我有CB,我有一个动作侦听器来返回值,还有一个按钮来替代新的分辨率测量值。 但是,每次我运行代码时,我都尝试更改分辨率,但没有任何反应。

有人有想法么?

而且,我想知道如何做到这一点,以便在首次运行时就有一个股票分辨率,但是它会保存您选择的分辨率。

谢谢!

插口

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

}

您应该在更改分辨率后调用setSize() ,然后为主框架调用setVisible(true) 如果要隐藏选项屏幕窗口,请调用setVisible(false)dispose()方法。

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

            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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