SetFrame.java
上传用户:hainatieta
上传日期:2019-07-13
资源大小:1076k
文件大小:7k
- package cn.seejava.lei2;
- import java.awt.Checkbox;
- import java.awt.CheckboxGroup;
- import java.awt.Choice;
- import java.awt.FlowLayout;
- import java.awt.HeadlessException;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- public class SetFrame extends JFrame implements ActionListener {
- public static final int width = 245;
- public static final int height = 300;
- public static File f = new File("config.ini");
- public static String defConfig = "1,1,0,0,0,11,1,1,1";
- JButton b1 = new JButton("保存设置");
- Choice ch = new Choice();
- Choice ch1 = new Choice();
- Choice ch2 = new Choice();
- JCheckBox jc1,jc2,jc3,jc4,jc8,jc9;
- Checkbox jc5,jc6;
- JLabel jl = new JLabel("房间人数小于此数退出此房间");
- JLabel jl1 = new JLabel("金牌代理设置");
- JLabel jl2 = new JLabel(" ");
- CheckboxGroup cbg = new CheckboxGroup();
- public SetFrame(){
- this.setBounds((getScreenWidth()-width)/2,(getScreenHeight()-height)/2,width,height);
- this.setTitle("参数设置");
- this.setResizable(false);
- //JCheckBox jc = new JCheckBox("请选择", true);
- ch.add("10");
- ch.add("11");
- ch.add("12");
- ch.add("13");
- ch.add("14");
- ch2.add("不使用代理");
- ch2.add("电信代理一");
- ch2.add("电信代理二");
- b1.addActionListener(this);
- loadConfig();
- this.setLayout(new FlowLayout(FlowLayout.LEFT));
- //this.add(jc);
- this.setVisible(true);
- this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-
- }
- private int getScreenWidth(){
- return Toolkit.getDefaultToolkit().getScreenSize().width;
- }
- private int getScreenHeight(){
- return Toolkit.getDefaultToolkit().getScreenSize().height;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- if(e.getSource()==b1){
- //保存用户设置
- int para[] = {0,0,0,0,0,11,1,1,1};
- if(jc1.isSelected()){
- para[0]=1;
- }
- if(jc2.isSelected()){
- para[1]=1;
- }
- if(jc3.isSelected()){
- para[2]=1;
- }
- if(jc4.isSelected()){
- para[3]=1;
- }
- if(jc5.getState()){
- para[4]=1;
- }
- if(Integer.parseInt((ch.getSelectedItem()))!=11){
- para[5]=Integer.parseInt((ch.getSelectedItem()));
- }
- if(ch2.getSelectedItem().equals("不使用代理")){
- para[6]=1;
- }
- if(ch2.getSelectedItem().equals("电信代理一")){
- para[6]=2;
- }
- if(ch2.getSelectedItem().equals("电信代理二")){
- para[6]=3;
- }
- if(!jc8.isSelected()){
- para[7]=0;
- }
- if(!jc9.isSelected()){
- para[8]=0;
- }
- String str = para[0]+","+para[1]+","+para[2]+","+para[3]+","+para[4]+","+para[5]+","+para[6]+","+para[7]+","+para[8];
- createConfig(str);
- this.dispose();
- }
- }
- public void loadConfig(){
-
- if(f.exists()){
- //直接加载
- FileReader fr;
- try {
- fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- String conf = null;
- if((conf=br.readLine())!=null){
- //
- String str[] = conf.split(",");
- //System.out.println(str.length);
- for(int i=0;i<str.length;i++){
- //System.out.println(str[i]);
- switch(i){
- case 0:
- if(Integer.parseInt(str[i])==0){
- //不需要自动准备
- jc1 = new JCheckBox("自动准备", false);
- }else{
- jc1 = new JCheckBox("自动准备", true);
- }
- break;
- case 1:
- if(Integer.parseInt(str[i])==0){
- //自动自杀
- jc2 = new JCheckBox("自动自杀", false);
- }else{
- jc2 = new JCheckBox("自动自杀", true);
- }
- break;
- case 2:
- if(Integer.parseInt(str[i])==0){
-
- jc3 = new JCheckBox("自动土匪", false);
- }else{
- jc3 = new JCheckBox("自动土匪", true);
- }
- break;
- case 3:
- if(Integer.parseInt(str[i])==0){
-
- jc4 = new JCheckBox("自动F5,F6喊话", false);
- }else{
- jc4 = new JCheckBox("自动F5,F6喊话", true);
- }
- break;
- case 4:
- if(Integer.parseInt(str[i])==0){
- jc5 = new Checkbox("成为房主自动开始游戏",cbg, false);
- jc6 = new Checkbox("成为房主退出房间",cbg,true);
- //this.add("one",cbg);
- }else{
- jc5 = new Checkbox("成为房主自动开始游戏",cbg, true);
- jc6 = new Checkbox("成为房主退出房间",cbg,false);
- }
- //cbg.add(jc5);
- break;
- case 5:
- switch(Integer.parseInt(str[i])){
- case 10:
- ch.select("10");
- break;
- case 11:
- ch.select("11");
- break;
- case 12:
- ch.select("12");
- break;
- case 13:
- ch.select("13");
- break;
- case 14:
- ch.select("14");
- break;
- }
- break;
- case 6:
- switch(Integer.parseInt(str[i])){
- case 1:
- ch2.select("不使用代理");
- break;
- case 2:
- ch2.select("电信代理一");
- break;
- case 3:
- ch2.select("电信代理二");
- break;
- }
- break;
- case 7:
-
- if(Integer.parseInt(str[i])==0){
- //自动自杀
- jc8 = new JCheckBox("房主三分钟不开始游戏自动退出此房", false);
- }else{
- jc8 = new JCheckBox("房主三分钟不开始游戏自动退出此房", true);
- }
- break;
- case 8:
- if(Integer.parseInt(str[i])==0){
- jc9 = new JCheckBox("被T后自动快速加入",false);
- }else{
- jc9 = new JCheckBox("被T后自动快速加入",true);
- }
- }
- }
- }else{
- createConfig(defConfig);
- loadConfig();
- //JOptionPane.showMessageDialog(null, "加载配置文件出错");
- }
- br.close();
-
-
- this.add(jc1);
- this.add(jc2);
- this.add(jc3);
- this.add(jc4);
- this.add(jc8);
- this.add(jc9);
- this.add(jc5);
- this.add(jc6);
- this.add(jl);
- this.add(ch);
- this.add(jl1);
- this.add(ch2);
- this.add(jl2);
- this.add(b1);
-
- //this.add("one",cbg,);
-
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (HeadlessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }else{
-
-
- //先写入 1 ,1 ,0 ,0 ,0, 11
- createConfig(defConfig);
- //再调用自己
- loadConfig();
- }
-
- }
- public static void createConfig(String str){
-
- try {
-
- FileWriter fw = new FileWriter(f);
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(str);
- bw.close();
-
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }