JAVA 實現《複雜迷宮》遊戲|CSDN創作打卡

專注的阿熊發表於2022-02-17

public class StartView extends JFrame {

     public StartView() {

         this.setTitle(" 複雜迷宮 ");

         this.setSize(240, 265);

         this.setLocationRelativeTo(null);

         this.setDefaultCloseOperation(EXIT_ON_CLOSE);

         this.setResizable(false);

         initialize();

         this.setVisible(true);

     }

     private void initialize() {

         JPanel contentPane = new JPanel();

         this.setContentPane(contentPane);

         contentPane.setLayout(null);

         JLabel widthLabel = new JLabel(" 迷宮長度: ");

         JLabel heightLabel = new JLabel(" 迷宮高度: ");

         JLabel levelLabel = new JLabel(" 難度 :");

         JTextField widthText = new JTextField();

         JTextField heightText = new JTextField();

         JRadioButton level1 = new JRadioButton("1");

         JRadioButton level2 = new JRadioButton("2");

         JRadioButton level3 = new JRadioButton("3");

         ButtonGroup levelGroup = new ButtonGroup();

         levelGroup.add(level1);

         levelGroup.add(level2);

         levelGroup.add(level3);

         JButton run = new JButton(" 生成迷宮 ");

         // 設定標籤位置

         widthLabel.setBounds(20, 20, 100, 30);

         heightLabel.setBounds(20, 70, 110, 30);

         widthText.setBounds(120, 20, 70, 30);

         heightText.setBounds(120, 70, 70, 30);

         levelLabel.setBounds(20, 120, 60, 30);

         level1.setBounds(80, 120, 50, 30);

         level2.setBounds(130, 120, 50, 30);

         level3.setBounds(180, 120, 50, 30);

         run.setBounds(55, 170, 120, 30);

         // 限制輸入框只接收數字

         widthText.setDocument(new NumberTextField());

         heightText.setDocument(new NumberTextField());

         // 改變字型

         Font font = new Font(" 楷體 ", Font.PLAIN, 17);

         widthLabel.setFont(font);

         heightLabel.setFont(font);

         widthText.setFont(font);

         heightText.setFont(font);

         levelLabel.setFont(font);

         level1.setFont(font);

         level2.setFont(font);

         level3.setFont(font);

         run.setFont(font);

         // 取消按鈕選中邊框

         level1.setFocusPainted(false);

         level2.setFocusPainted(false);

         level3.setFocusPainted(false);

         // 預設選擇難度 3

         level3.setSelected(true);

         contentPane.add(widthLabel);

         contentPane.add(heightLabel);

         contentPane.add(widthText);

         contentPane.add(heightText);

         contentPane.add(levelLabel);

         contentPane.add(level1);

         contentPane.add(level2);

         contentPane.add(level3);

         contentPane.add(run);

         // 生成迷宮監聽器

         run.addActionListener(e -> {

             // 建議寬在 10-90 ,長在 10-45 之間

             if (widthText.getText().equals("")) {

                 JOptionPane.showMessageDialog(null, " 長度不能為空! ", " 提示 ", JOptionPane.INFORMATION_MESSAGE);

             } else if (heightText.getText().equals("")) {

                 JOptionPane.showMessageDialog(null, " 高度不能為空! ", " 提示 ", JOptionPane.INFORMATION_MESSAGE);

             } else {

                 int width = Integer.parseInt(widthText.getText());

                 int height = 外匯跟單gendan5.comInteger.parseInt(heightText.getText());

                 if (width >= 10 && width <= 90 && height >= 10 && height <= 45) {

                     int level = level1.isSelected() ? 1 : level2.isSelected() ? 2 : 3;

                     MazeModel maze = new MazeModel(width, height, level);

                     this.dispose();

                     maze.draw();

                 } else {

                     JOptionPane.showMessageDialog(null, " 迷宮的長必須在 10-45 之間,寬必須在 10-90 之間,請檢查輸入是否有誤! ", " 錯誤輸入 ", JOptionPane.ERROR_MESSAGE);

                 }

             }

         });

         // 新增Enter鍵入監聽器

         KeyAdapter enterAdapter = new KeyAdapter() {

             @Override

             public void keyPressed(KeyEvent e) {

                 if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                     run.doClick();   // 回車即生成迷宮

                 }

             }

         };

         widthText.addKeyListener(enterAdapter);

         heightText.addKeyListener(enterAdapter);

     }

     public static void main(String[] args) {

         new StartView();

     }

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2856216/,如需轉載,請註明出處,否則將追究法律責任。

相關文章