基于UI的记忆游戏

时间:2016-05-28 09:27:46

标签: java swing

我创建了截图中显示的UI。我在代码中的一个面板中放了太多JPanel来实现UI,这将导致代码设计错误和性能降低。

在编写此类用户界面时是否有任何良好做法?

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class GameUI extends JFrame {

    static JLabel blankCardLabel;
    static JLabel cardOne;
    JLabel cardTwo;
    JLabel cardThree;
    JLabel cardFour;
    JLabel cardFive;
    JLabel cardSix;
    JLabel cardSeven;
    JLabel cardEight;
    JLabel cardNine;
    JLabel cardTen;
    JLabel cardEleven;
    JLabel cardTwelve;
    JLabel cardThirteen;
    JLabel cardFourteen;
     BufferedImage blankCardPic;
    JLabel deck;
     BufferedImage revealCardPic;
    JPanel eastPanel;
    JPanel southPanel;
    JPanel messagePanel;
    JPanel cardPanel;
    BoxLayout eastPanelBL;
    BoxLayout southPanelBL;
    BoxLayout cardPanelBL;
    JLabel playerOne = new JLabel("Player One");
    JLabel playerTwo = new JLabel("Player Two");
    JLabel playerThree = new JLabel("Player Three");
    JLabel playerFour = new JLabel("Player Four");
    JLabel c;
    public GameUI() {
        setLayout(new BorderLayout());

        add(new JLabel("Memory Game", SwingConstants.CENTER), BorderLayout.NORTH);
        /*cardOne = new JLabel("One");
        cardTwo = new JLabel("Two");
        cardThree = new JLabel("Three");
        cardFour = new JLabel("Four");
        cardFive = new JLabel("Five");
        cardSix = new JLabel("Six");
        cardSeven = new JLabel("Seven");
        cardEight = new JLabel("Eight");
        cardNine = new JLabel("Nine");
        cardTen = new JLabel("Ten");
        cardEleven = new JLabel("Eleven");
        cardTwelve = new JLabel("Twelve");
        cardThirteen = new JLabel("Thirteen");
        cardFourteen = new JLabel("Fourteen");*/

        deck = new JLabel(new ImageIcon(blankCard()));
        cardOne = new JLabel(new ImageIcon(blankCard()));
        cardTwo = new JLabel(new ImageIcon(blankCard()));
        cardThree = new JLabel(new ImageIcon(blankCard()));
        cardFour = new JLabel(new ImageIcon(blankCard()));
        cardFive = new JLabel(new ImageIcon(blankCard()));
        cardSix = new JLabel(new ImageIcon(blankCard()));
        cardSeven = new JLabel(new ImageIcon(blankCard()));
        cardEight = new JLabel(new ImageIcon(blankCard()));
        cardNine = new JLabel(new ImageIcon(blankCard()));
        cardTen = new JLabel(new ImageIcon(blankCard()));
        cardEleven = new JLabel(new ImageIcon(blankCard()));
        cardTwelve = new JLabel(new ImageIcon(blankCard()));
        cardThirteen = new JLabel(new ImageIcon(blankCard()));
        cardFourteen = new JLabel(new ImageIcon(blankCard()));


        cardPanel = new JPanel();
        cardPanelBL = new BoxLayout(cardPanel, BoxLayout.Y_AXIS);
        cardPanel.setLayout(cardPanelBL);

        JPanel topRowPanel = new JPanel();

        //cardPanel.add(deck);
        //c= new JLabel("Card Left In deck",SwingConstants.CENTER);
        //cardPanel.add(c);

        topRowPanel.add(cardOne);
        topRowPanel.add(cardTwo);
        topRowPanel.add(cardThree);
        topRowPanel.add(cardFour);
        topRowPanel.add(cardFive);
        topRowPanel.add(cardSix);
        topRowPanel.add(cardSeven);
        cardPanel.add(topRowPanel);

        JPanel bottomRowPanel = new JPanel();
        bottomRowPanel.add(cardEight);
        bottomRowPanel.add(cardNine);
        bottomRowPanel.add(cardTen);
        bottomRowPanel.add(cardEleven);
        bottomRowPanel.add(cardTwelve);
        bottomRowPanel.add(cardThirteen);
        bottomRowPanel.add(cardFourteen);
        cardPanel.add(bottomRowPanel);


        add(cardPanel,BorderLayout.CENTER);
        //cardOne.setBounds(50,50,50,50);
        eastPanel = new JPanel();
        eastPanelBL = new BoxLayout(eastPanel, BoxLayout.Y_AXIS);
        eastPanel.setLayout(eastPanelBL);
        eastPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        eastPanel.add(playerOne);
        eastPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        eastPanel.add(playerTwo);
        eastPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        eastPanel.add(playerThree);
        eastPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        eastPanel.add(playerFour);
        add(eastPanel, BorderLayout.EAST);  
        southPanel = new JPanel(new BorderLayout());
        southPanel.add(new JTextArea(10,10),BorderLayout.CENTER);
        messagePanel = new JPanel(new BorderLayout());
        messagePanel.add(new JTextField(10),BorderLayout.CENTER);
        messagePanel.add(new JButton("Send"),BorderLayout.EAST);
        southPanel.add(messagePanel,BorderLayout.SOUTH);
        add(southPanel, BorderLayout.SOUTH);        

        //add(cardOne);
        //cardOne.setText("ClubsAce.png");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setSize(1024,768);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new GameUI();
    }

    public BufferedImage blankCard() {
        try {
            blankCardPic = ImageIO.read(new File("resources/images/blank.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return blankCardPic;
    }

    /*public static BufferedImage revealCard(String s) {
        try {
            revealCardPic = ImageIO.read(new File("resources/images/"+s));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return revealCardPic;
    }*/
}

1 个答案:

答案 0 :(得分:0)

如果你有太多的'JPanel'或'JLabel'或'JButton'......最好认为你可以做的是你可以创建它的数组。在for循环中,如果代码可以在几行中创建它。