下面说的是游戏工具界面的搭建。在MainFrame里定义下面属性
private GamePanel gamePanel; //级别 private JLabel levelTextLabel = new JLabel("级别"); private JLabel levelLabel = new JLabel(); private Box levelTextBox = Box.createHorizontalBox(); private Box levelBox = Box.createHorizontalBox(); //分数 private Box scoreTextBox = Box.createHorizontalBox(); private Box scoreBox = Box.createHorizontalBox(); private JLabel scoreTextLabel = new JLabel("分数"); private JLabel scoreLabel = new JLabel(); //下一个 private Box nextTextBox = Box.createHorizontalBox(); private JLabel nextTextLabel = new JLabel("下一个"); //继续 private Box resumeBox = Box.createHorizontalBox(); private JLabel resumeLabel = new JLabel(); //暂停 private Box pauseBox = Box.createHorizontalBox(); private JLabel pauseLabel = new JLabel(); //开始 private Box startBox = Box.createHorizontalBox(); private JLabel startLabel = new JLabel(); //工具类 private JPanel toolPanel = new JPanel(); 在MainFrame类里的构造器里添加下面代码来创造界面public MainFrame() { this.currentLevel = 1;
this.gamePanel = new GamePanel(this); BoxLayout toolPanelLayout = new BoxLayout(this.toolPanel, BoxLayout.Y_AXIS); this.toolPanel.setLayout(toolPanelLayout); this.toolPanel.setBorder(new EtchedBorder()); this.toolPanel.setBackground(Color.gray); //分数 this.scoreTextBox.add(this.scoreTextLabel); this.scoreLabel.setText(String.valueOf(this.score)); this.scoreBox.add(this.scoreLabel); //级别 this.levelTextBox.add(this.levelTextLabel); this.levelLabel.setText(String.valueOf(this.currentLevel)); this.levelBox.add(this.levelLabel); //继续按钮 this.resumeLabel.setIcon(RESUME_ICON); this.resumeLabel.setPreferredSize(new Dimension(3, 25)); this.resumeBox.add(this.resumeLabel); //暂停按钮 this.pauseLabel.setIcon(PAUSE_ICON); this.pauseLabel.setPreferredSize(new Dimension(3, 25)); this.pauseBox.add(this.pauseLabel); //开始 this.startLabel.setIcon(START_ICON); this.startLabel.setPreferredSize(new Dimension(3, 25)); this.startBox.add(this.startLabel); //下一个 this.nextTextBox.add(this.nextTextLabel);
this.toolPanel.add(Box.createVerticalStrut(10)); this.toolPanel.add(scoreTextBox); this.toolPanel.add(Box.createVerticalStrut(10)); this.toolPanel.add(scoreBox); this.toolPanel.add(Box.createVerticalStrut(10)); this.toolPanel.add(levelTextBox); this.toolPanel.add(Box.createVerticalStrut(10)); this.toolPanel.add(levelBox); this.toolPanel.add(Box.createVerticalStrut(15)); this.toolPanel.add(this.resumeBox); this.toolPanel.add(Box.createVerticalStrut(15)); this.toolPanel.add(this.pauseBox); this.toolPanel.add(Box.createVerticalStrut(15)); this.toolPanel.add(this.startBox); this.toolPanel.add(Box.createVerticalStrut(30)); this.toolPanel.add(this.nextTextBox);
this.blankBox.add(Box.createHorizontalStrut(99)); this.toolPanel.add(blankBox); this.add(this.gamePanel, BorderLayout.CENTER); this.add(this.toolPanel, BorderLayout.EAST); this.setPreferredSize(new Dimension(349, 416)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(350, 200); this.setResizable(false); this.setTitle("俄罗斯方块"); this.pack(); }