📄 mainframe.java
字号:
private void howtoplayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_howtoplayActionPerformed
try {
// TODO add your handling code here:
Runtime.getRuntime().exec("cmd /c start tetra.hlp");//调用本地帮助文档
} catch (IOException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_howtoplayActionPerformed
private void changecolorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changecolorActionPerformed
if(colorchooser!=null){ //如果窗口已经打开了,就不打开另一个副本
colorchooser.setVisible(true);
return;
}
colorchooser = new JFrame();
colorchooser.add(new ColorSelect(this));
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
colorchooser.setBounds((int)screensize.getHeight()/3,(int)screensize.getHeight()/4,0,0);
colorchooser.pack();
colorchooser.setVisible(true);
}//GEN-LAST:event_changecolorActionPerformed
private void ViewHighScoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewHighScoreActionPerformed
// TODO 将在此处添加您的处理代码:
if(frame!=null){ //如果窗口已经打开了,就不打开另一个副本
frame.setVisible(true);
return;
}
frame=new JFrame();
frame.add(hs);
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds((int)screensize.getHeight()/3,(int)screensize.getHeight()/4,0,0);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}//GEN-LAST:event_ViewHighScoreActionPerformed
private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
// TODO 将在此处添加您的处理代码:
int keycode=evt.getKeyCode();
if(keycode==java.awt.event.KeyEvent.VK_SPACE) g.rotate();
else {
if(keycode==java.awt.event.KeyEvent.VK_UP) g.movCursor(1);
else if(keycode==java.awt.event.KeyEvent.VK_RIGHT) g.movCursor(2);
else if(keycode==java.awt.event.KeyEvent.VK_DOWN) g.movCursor(3);
else if(keycode==java.awt.event.KeyEvent.VK_LEFT) g.movCursor(4);
else if(keycode==java.awt.event.KeyEvent.VK_C) g.placeShape(g.xkey,g.ykey);
}
}//GEN-LAST:event_formKeyPressed
private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
// TODO 将在此处添加您的处理代码:
JFrame aboutFrame=new JFrame();
aboutFrame.setSize(300,200);
aboutFrame.setTitle("欢迎您使用本软件");
aboutFrame.setLayout(new BorderLayout());
aboutFrame.setBounds(400,300,0,0);
aboutFrame.add(new JLabel("开发人:刘悦斌 北京工业大学计算机学院"),BorderLayout.CENTER);
aboutFrame.pack();
aboutFrame.setVisible(true);
aboutFrame.setResizable(false);
}//GEN-LAST:event_aboutActionPerformed
private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
// TODO 将在此处添加您的处理代码:
if(!g.isSaved){
int j=JOptionPane.showConfirmDialog(this,"真的要退出吗?未保存的游戏将丢失","退出",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
if (j == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
else System.exit(0);
}//GEN-LAST:event_exitActionPerformed
private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
// TODO 将在此处添加您的处理代码:
g.saveGame();
}//GEN-LAST:event_saveActionPerformed
private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
// TODO 将在此处添加您的处理代码:
g.loadGame();
}//GEN-LAST:event_openActionPerformed
private void NewGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewGameActionPerformed
// TODO 将在此处添加您的处理代码:
g.newGame();
}//GEN-LAST:event_NewGameActionPerformed
/**
* @param args the command line arguments
*/
private void initInterfaceListener(){
metal.setSelected(true);
ActionListener ifl=new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==metal){
SwitchLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
else if(e.getSource()==winclassic){
SwitchLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
else if(e.getSource()==liquid){
SwitchLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
com.birosoft.liquid.LiquidLookAndFeel.setLiquidDecorations(true);
}
}
};
winclassic.addActionListener(ifl);
metal.addActionListener(ifl);
liquid.addActionListener(ifl);
//substance.addActionListener(ifl);
}
private void SwitchLookAndFeel(String lfname){
try{
UIManager.setLookAndFeel(
//UIManager.getSystemLookAndFeelClassName()
lfname
);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){
System.out.println(e.getMessage());}
}
private void initMusicListener(){
ActionListener a=new ActionListener(){
public void actionPerformed(ActionEvent e) {
int tune = 0;
if(e.getSource()==nomusic){
if(sequence!=null)sequencer.stop();
sequence=null;
}
else if(e.getSource()==music1) tune=1;
else if(e.getSource()==music2) tune = 2;
else if(e.getSource() == music3)tune = 3;
else if(e.getSource()==music4) tune=4;
if(tune!=0)
try {
if(sequence!=null) {
sequencer.stop();
sequence=null;
}
sequence=MidiSystem.getSequence(new File("0"+tune+".mid"));
sequencer=MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
sequencer.start();
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
nomusic.setSelected(true);
nomusic.addActionListener(a);
music1.addActionListener(a);
music2.addActionListener(a);
music3.addActionListener(a);
music4.addActionListener(a);
}
private void initShortcut(){
//生成键盘快捷键
NewGame.setMnemonic(KeyEvent.VK_N);
open.setMnemonic(KeyEvent.VK_O);
save.setMnemonic(KeyEvent.VK_S);
login.setMnemonic(KeyEvent.VK_L);
ViewHighScore.setMnemonic(KeyEvent.VK_H);
exit.setMnemonic(KeyEvent.VK_X);
GameMenu.setMnemonic(KeyEvent.VK_G);
Face.setMnemonic(KeyEvent.VK_F);
winclassic.setMnemonic(KeyEvent.VK_I);
metal.setMnemonic(KeyEvent.VK_T);
Help.setMnemonic(KeyEvent.VK_E);
howtoplay.setMnemonic(KeyEvent.VK_H);
about.setMnemonic(KeyEvent.VK_A);
Music.setMnemonic(KeyEvent.VK_M);
user1.setSelected(true);//将用户1设为默认选中状态
}
public void setStatistics(){
jLabel4.setText(g.level+"");
jLabel5.setText(g.score+"");
jLabel6.setText(g.piecesleft+"");
}
private void initGrid(){
//生成主棋盘与显示下一个将出现的图形区域的格子
for(int j=0;j<8;j++){
for(int i=0;i<8;i++){
a[i][j]=new block(0);
a[i][j].setBorder(javax.swing.BorderFactory.createEtchedBorder());
a[i][j].addMouseListener(new MouseHandler(this));
gamefield.add(a[i][j]);
}
}
for(int j=0;j<5;j++){
for(int i=0;i<5;i++){
b[i][j]=new block(0);
nextshape.add(b[i][j]);
b[i][j].addMouseListener(new java.awt.event.MouseListener() {
public void mousePressed(java.awt.event.MouseEvent evt) {
if(!g.isStart) return;
g.rotate();
}
public void mouseExited(java.awt.event.MouseEvent evt){
}
public void mouseEntered(java.awt.event.MouseEvent evt){
}
public void mouseReleased(java.awt.event.MouseEvent evt){
}
public void mouseClicked(java.awt.event.MouseEvent evt){
}
});
}
}
}
private void initUserListener(){
ActionListener act=new ActionListener(){
public void actionPerformed(ActionEvent e) {
JRadioButtonMenuItem radio=(JRadioButtonMenuItem)e.getSource();
if(radio==user1) g.setUser(1);
else if(radio==user2) g.setUser(2);
else if(radio==user3) g.setUser(3);
else if(radio==user4) g.setUser(4);
else if(radio==user5) g.setUser(5);
else if(radio==user6) g.setUser(6);
else if(radio==user7) g.setUser(7);
else if(radio==user8) g.setUser(8);
}
};
user1.addActionListener(act);
user2.addActionListener(act);
user3.addActionListener(act);
user4.addActionListener(act);
user5.addActionListener(act);
user6.addActionListener(act);
user7.addActionListener(act);
user8.addActionListener(act);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//JFrame.setDefaultLookAndFeelDecorated(true);
new mainFrame().setVisible(true);
}
});
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JMenu Face;
private javax.swing.JMenu GameMenu;
private javax.swing.JMenu Help;
private javax.swing.JMenu Music;
private javax.swing.JMenuItem NewGame;
private javax.swing.JMenuItem ViewHighScore;
private javax.swing.JMenuItem about;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JMenuItem changecolor;
private javax.swing.JMenuItem exit;
private javax.swing.ButtonGroup faceGroup;
private javax.swing.JPanel gamefield;
private javax.swing.JMenuItem howtoplay;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JSeparator jSeparator4;
private javax.swing.JRadioButtonMenuItem liquid;
private javax.swing.JMenu login;
private javax.swing.JRadioButtonMenuItem metal;
private javax.swing.ButtonGroup music;
private javax.swing.JRadioButtonMenuItem music1;
private javax.swing.JRadioButtonMenuItem music2;
private javax.swing.JRadioButtonMenuItem music3;
private javax.swing.JRadioButtonMenuItem music4;
private javax.swing.JPanel nextshape;
private javax.swing.JRadioButtonMenuItem nomusic;
private javax.swing.JMenuItem open;
private javax.swing.JMenuItem save;
private javax.swing.JPanel statistics;
private javax.swing.JRadioButtonMenuItem user1;
private javax.swing.JRadioButtonMenuItem user2;
private javax.swing.JRadioButtonMenuItem user3;
private javax.swing.JRadioButtonMenuItem user4;
private javax.swing.JRadioButtonMenuItem user5;
private javax.swing.JRadioButtonMenuItem user6;
private javax.swing.JRadioButtonMenuItem user7;
private javax.swing.JRadioButtonMenuItem user8;
private javax.swing.JRadioButtonMenuItem winclassic;
// 变量声明结束//GEN-END:variables
block[][] a=new block[8][8];
block[][] b=new block[5][5];
Game g=new Game(this);
HighScorePanel hs=new HighScorePanel();
Sequence sequence;
Sequencer sequencer;
private javax.swing.JFrame frame;
private javax.swing.JFrame colorchooser;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -