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

📄 minedialog.java

📁 研学作品
💻 JAVA
字号:
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;

public class MineDialog extends JDialog{
 	JLabel tipLabel = new JLabel("自定义游戏",JLabel.CENTER);//提示标签
 	JLabel rowLabel = new JLabel("列数",JLabel.CENTER);//行数标签
 	JLabel colLabel = new JLabel("行数",JLabel.CENTER);//列数标签
 	JLabel mineLabel = new JLabel("地雷数",JLabel.CENTER);//地雷数标签
 	
 	JTextField rowShow = new JTextField(10);//行显示文本域
 	JTextField colShow = new JTextField(10);//列显示文本域
 	JTextField mineShow = new JTextField(10);//地雷数显示文本域
 	
    JButton OKButton = new JButton("确定");
    
    JLabel tipLabel2 = new JLabel("难度选择:");
    JRadioButton optEasy=new JRadioButton("简单难度");
    JRadioButton optNorm=new JRadioButton("中等难度");
    JRadioButton optHard=new JRadioButton("困难难度");
    JRadioButton optCustom=new JRadioButton("自定义游戏");
    JRadioButton optLoad=new JRadioButton("继续上次保存的游戏");
    
    ButtonGroup bg = new ButtonGroup();
    
    int gridx,gridy,mines;
    int level;
    
    public MineDialog(int x,int y,int m,int lv){
      	super();
      	
      	this.setTitle("开始");
      	this.setModal(true);
      	this.setLocation(300,300);
      	
      	JPanel OKPanel = new JPanel();
      	JPanel setPanel= new JPanel();
      	JPanel lvlPanel= new JPanel();
      	JPanel tipPanel= new JPanel();
      	
      	tipPanel.setLayout(new GridLayout(1,2));
      	setPanel.setLayout(new GridLayout(3,2));
      	lvlPanel.setLayout(new GridLayout(5,1));
      	
      	tipPanel.add(tipLabel2);
      	tipPanel.add(tipLabel);
      	
	    setPanel.add(colLabel);
      	setPanel.add(colShow);
      	setPanel.add(rowLabel);
      	setPanel.add(rowShow);
      	setPanel.add(mineLabel);
      	setPanel.add(mineShow);
      	
      	OKButton.addActionListener(new ButtonListener());
      	OKPanel.add(OKButton);
      	getRootPane().setDefaultButton(OKButton);
      	
      	bg.add(optEasy);
      	bg.add(optNorm);
      	bg.add(optHard);
      	bg.add(optCustom);
      	bg.add(optLoad);
      	
      	optListener optLis=new optListener();
      	optEasy.addActionListener(optLis);
        optNorm.addActionListener(optLis);
        optHard.addActionListener(optLis);
        optCustom.addActionListener(optLis);
        optLoad.addActionListener(optLis);
        
      	lvlPanel.add(optEasy);
      	lvlPanel.add(optNorm);
      	lvlPanel.add(optHard);
      	lvlPanel.add(optCustom);
      	lvlPanel.add(optLoad);
      	
		Container dialogContentPane = getContentPane();
		dialogContentPane.setLayout(new BorderLayout(1,5));
		dialogContentPane.add(tipPanel,BorderLayout.NORTH);
      	dialogContentPane.add(setPanel,BorderLayout.EAST);
      	dialogContentPane.add(OKPanel,BorderLayout.SOUTH);
      	dialogContentPane.add(lvlPanel,BorderLayout.WEST);
      	
      	this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
      	addWindowListener(new Dlg_Close());
      	
      	File sav=new File("saveInfo.dat");
      	if(sav.exists()){
      		try{
      			DataInputStream in=new DataInputStream(new FileInputStream(sav));
      			gridx=in.readInt();
      			gridy=in.readInt();
      			mines=in.readInt();
      			in.close();
      			optLoad.setSelected(true);
      			optLoad.getActionListeners()[0].actionPerformed(new ActionEvent(optLoad,0,""));
      		}
      		catch(Exception e){
      			JOptionPane.showMessageDialog(null,"读取失败!");
      			optLoad.setEnabled(false);
				optCustom.setSelected(true);
	      		optCustom.getActionListeners()[0].actionPerformed(new ActionEvent(optCustom,0,""));
	      		rowShow.setText(new Integer(x).toString());
	      		colShow.setText(new Integer(y).toString());
	      		mineShow.setText(new Integer(m).toString());
      		}
      	}
      	else{
      		optLoad.setEnabled(false);
      		switch(lv){
      			case 1:
      				optEasy.setSelected(true);
      				optEasy.getActionListeners()[0].actionPerformed(new ActionEvent(optEasy,0,""));
      				break;
      			case 2:
      				optNorm.setSelected(true);
      				optNorm.getActionListeners()[0].actionPerformed(new ActionEvent(optNorm,0,""));
      				break;
				case 3:
					optHard.setSelected(true);
	      			optHard.getActionListeners()[0].actionPerformed(new ActionEvent(optHard,0,""));
	      			break;
				case 4:
					optCustom.setSelected(true);
	      			optCustom.getActionListeners()[0].actionPerformed(new ActionEvent(optCustom,0,""));
	      			rowShow.setText(new Integer(x).toString());
	      			colShow.setText(new Integer(y).toString());
	      			mineShow.setText(new Integer(m).toString());
	      	}
      	}
		this.pack();
      	this.setVisible(true);
    }//构造方法FileNameDialog()声明结束
    	
    class optListener implements ActionListener{	
     	public void actionPerformed(ActionEvent e){
     		Object source = e.getSource();
       		if(source==optEasy){
       			rowShow.setText("10");
       			rowShow.setEditable(false);
       			colShow.setText("10");
       			colShow.setEditable(false);
       			mineShow.setText("10");
       			mineShow.setEditable(false);
       			level=1;
       		}
       		if(source==optNorm){
       			rowShow.setText("16");
       			rowShow.setEditable(false);
       			colShow.setText("16");
       			colShow.setEditable(false);
       			mineShow.setText("40");
       			mineShow.setEditable(false);
       			level=2;
       		}
       		if(source==optHard){
       			rowShow.setText("30");
       			rowShow.setEditable(false);
       			colShow.setText("16");
       			colShow.setEditable(false);
       			mineShow.setText("99");
       			mineShow.setEditable(false);
       			level=3;
       		}
       		if(source==optCustom){
       			rowShow.setEditable(true);
       			colShow.setEditable(true);
       			mineShow.setEditable(true);
       			level=4;
       		}
       		if(source==optLoad){
       			rowShow.setText(new Integer(gridx).toString());
       			colShow.setText(new Integer(gridy).toString());
       			mineShow.setText(new Integer(mines).toString());
       			rowShow.setEditable(false);
       			colShow.setEditable(false);
       			mineShow.setEditable(false);
       			level=5;
       		}
     }
   }
    
  	class ButtonListener implements ActionListener{
  	
     	public void actionPerformed(ActionEvent e){
     		try{	
       			gridx=Integer.valueOf(rowShow.getText()).intValue();
				gridy=Integer.valueOf(colShow.getText()).intValue();
				mines=Integer.valueOf(mineShow.getText()).intValue();
     		}
     		catch(Exception exp){}
     		
			if(gridx>0 && gridy>0 && mines>0){
				if(mines>(int)(gridx*gridy*0.9)){
					JOptionPane.showMessageDialog(null,"地雷数太大!","开始",JOptionPane.WARNING_MESSAGE);
					mineShow.setText(new Integer((int)(gridx*gridy*0.9)).toString());
				}
				else if(gridx<10){
					JOptionPane.showMessageDialog(null,"地雷区列数太小!","开始",JOptionPane.WARNING_MESSAGE);
					rowShow.setText("10");
				}
				else if(gridy<10){
					JOptionPane.showMessageDialog(null,"地雷区行数太小!","开始",JOptionPane.WARNING_MESSAGE);
					colShow.setText("10");
				}
				else{
					setVisible(false);
				}
			}
			else
				JOptionPane.showMessageDialog(null,"设置值必须大于零!","开始",JOptionPane.WARNING_MESSAGE);
     	}
   	}
   	
   	class Dlg_Close implements WindowListener{
    	public void windowOpened(WindowEvent e){}
    	public void windowClosing(WindowEvent e){
    		OKButton.getActionListeners()[0].actionPerformed(new ActionEvent(OKButton,1,""));
   		}
    	public void windowClosed(WindowEvent e){}
    	public void windowIconified(WindowEvent e){}
    	public void windowDeiconified(WindowEvent e){}
    	public void windowActivated(WindowEvent e){}
    	public void windowDeactivated(WindowEvent e){}
	}

}

⌨️ 快捷键说明

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