⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysdilog.java

📁 wiziqi
💻 JAVA
字号:
package dilog;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import vo.CursorsAndImg;

import iniset.PropertySet;
import iniset.finals.SetDatas;

/**
 * 设置类,继承自JDialog类,以单子模式实现
 * 
 * @author B.Lee
 * 
 */
public class SysDilog extends JDialog {
  private PropertySet propertySet = null;

  private JRadioButton soundOn = null;

  private JRadioButton soundOff = null;

  private JRadioButton musicOn = null;

  private JRadioButton musicOff = null;

  private JRadioButton person = null;

  private JRadioButton computer = null;

  private JRadioButton blackFirst = null;

  private JRadioButton whiteFirst = null;

  private JRadioButton canRegret = null;

  private JRadioButton canNotRegret = null;

  private ButtonGroup sound = null;

  private ButtonGroup music = null;

  private ButtonGroup model = null;

  private ButtonGroup first = null;

  private ButtonGroup regret = null;

  /** 用来标示按下的是保存与否 */
  private boolean isCommit = false;

  /** 本类的一个引用 */
  private static SysDilog sysDilog = null;

  /** 提交按钮 */
  private JButton commit = null;

  /** 取消按钮 */
  private JButton cancel = null;

  private SysDilog(Frame window) {
    super(window, "系统设置", true);
    this.setSize(290, 240);
    this.setLayout(null);
    this.setResizable(false);
    Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((scrSize.width - getSize().width) / 2,
        (scrSize.height - getSize().height) / 2);// 最佳位置
    this.setVisible(false);// 最初不可见

    // 初始化各单选项
    soundOn = new JRadioButton("音效开");
    soundOff = new JRadioButton("音效关");
    musicOn = new JRadioButton("背景音乐开");
    musicOff = new JRadioButton("背景音乐关");
    person = new JRadioButton("人VS人");
    computer = new JRadioButton("人VS电脑");
    whiteFirst = new JRadioButton("白棋先");
    blackFirst = new JRadioButton("黑棋先");
    canRegret = new JRadioButton("允许悔棋");
    canNotRegret = new JRadioButton("禁止悔棋");

    soundOn.setBounds(40, 20, 100, 20);
    soundOff.setBounds(150, 20, 100, 20);
    musicOn.setBounds(40, 45, 100, 20);
    musicOff.setBounds(150, 45, 100, 20);
    person.setBounds(40, 70, 100, 20);
    computer.setBounds(150, 70, 100, 20);
    whiteFirst.setBounds(40, 95, 100, 20);
    blackFirst.setBounds(150, 95, 100, 20);
    canRegret.setBounds(40, 120, 100, 20);
    canNotRegret.setBounds(150, 120, 100, 20);

    // 初始化按钮信息
    commit = new JButton("保存", CursorsAndImg.commit);
    cancel = new JButton("取消", CursorsAndImg.cancel);

    commit.setBounds(45, 162, 80, 30);
    cancel.setBounds(145, 162, 80, 30);

    // ***
    sound = new ButtonGroup();
    music = new ButtonGroup();
    model = new ButtonGroup();
    first = new ButtonGroup();
    regret = new ButtonGroup();
    // 将对应单选项编组
    sound.add(soundOn);
    sound.add(soundOff);
    music.add(musicOn);
    music.add(musicOff);
    model.add(person);
    model.add(computer);
    first.add(whiteFirst);
    first.add(blackFirst);
    regret.add(canRegret);
    regret.add(canNotRegret);

    // 初始化选项被选择的信息
    propertySet = PropertySet.getPropertySet();
    init();

    // 添加各组件
    this.add(soundOn);
    this.add(soundOff);
    this.add(musicOn);
    this.add(musicOff);
    this.add(person);
    this.add(computer);
    this.add(whiteFirst);
    this.add(blackFirst);
    this.add(canRegret);
    this.add(canNotRegret);
    this.add(commit);
    this.add(cancel);

    // 添加事件聆听,当保存按钮按下时,根据新的设置刷新棋盘并隐藏
    // 当取消按钮按下时,隐藏
    commit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (person.isSelected()) {
          if (propertySet.getModel() != SetDatas.PEOPLE_VS_PEOPLE) {
            isCommit = true;
          }
        } else {
          if (propertySet.getModel() != SetDatas.PEOPLE_VS_COMPUTER) {
            isCommit = true;
          }
        }

        // 改变设置
        if (soundOn.isSelected()) {
          propertySet.setSound(SetDatas.SOUND_ON);
        } else {
          propertySet.setSound(SetDatas.SOUND_OFF);
        }
        if (musicOn.isSelected()) {
          propertySet.setMusic(SetDatas.MUSIC_ON);
        } else {
          propertySet.setMusic(SetDatas.MUSIC_OFF);
        }
        if (person.isSelected()) {
          propertySet.setModel(SetDatas.PEOPLE_VS_PEOPLE);
        } else {
          propertySet.setModel(SetDatas.PEOPLE_VS_COMPUTER);
        }
        if (whiteFirst.isSelected()) {
          propertySet.setFirst(SetDatas.WHITE_FIRST);
        } else {
          propertySet.setFirst(SetDatas.BLACK_FIRST);
        }
        if (canRegret.isSelected()) {
          propertySet.setRegret(SetDatas.REGRET_ABLE);
        } else {
          propertySet.setRegret(SetDatas.UN_REGRET_ABLE);
        }

        // 此处添加刷新棋盘信息

        sysDilog.setVisible(false);
        if (!propertySet.savePropertySet()) {
          JOptionPane.showMessageDialog(null, "设置信息保存失败!", "警告",
              JOptionPane.WARNING_MESSAGE);
        }
      }
    });

    cancel.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        sysDilog.setVisible(false);
        isCommit = false;
        init();
      }
    });

    this.addWindowListener(new WindowAdapter() {
      public void windowClosed(WindowEvent e) {
        sysDilog.setVisible(false);
        isCommit = false;
        init();
      }
    });
  }

  /**
   * 用于外界获取设置窗口
   * 
   * @param window
   *          传入的设置对话框的依附窗口
   * @return 一个SysDilog的引用
   */
  public static SysDilog getSysDilog(Frame window) {
    if (sysDilog == null) {
      sysDilog = new SysDilog(window);
      return sysDilog;
    } else {
      return sysDilog;
    }
  }

  public boolean isCommit() {
    return isCommit;
  }

  public void setCommit(boolean isCommit) {
    this.isCommit = isCommit;
  }
  
  /**
   * 将选项初始化为文件中的值
   */
  private void init() {
    if (propertySet.getSound() == SetDatas.MUSIC_ON) {
      soundOn.setSelected(true);
    } else {
      soundOff.setSelected(true);
    }
    if (propertySet.getMusic() == SetDatas.MUSIC_ON) {
      musicOn.setSelected(true);
    } else {
      musicOff.setSelected(true);
    }
    if (propertySet.getModel() == SetDatas.PEOPLE_VS_PEOPLE) {
      person.setSelected(true);
    } else {
      computer.setSelected(true);
    }
    if (propertySet.getFirst() == SetDatas.WHITE_FIRST) {
      whiteFirst.setSelected(true);
    } else {
      blackFirst.setSelected(true);
    }
    if (propertySet.getRegret() == SetDatas.REGRET_ABLE) {
      canRegret.setSelected(true);
    } else {
      canNotRegret.setSelected(true);
    }
  }

  // //test
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setVisible(true);
    SysDilog.getSysDilog(frame).setVisible(true);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -