Java. Layout per un gioco. Come mettere le etichette nella parte superiore destra della finestra?


Questo è ciò di cui ho bisogno per ottenere come risultato

inserisci qui la descrizione dell'immagine

C'è un'area di gioco che dovrebbe essere di dimensioni (800.600) sul lato sinistro. Sul resto della finestra a destra dovrebbe essere l'area Menu / Punteggio.

Ho due etichette (scoreLabel;pointsLabel;), che voglio mettere nella parte superiore dell'area del menu, come nell'immagine. nella mia versione sto usando gridlayout, ma non funziona come voglio:)

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


public class PongFrame extends JFrame {

        private JLabel scoreLabel;
        private JLabel pointsLabel;

        public PongFrame () {

        this.setTitle("Game");
        this.setSize(1100,600);


        this.scoreLabel = new JLabel("score");
        this.pointsLabel = new JLabel("");

        JPanel labelPanel = new JPanel();
            labelPanel.setLayout(new GridLayout(1,2));
            labelPanel.add(scoreLabel);
            labelPanel.add(pointsLabel);

         JPanel gameArea = new JPanel();
            gameArea.setBackground(Color.orange);
            gameArea.setSize(800,600);


        Container con = this.getContentPane();
            con.setLayout(new GridLayout(1,2));
            con.add(gameArea);
            con.add(labelPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

        public void setPoints (String labeltext){
        this.pointsLabel.setText(labeltext);
    }

    public static void main (String [] args){
        PongFrame window = new PongFrame();
        window.pointsLabel.setText("0");
    }

}
Author: Arnaud, 2016-04-28

1 answers

È possibile ottenere questo layout utilizzando BorderLayout.

Uno per il pannello contenuto, per posizionare il pannello di gioco (con la sua dimensione preferita) ad ovest e il pannello punteggio al centro (guadagnerà tutto lo spazio extra).

Un altro per il pannello punteggio, in modo da poter posizionare le etichette (il labelPanel) a nord.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class PongFrame extends JFrame {

    private final JLabel scoreLabel;
    private final JLabel pointsLabel;

    public PongFrame() {

        this.setTitle("Game");
        this.setSize(1100, 600);

        scoreLabel = new JLabel("score");
        pointsLabel = new JLabel("");

        Container con = this.getContentPane();
        con.setLayout(new BorderLayout());

        //create score/menu panel
        JPanel scorePanel = new JPanel();

        scorePanel.setLayout(new BorderLayout());

        JPanel labelPanel = new JPanel();
        labelPanel.setLayout(new GridLayout(1, 2));
        labelPanel.add(scoreLabel);
        labelPanel.add(pointsLabel);

        // add the labels to the north
        scorePanel.add(labelPanel, BorderLayout.NORTH);

        // create game panel with a preferred size
        JPanel gameArea = new JPanel() {

            public Dimension getPreferredSize() {

                return new Dimension(800, 600);

            }
        };
        gameArea.setBackground(Color.orange);

        // add the game panel to the west
        con.add(gameArea, BorderLayout.WEST);

        // add the score/menu panel to the center
        con.add(scorePanel, BorderLayout.CENTER);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public void setPoints(final String labeltext) {
        pointsLabel.setText(labeltext);
    }

    public static void main(final String[] args) {
        PongFrame window = new PongFrame();
        window.pointsLabel.setText("0");
    }

}

Si noti che potrebbe essere necessario giocare un po ' con il labelPanel e l'allineamento del testo del JLabel s.

 0
Author: Arnaud, 2016-04-28 13:40:15