?? sysdilog.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 + -