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

📄 frame.java

📁 自制 9格拼图游戏 java jfc程序 按钮拼图 记录步数 用时 重玩 退一步 改变风格
💻 JAVA
字号:
/**
 * @(#)JFCTestFrame.java
 *
 * JFC JFCTest application
 *
 * @author 
 * @version 1.00 2008/9/1
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

class FGButton  extends JButton implements ActionListener
{
    static String []classNames;
    static {
        UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
        int num=infos.length;
        classNames=new String [num];
        for(int i=0;i<num;i++)
        {
            String className =infos[i].getClassName();
            classNames[i]=className;
            My.puts(className);
        }
        // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
    }
    public Component parent;
    public FGButton(String text,Component parent)
    {
        super(text);this.parent=parent;
        this.addActionListener(this);
    }
    int i=1;
    public void actionPerformed(ActionEvent event) {
        My.puts(event);
        try{
            UIManager.setLookAndFeel(classNames[i]);
            SwingUtilities.updateComponentTreeUI(parent);
            // SwingUtilities.updateComponentTreeUI(this);
            My.puts(i,classNames[i]);
        }
        catch(Exception e){
            My.puts(e);
        }
        int num=classNames.length;
        i++;
        if(i==num)
            i=0;
    }
}
public class Frame extends JFrame implements ActionListener
{
    
    /**
     * The constructor
     */
    JButton boxs[]=new JButton[9];
    Data d=new Data();
    JLabel buShu=new JLabel("步数: ");
    JLabel shiJian=new JLabel("时间: ");
    boolean showedMsg=false;
    
	public Frame() throws Exception{
        d.init();
        setTitle("拼图");
        setSize(new Dimension(400, 400));
        
        KeyAdapter keyA=new KeyAdapter(){
            public void keyPressed(KeyEvent e) {
                String fx=e.getKeyText(e.getKeyCode());
                My.puts(fx);
                d.move(fx);
            }
        };
        getContentPane().addKeyListener(keyA);
        // gridC.addKeyListener(keyA);
        // sysC.addKeyListener(keyA);
        // addKeyListener(keyA);
        
        
        JPanel  gridC=new JPanel();
        GridLayout gridl=new GridLayout(3,3);
        gridC.setLayout(gridl);
        for(int i=0;i<9;i++){
            boxs[i]=new JButton();
            boxs[i].addKeyListener(keyA);
            boxs[i].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    int no=0;
                    // My.puts(e.getSource());
                    for(int i=0;i<9;i++){
                        if(e.getSource()==boxs[i]){
                            no=i;break;
                        }
                    }
                    My.puts(no);
                    d.move(no);
                    showedMsg=false;
                }
            });
            gridC.add(boxs[i]);
        }
        add(gridC,BorderLayout.CENTER);
        
        //getContentPane().setLayout(new FlowLayout());
        
        JPanel  sysC=new JPanel();
        int SET_BTN_NUM=7;
        sysC.setLayout(new GridLayout(SET_BTN_NUM,0));

        sysC.add(buShu);
        sysC.add(shiJian);
        
        FGButton setFG=new FGButton("风格",this);
        Vector<JButton> setBtns=new Vector<JButton>();
        setBtns.add(setFG);
        // FlowLayout fl=new FlowLayout();
        // fl.setAlignment(FlowLayout.TRAILING );
        // fl.setAlignOnBaseline(false);
        // sysC.setLayout(fl);
        JButton init=new JButton("新局");
        init.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    d.init();
                }
            });   
        JButton back=new JButton("退一步");
        back.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    d.back();
                }
            });
        JButton re=new JButton("重新此局");
        re.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    d.rePlay();
                }
            });
        JButton quit=new JButton("退出");
        quit.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    System.exit(0);
                }
            });
        
        setBtns.add(init);
        setBtns.add(back);
        setBtns.add(re);
        setBtns.add(quit);
        for(JButton b:setBtns){
            sysC.add(b);
            b.addKeyListener(keyA);
        }
        add(sysC,BorderLayout.EAST);
        // Add window listener.
        int delay = 100; //milliseconds
        new Timer(delay, this).start();
        this.addWindowListener
        (
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    Frame.this.windowClosed();
                }
            }
        );
    }
    public void actionPerformed(ActionEvent evt) {
        buShu.setText("步数: "+d.stepNum);
        shiJian.setText("用时: "+d.timePassed);
        for(int i=0;i<9;i++){
            if(d.nos[i]==0){
                boxs[i].setVisible(false);
                boxs[i].setText("X");
            }
            else{
                boxs[i].setVisible(true);
                boxs[i].setText(""+d.nos[i]);
            }
        }
        if(d.isWin())
        {
            setTitle("拼图 你赢了");
            if(!showedMsg)
                JOptionPane.showMessageDialog(null,"步数: "+d.stepNum+"\n用时: "+d.timePassed,"你赢了", JOptionPane.INFORMATION_MESSAGE);
            showedMsg=true;
        }
        else
            setTitle("拼图");
    }
    /**
     * Shutdown procedure when run as an application.
     */
    protected void windowClosed() {
    	
    	// TODO: Check if it is safe to close the application
    	
        // Exit application.
        System.exit(0);
    }
}

⌨️ 快捷键说明

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