?? mymainframe.java
字號:
/*
* JAVA小游戲-拼圖 我做的第一個小游戲
* Cell類是繼承的按鈕類,并加上相應圖形,形成方格
*MyCanvas是一個面板,加載Cell類的對象(方格),是這三個類中的核心
*
*2007年10月18日-19日
*liumingtao1023@163.com
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyMainFrame extends JFrame implements ActionListener {
MyCanvas myCanvas;
JPanel panelNorth,panelPreview;//定義上方的面板,及預覽所需的面板
Button start,preview,set;//定義開始,預覽,設定按鈕
Container container;//容器,得到內容面板
public MyMainFrame() {//初使化
container=this.getContentPane();
start=new Button("開始");
start.addActionListener(this);
preview=new Button("預覽");
preview.addActionListener(this);
set = new Button("設置");
set.addActionListener(this);
panelPreview=new JPanel();
panelPreview.setLayout(null);
Icon icon=new ImageIcon("pictrue/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
label.setBounds(0,0,300,300);
panelPreview.add(label);
panelNorth=new JPanel();
panelNorth.setBackground(Color.red);
panelNorth.add(start);
panelNorth.add(preview);
panelNorth.add(set);
myCanvas=new MyCanvas();
container.add(myCanvas,BorderLayout.CENTER);
container.add(panelNorth,BorderLayout.NORTH);
this.setTitle("拼圖小游戲-明");
this.setLocation(300,200);
this.setSize(308,365);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args) {
// TODO 自動生成方法存根
new MyMainFrame();
}
public void actionPerformed(ActionEvent arg0) {//對三個按鈕事件的處理
// TODO 自動生成方法存根
Button button=(Button)arg0.getSource();
if(button==start){
myCanvas.Start();
}else if(button==preview){
if(button.getLabel()=="預覽"){
container.remove(myCanvas);
container.add(panelPreview);
panelPreview.updateUI();
container.repaint();
button.setLabel("返回");
}else{
container.remove(panelPreview);
container.add(myCanvas);
container.repaint();
button.setLabel("預覽");
}
}else if(button==set){//修改所選圖片
Choice pic = new Choice();
pic.add("小貓");
pic.add("小豬");
pic.add("云");
pic.add("QQ");
pic.add("卡通");
pic.add("花");
int i=JOptionPane.showConfirmDialog(this, pic, "選擇圖片", JOptionPane.OK_CANCEL_OPTION);
if(i==JOptionPane.YES_OPTION){
MyCanvas.pictureID=pic.getSelectedIndex()+1;
myCanvas.reLoadPictrue();
Icon icon=new ImageIcon("pictrue/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
label.setBounds(0,0,300,300);
panelPreview.removeAll();
panelPreview.add(label);
panelPreview.repaint();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -