📄 mainwnd.java
字号:
package findmines;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.JMenuItem.*;import javax.swing.border.*;import java.io.*;import screen.*;/** * Title: * Description: * Copyright: Copyright (c) 2004 * Company: * @author * @version 1.0 */public class MainWnd extends JFrame { JPanel contentPane; JMenuBar jMenuBar = new JMenuBar(); JMenu jMenuGame = new JMenu(); JMenuItem jMenuGameBegin = new JMenuItem(); JCheckBoxMenuItem jMenuGameFirst = new JCheckBoxMenuItem(); JCheckBoxMenuItem jMenuGameSecond = new JCheckBoxMenuItem(); JCheckBoxMenuItem jMenuGameThird = new JCheckBoxMenuItem(); JCheckBoxMenuItem jMenuGameCustom = new JCheckBoxMenuItem(); JMenuItem jMenuGameWinner = new JMenuItem(); JMenuItem jMenuGameSound = new JMenuItem(); JMenuItem jMenuGameExit = new JMenuItem(); JMenu jMenuHelp = new JMenu(); JMenuItem jMenuHelpAbout = new JMenuItem(); SettingParameters param = new SettingParameters(); MainScreen mainScreen ; int screenHeight; int screenWidth; int grade=0; FlowLayout flowLayout1 = new FlowLayout(); /**Construct the frame*/ public MainWnd() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } ////////////////////////////////////////////////////////////////////////////// /**Game | Begin the game*/ public void jMenuGameBegin_actionPerformed(ActionEvent e) { mainScreen.gameReady(); } /**Game | Set the grade*/ public void jMenuGameGrade_actionPerformed(ActionEvent e,int gd) { if(grade!=gd) { switch(grade) { case 1: jMenuGameFirst.setState(false); break; case 2: jMenuGameSecond.setState(false); break; case 3: jMenuGameThird.setState(false); break; case 4: jMenuGameCustom.setState(false); break; } grade = gd; switch(grade) { case 1: param.height=9; param.width =9; break; case 2: param.height=16; param.width =16; break; case 3: param.height =16; param.width =32; break; case 4: break; } resetScreen(); this.mainScreen.resetParam(param); } } /**Game | Show the Winner List*/ /**Game | Exit action performed*/ public void jMenuGameExit_actionPerformed(ActionEvent e) { putParameters(); System.exit(0); } /**Help | About action performed*/ public void jMenuHelpAbout_actionPerformed(ActionEvent e) { MainWnd_AboutBox dlg = new MainWnd_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { jMenuGameExit_actionPerformed(null); } } void this_keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_F2) { mainScreen.gameReady(); } } //??????????????????????????????????????????? /**read | write parameters from file */ private void getParameters() { try { FileInputStream file = new FileInputStream("Param.ini"); ObjectInputStream input = new ObjectInputStream(file); param = (SettingParameters)input.readObject(); input.close(); file.close(); } catch(ClassNotFoundException e) { System.out.println("Class - SettingParameters not find"); } catch(FileNotFoundException e) { } catch(IOException e) { } } private void putParameters() { try { FileOutputStream file = new FileOutputStream("param.ini"); ObjectOutputStream output = new ObjectOutputStream(file); output.writeObject(param); } catch(FileNotFoundException e) { } catch(IOException e) { } } ///////////////////////////////////////////////////////////////////////////// /**Component initialization*/ private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(MainWnd.class.getResource("images/icon.gif"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(flowLayout1); getParameters(); mainScreen = new MainScreen(param); resetScreen(); initMenu(); this.setTitle("扫雷"); this.setResizable(false); this.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e) { this_keyPressed(e); } }); contentPane.add(mainScreen, null); } /**Initial Menu*/ private void initMenu() { jMenuGame.setText("Game(P)"); jMenuGame.setMnemonic(KeyEvent.VK_P); jMenuGameBegin.setText("Start(N)"); jMenuGameBegin.setMnemonic(KeyEvent.VK_N); jMenuGameBegin.setAccelerator(KeyStroke.getKeyStroke("F2")); jMenuGameBegin.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuGameBegin_actionPerformed(e); } }); /* jMenuGameFirst.setText("初级(B)"); jMenuGameFirst.setMnemonic(KeyEvent.VK_B); jMenuGameFirst.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuGameGrade_actionPerformed(e,1); } }); jMenuGameSecond.setText("中级(I)"); jMenuGameSecond.setMnemonic(KeyEvent.VK_I); jMenuGameSecond.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuGameGrade_actionPerformed(e,2); } }); jMenuGameThird.setText("高级(E)"); jMenuGameThird.setMnemonic(KeyEvent.VK_E); jMenuGameThird.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuGameGrade_actionPerformed(e,3); } }); jMenuGameCustom.setText("自定义(C)..."); jMenuGameCustom.setMnemonic(KeyEvent.VK_C); jMenuGameCustom.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuGameGrade_actionPerformed(e,4); } }); jMenuGameWinner.setText("扫雷英雄榜(T)..."); jMenuGameWinner.setMnemonic(KeyEvent.VK_T); */ jMenuGameExit.setText("Exit(X)"); jMenuGameExit.setMnemonic(KeyEvent.VK_X); jMenuGameExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuGameExit_actionPerformed(e); } }); jMenuHelp.setText("Help(H)"); jMenuHelp.setMnemonic('H'); jMenuHelpAbout.setText("About(A)..."); jMenuHelpAbout.setMnemonic('A'); jMenuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuHelpAbout_actionPerformed(e); } }); ////////////////////////////////////////////////// /** Setting all the menuitems */ jMenuGame.add(jMenuGameBegin); jMenuGame.addSeparator(); /* jMenuGame.add(jMenuGameFirst); jMenuGame.add(jMenuGameSecond); jMenuGame.add(jMenuGameThird); jMenuGame.add(jMenuGameCustom); jMenuGame.addSeparator(); jMenuGame.add(jMenuGameWinner); jMenuGame.addSeparator(); */ jMenuGame.add(jMenuGameExit); jMenuHelp.add(jMenuHelpAbout); jMenuBar.add(jMenuGame); jMenuBar.add(jMenuHelp); jMenuBar.setBorder(BorderFactory.createEmptyBorder()); this.setJMenuBar(jMenuBar); } private void resetScreen() { screenHeight = param .getParamHeight()*(param.mineWidth)+param.displayHeight+(param.borderWidth+2)*2+55; screenWidth = param .getParamWidth()*(param.mineWidth)+(param.borderWidth+2)*2+10; this.setSize(screenWidth,screenHeight); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -