JAVA多屏幕

时间:2014-11-16 22:13:55

标签: java swing user-interface

我正在构建一个简单的1024游戏。主界面(或屏幕)已完成并正常工作,但我正在尝试添加另一个主界面的界面(或屏幕)。

我试图通过向同一个JFrame添加两个容器来实现这一目的;但由于某种原因,第一个容器无法看到,即使最后一个容器被设置为隐藏[setVisible(false)]。

我尝试过其他方法,例如拥有一个Container组件并将JPanels添加到CENTERWESTEAST空格中并切换它们之间的可见性,这样可行,但我可以告诉我这听起来像是一个糟糕的编程习惯,因为我无法使用这种方法添加超过3个屏幕。

是否有特定的方法或正确的方法来完成基本的程序流程?

这是我的GUI界面类的代码,我有3个其他类与游戏实现。另外,我的主要语言是西班牙语,所以大多数参考文献都是用西班牙语写的。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 

class Interfaz extends JFrame
{
private Controlador controlador;
private JOptionPane mensajeOpcion;

private Container contenedorJuego;
private Container contenedorMenu;


private JButton newGame;
private JButton startGame;

private JLabel recordLabel;
private JLabel puntajeLabel;
private JLabel[] labelFichas;
private JLabel marcador;

private JPanel tableroPanel;
private JPanel marcadorPanel;
private JPanel mandosPanel;

private final Color COLOR_0 = new Color(0,153,153);
private final Color COLOR_BG_FRAME = new Color(240,233,210);
private final Color COLOR_MARCADOR = new Color(163,155,127);
private final Color COLOR_MARCADOR_LABEL = new Color(240,233,210);
private final Color COLOR_CONTENIDO_LABELS = new Color(255,255,204);
private final Font FONT_PRINCIPAL = new Font("Helvetica",Font.PLAIN,24);
private final Font FONT_SECUNDARIA = new Font("Helvetica",Font.PLAIN,14);
private final Font FONT_MARCADOR = new Font("Impact",Font.BOLD,26);
private final String[] RESULTADO_FINAL = {"¡Volver a Jugar!","¡Salir!"};

private final ImageIcon FLECHA_ARRIBA = new ImageIcon(this.getClass().getResource("/imagenes/Flecha-Arriba.png"));
private final ImageIcon FLECHA_DERECHA = new ImageIcon(this.getClass().getResource("/imagenes/Flecha-Der.png"));
private final ImageIcon FLECHA_IZQUIERDA = new ImageIcon(this.getClass().getResource("/imagenes/Flecha-Izq.png"));
private final ImageIcon FLECHA_ABAJO = new ImageIcon(this.getClass().getResource("/imagenes/Flecha-Abajo.png"));

/**
 * Controlador de la clase que se encarga de inicializar el objeto Interfaz
 */

public Interfaz(Controlador elControlador)
{
    controlador = elControlador;
    mensajeOpcion = new JOptionPane();

    this.initComponent();
    this.makeMarcador(marcadorPanel);
    this.makeTablero(tableroPanel);
    this.makeMenu();
    this.agregarContenidoContenedorJuego();
    this.makeMandos(mandosPanel);
    this.configurar();
}

/**
 * Metodo que se encarga de inicializar los componentes del objeto interfaz
 */

public void initComponent()
{
    contenedorJuego = new Container();
    contenedorJuego.setLayout(new BorderLayout());

    contenedorMenu = new Container();

    newGame = new JButton("New Game");
    startGame = new JButton("Iniciar");
    recordLabel = new JLabel("0");
    puntajeLabel = new JLabel("0");
    labelFichas = new JLabel[16];
    marcador = new JLabel("0");

    marcadorPanel = new JPanel();
    marcadorPanel.setLayout(new BorderLayout());
    marcadorPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 0, 30));
    marcadorPanel.setBackground(COLOR_BG_FRAME);
    marcadorPanel.setOpaque(true);

    tableroPanel = new JPanel();
    tableroPanel.setLayout(new GridLayout(4,4,5,5));
    tableroPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 0, 30));
    tableroPanel.setBackground(COLOR_BG_FRAME);
    tableroPanel.setOpaque(true);

    mandosPanel = new JPanel();
    mandosPanel.setLayout(new FlowLayout());
    mandosPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));
    mandosPanel.setBackground(COLOR_BG_FRAME);
    mandosPanel.setOpaque(true);
}

public void makeMenu()
{
    startGame.setActionCommand("Iniciar");
    contenedorMenu.add(startGame);
    //this.add(contenedorMenu);
}

/**
 * Metodo que recibe un JPanel y le agrega los componentes necesarios
 * para poder mostrar el marcador con los puntajes correspondientes a
 * cada juego
 */

public void makeMarcador(JPanel panel)
{
    Box panelIzquierdo = new Box(1);
    panelIzquierdo.setBorder(BorderFactory.createEmptyBorder(13, 13, 13, 13));
    panelIzquierdo.setBackground(COLOR_0);
    panelIzquierdo.setOpaque(true);

    Box panelDerecho = new Box(1);
    panelDerecho.setBorder(BorderFactory.createEmptyBorder(13, 25, 13, 25));
    panelDerecho.setBackground(COLOR_0);
    panelDerecho.setOpaque(true);

    JButton nuevoJuego = new JButton("NUEVO JUEGO");
    nuevoJuego.setFocusable(false);
    nuevoJuego.setBackground(COLOR_MARCADOR);
    nuevoJuego.setForeground(Color.white);
    nuevoJuego.setOpaque(true);
    nuevoJuego.setBorderPainted(false);
    nuevoJuego.setFont(FONT_SECUNDARIA);
    nuevoJuego.setCursor(new Cursor(Cursor.HAND_CURSOR));
    nuevoJuego.setActionCommand("Nuevo");
    nuevoJuego.addActionListener(controlador);

    JLabel marcadorTexto = new JLabel("MARCADOR");
    marcadorTexto.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
    marcadorTexto.setAlignmentX(Component.CENTER_ALIGNMENT);
    marcadorTexto.setFont(FONT_SECUNDARIA);
    marcadorTexto.setForeground(Color.white);

    marcador.setFont(FONT_MARCADOR);
    marcador.setAlignmentX(Component.CENTER_ALIGNMENT);
    marcador.setForeground(Color.white);

    JLabel recordTexto = new JLabel("RECORD");
    recordTexto.setAlignmentX(Component.CENTER_ALIGNMENT);
    recordTexto.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
    recordTexto.setFont(FONT_SECUNDARIA);
    recordTexto.setForeground(Color.white);

    recordLabel.setFont(FONT_MARCADOR);
    recordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    recordLabel.setForeground(Color.white);

    JPanel panelPuntaje = new JPanel();
    panelPuntaje.setLayout(new FlowLayout());
    panelPuntaje.setBackground(COLOR_BG_FRAME);
    panelPuntaje.setOpaque(true);

    panelDerecho.add(recordTexto);
    panelDerecho.add(recordLabel);

    panelIzquierdo.add(marcadorTexto);
    panelIzquierdo.add(marcador);

    panelPuntaje.add(panelIzquierdo);
    panelPuntaje.add(panelDerecho);

    panel.add(nuevoJuego,BorderLayout.CENTER);
    panel.add(panelPuntaje,BorderLayout.EAST);
}

/**
 * Metodo que recibe un JPanel y le agrega todos los componentes necesarios para que pueda mostrar
 * el tablero con el cual el programa se comunicara con el usuario
 */

public void makeTablero(JPanel panel)
{
    for (int contador = 0; contador<16; contador++)
    {
        labelFichas[contador] = new JLabel();
        labelFichas[contador].setHorizontalAlignment(SwingConstants.CENTER);
        labelFichas[contador].setBackground(COLOR_0);
        labelFichas[contador].setOpaque(true);
        labelFichas[contador].setForeground(Color.white);
        labelFichas[contador].setFont(FONT_MARCADOR);
        tableroPanel.add(labelFichas[contador]);
    }
}

/**
 * Metodo que recibe un JPanel que le agregar los botones, que conceptualizan los mandos
 * del juego, para que el jugador pueda comunicarse con el programa
 */

public void makeMandos(JPanel panel)
{
    JButton botonArriba = new JButton();
    botonArriba.setActionCommand("Arriba");
    this.aagregarEfectosBotonosMandos(botonArriba,FLECHA_ARRIBA);
    botonArriba.setMnemonic(KeyEvent.VK_UP);
    botonArriba.addKeyListener(controlador);

    JButton botonDerecha = new JButton();
    botonDerecha.setActionCommand("Derecha");
    this.aagregarEfectosBotonosMandos(botonDerecha,FLECHA_DERECHA);
    botonDerecha.setMnemonic(KeyEvent.VK_RIGHT);
    botonDerecha.addKeyListener(controlador);

    JButton botonIzquierda = new JButton();
    botonIzquierda.setActionCommand("Izquierda");
    this.aagregarEfectosBotonosMandos(botonIzquierda,FLECHA_IZQUIERDA);
    botonIzquierda.setMnemonic(KeyEvent.VK_LEFT);
    botonIzquierda.addKeyListener(controlador);

    JButton botonAbajo = new JButton();
    botonAbajo.setActionCommand("Abajo");
    this.aagregarEfectosBotonosMandos(botonAbajo,FLECHA_ABAJO);
    botonAbajo.setMnemonic(KeyEvent.VK_DOWN);
    botonAbajo.addKeyListener(controlador);

    panel.add(botonIzquierda);
    panel.add(botonArriba);
    panel.add(botonDerecha);
    panel.add(botonAbajo);
}

/**
 * Metodo que recibe un objeto JButton y un objeto ImageIcon y le agrega los efectos necesarios
 * para que tengan los diseños que queremos que tengán
 */

private void aagregarEfectosBotonosMandos(JButton boton,ImageIcon imagen)
{
    boton.addActionListener(controlador);
    boton.setCursor(new Cursor(Cursor.HAND_CURSOR));
    boton.setBackground(COLOR_0);
    boton.setOpaque(true);
    boton.setBorderPainted(false);

    Image newImagen = imagen.getImage().getScaledInstance(50,50,Image.SCALE_SMOOTH);
    ImageIcon imagenDimensionada = new ImageIcon();
    imagenDimensionada.setImage(newImagen);
    boton.setIcon(imagenDimensionada);
}

private void agregarContenidoContenedorJuego()
{
    contenedorJuego.add(marcadorPanel,BorderLayout.NORTH);
    contenedorJuego.add(tableroPanel,BorderLayout.CENTER);
    contenedorJuego.add(mandosPanel,BorderLayout.SOUTH);
    this.add(contenedorJuego);
    contenedorJuego.setVisible(true);
}

public void cambiarContenedor()
{
    contenedorJuego.setVisible(true);
}

private void configurar()
{
    this.setTitle("MEGA 2048");
    this.setLocationRelativeTo(null);
    this.setSize(450,650);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void iniciar()
{
    this.moverTablero();
    this.setVisible(true);
}

public void setMarcador(int marcadorNumero)
{
    marcador.setText(""+marcadorNumero);
}

public void setRecord(int recordNumero)
{
    recordLabel.setText(""+recordNumero);
}

public void moverTablero()
{
    int indiceLabel = 0;
    int valorTemporal = 0;
    for (int contadorX = 0; contadorX<4; contadorX++)
    {
        for (int contadorY = 0; contadorY<4; contadorY++)
        {
            valorTemporal = controlador.getValorFicha(contadorX,contadorY);
            if (valorTemporal==0)
            {
                labelFichas[indiceLabel].setText("");
                labelFichas[indiceLabel].setBackground(controlador.getColorFicha(contadorX,contadorY));
            }
            else
            {
                labelFichas[indiceLabel].setText(""+valorTemporal);
                labelFichas[indiceLabel].setBackground(controlador.getColorFicha(contadorX,contadorY)); 
            }
            indiceLabel++;
        }
    }
    this.repaint();
}

public void nuevaFichaTablero()
{
    this.repaint();
}

public int anunciarResultado(int opcion)
{
    int resultado = 0;
    switch(opcion)
    {
        case 1:
        resultado = mensajeOpcion.showOptionDialog(this,"¡Ganaste!\nTu record es de: " + controlador.getRecord() + "\nTu puntaje es de: " + controlador.getPuntaje(),"2048",mensajeOpcion.DEFAULT_OPTION,mensajeOpcion.PLAIN_MESSAGE,null,RESULTADO_FINAL,null);
        break;
        case 2:
        resultado = mensajeOpcion.showOptionDialog(this,"¡Perdiste!\nTu record es de: " + controlador.getRecord() + "\nTu puntaje es de: " + controlador.getPuntaje(),"2048",mensajeOpcion.DEFAULT_OPTION,mensajeOpcion.PLAIN_MESSAGE,null,RESULTADO_FINAL,null);
        break;
    }
    return resultado;
}
}

2 个答案:

答案 0 :(得分:1)

请注意,JFrames使用的基础容器默认使用BorderLayout。这是一个简单的示例,它将向JFrame添加2个容器,并在单击按钮时在容器之间切换:

class Interfaz extends JFrame {
    private Container contenedorJuego, contenedorMenu, mainPane;
    private JButton showC;

    public Interfaz() {
        super("MEGA 2048");
        showC = new JButton("Switch);
        contenedorJuego = new JPanel();
        contenedorMenu = new JPanel();
        mainPane = new JPanel(new CardLayout());
    }

    private void initComponents() {
        contenedorJuego.setBorder(BorderFactory
            .createTitledBorder(BorderFactory
            .createRaisedBevelBorder(), "ContenedorJuego"));
        mainPane.add(contenedorJuego, "first");

        contenedorMenu.setBorder(BorderFactory
            .createTitledBorder(BorderFactory
            .createRaisedBevelBorder(), "ContenedorMenu"));
        mainPane.add(contenedorMenu, "second");
        add(mainPane, BorderLayout.CENTER);
        add(showC, BorderLayout.SOUTH);
        addC.addActionListener(new ActionListener() {
            private String selectedComponent = "first";
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout)mainPane.getLayout();
                if (selectedComponent.equals("first")) {
                    selectedComponent = "second";
                }
                else {
                    selectedComponent = "first";
                }
                cl.show(mainPane, selectedComponent);
           }
       });      
    }

    public static void main(String []args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Interfaz f = new Interfaz();
                f.initComponents();
                f.setLocationRelativeTo(null);
                f.setSize(100, 100);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }
}

我在没有IDE的情况下编写了这个,但我确信它应该具备所有功能。简单的想法是使用CardLayout通过单击按钮从一个容器(JPanel)切换到另一个容器。您不必将这些组件设置为可见,但您必须使JFrame可见,布局管理器将完成剩下的工作。

希望有所帮助

答案 1 :(得分:0)

您可能希望您的引擎基于状态,并具有菜单状态和游戏状态以及您想要的任何其他状态。考虑以下不完整的类块:

State currentState;

State menuState;
State gameState;

void init() {
    menuState = new MenuState();
    gameState = new GameState();
    currentState = menuState;
}

void render() {
    currentState.update();
    currentState.render(); // Maybe pass in Graphics/Graphics2D object here?
}

你需要某种方式让状态改变当前状态,可能是另一种方法。你的州可能看起来像这样:

class GameState implements State {
    // Implement methods here.
}
相关问题