跳棋/跳棋游戏?这可以工作还是我走错了路

发布于 2024-12-27 05:27:33 字数 8886 浏览 1 评论 0原文

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.Action;
import java.util.*;
import java.io.*; 
import javax.swing.*; 

public class testout extends JFrame{ 
    public JLabel l1; 

    //declaring images
    ImageIcon a= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b1.png");
    ImageIcon b= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b2.png");
    ImageIcon c= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b3.png");
    ImageIcon d= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b4.png");
    ImageIcon e= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b5.png");
    ImageIcon f= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b6.png");
    ImageIcon g= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b7.png");
    ImageIcon h= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b8.png");
    ImageIcon i= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b9.png");
    ImageIcon j= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b10.png");
    ImageIcon k= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b11.png");
    ImageIcon l= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b12.png");

    public testout(){ 
        super("Checkers v0.1 beta");//name of window 
        try{
            JFrame checkerboard = new JFrame(); 
            checkerboard.setSize(400, 400);
            checkerboard.setLayout(new GridLayout(8,8));
            checkerboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            checkerboard.setResizable(false); 

            JLabel b1= new JLabel();
            b1.setBackground(Color.black); 
            checkerboard.add(b1); 
            JLabel l1=new JLabel(); 
            l1.setIcon(a); //sets icon for label
            checkerboard.add(l1);

            //mouse event 
            JLabel b2=new JLabel(); 
            checkerboard.add(b2);
            b2.setBackground(Color.BLACK); 
            JLabel l2=new JLabel();
            l2.setIcon(b); 
            checkerboard.add(l2); 

            JLabel b3=new JLabel();
            checkerboard.add(b3); 
            b3.setBackground(Color.BLACK); 
            JLabel l3=new JLabel(); 
            l3.setIcon(c); 
            checkerboard.add(l3); 

            JLabel b4=new JLabel(); 
            checkerboard.add(b4); 
            b4.setBackground(Color.BLACK);
            JLabel l4=new JLabel(); 
            l4.setIcon(d); 
            checkerboard.add(l4);

            JLabel l5=new JLabel(); 
            l5.setIcon(e); 
            checkerboard.add(l5); 
            JLabel b5=new JLabel(); 
            checkerboard.add(b5); 
            b5.setBackground(Color.BLACK);

            JLabel l6=new JLabel(); 
            l6.setIcon(f); 
            checkerboard.add(l6); 
            JLabel b6=new JLabel(); 
            checkerboard.add(b6);
            b6.setBackground(Color.BLACK); 

            JLabel l7=new JLabel();
            l7.setIcon(g); 
            checkerboard.add(l7); 
            JLabel b7=new JLabel();
            checkerboard.add(b7); 
            b7.setBackground(Color.BLACK); 

            JLabel l8=new
            JLabel(); l8.setIcon(h); checkerboard.add(l8); JLabel b8=new
            JLabel(); checkerboard.add(b8); b8.setBackground(Color.BLACK);

            JLabel b9= new JLabel(); checkerboard.add(b9);
            b9.setBackground(Color.BLACK); JLabel l9=new JLabel();
            l9.setIcon(i); checkerboard.add(l9); 

            JLabel b10=new JLabel();
            checkerboard.add(b10); b10.setBackground(Color.BLACK); JLabel
            l10=new JLabel(); l10.setIcon(j); checkerboard.add(l10); JLabel

            b11=new JLabel(); checkerboard.add(b11);
            b11.setBackground(Color.BLACK); JLabel l11=new JLabel();
            l11.setIcon(k); checkerboard.add(l11); 

            JLabel b12=new JLabel();
            checkerboard.add(b12); b12.setBackground(Color.BLACK); JLabel
            l12=new JLabel(); l12.setIcon(l); checkerboard.add(l12); 

            JLabel
            l13=new JLabel("13"); checkerboard.add(l13); JLabel b13=new
            JLabel("13"); checkerboard.add(b13); b13.setBackground(Color.BLACK);

            JLabel l14=new JLabel("14"); checkerboard.add(l14); JLabel b14=new
            JLabel("14"); checkerboard.add(b14); b14.setBackground(Color.BLACK);

            JLabel l15=new JLabel("15"); checkerboard.add(l15); JLabel b15=new
            JLabel("15"); checkerboard.add(b15); b15.setBackground(Color.BLACK);

            JLabel l16=new JLabel("16"); checkerboard.add(l16); JLabel b16= new
            JLabel("16"); checkerboard.add(b16); b16.setBackground(Color.BLACK);

            JLabel b17=new JLabel("17"); checkerboard.add(b17);
            b17.setBackground(Color.BLACK); JLabel l17=new JLabel("17");
            checkerboard.add(l17); 

            JLabel b18=new JLabel("18");
            checkerboard.add(b18); b18.setBackground(Color.BLACK); JLabel
            l18=new JLabel("18"); checkerboard.add(l18); 

            JLabel b19=new
            JLabel("19"); checkerboard.add(b19); b19.setBackground(Color.BLACK);
            JLabel l19=new JLabel("19"); checkerboard.add(l19); 

            JLabel b20=new
            JLabel("20"); checkerboard.add(b20); b20.setBackground(Color.BLACK);
            JLabel l20=new JLabel("20"); checkerboard.add(l20); 

            JLabel l21=new
            JLabel("21"); checkerboard.add(l21); JLabel b21=new JLabel("21");
            checkerboard.add(b21); b21.setBackground(Color.BLACK); 

            JLabel
            l22=new JLabel("22"); checkerboard.add(l22); JLabel b22=new
            JLabel("22"); checkerboard.add(b22); b22.setBackground(Color.BLACK);

            JLabel l23=new JLabel("23"); checkerboard.add(l23); JLabel b23= new
            JLabel("23"); checkerboard.add(b23); b23.setBackground(Color.BLACK);

            JLabel l24=new JLabel("24"); checkerboard.add(l24); JLabel b24=new
            JLabel("24"); checkerboard.add(b24); b24.setBackground(Color.BLACK);

            JLabel b25=new JLabel("25"); checkerboard.add(b25);
            b25.setBackground(Color.BLACK); JLabel l25=new JLabel("25");
            checkerboard.add(l25); 

            JLabel b26=new JLabel("26");
            checkerboard.add(b26); b26.setBackground(Color.BLACK); JLabel
            l26=new JLabel("26"); checkerboard.add(l26); 

            JLabel b27=new
            JLabel("27"); b27.setBackground(Color.BLACK); checkerboard.add(b27);
            JLabel l27=new JLabel("27"); checkerboard.add(l27); 

            JLabel b28=new
            JLabel("28"); checkerboard.add(b28); b28.setBackground(Color.BLACK);
            JLabel l28=new JLabel("28"); checkerboard.add(l28); 

            JLabel l29=new
            JLabel("29"); checkerboard.add(l29); JLabel b29=new JLabel("29");
            checkerboard.add(b29); b29.setBackground(Color.BLACK); 

            JLabel
            l30=new JLabel("30"); checkerboard.add(l30); JLabel b30= new
            JLabel("30"); checkerboard.add(b30); b30.setBackground(Color.BLACK);

            JLabel l31=new JLabel("31"); checkerboard.add(l31); JLabel b31=new
            JLabel("31"); checkerboard.add(b31); b31.setBackground(Color.BLACK);

            JLabel l32=new JLabel("32"); checkerboard.add(l32); JLabel b32=new
            JLabel("32"); checkerboard.add(b32); b32.setBackground(Color.BLACK);

            checkerboard.setVisible(true); //event handlers 
            moves move=new moves(); //listener 
            l1.addMouseListener(move);
            l1.addMouseMotionListener(move); 
            l12.addMouseMotionListener(move);
            l12.addMouseListener(move); 
        } catch(Exception w){ } 
    } 

    public class moves implements MouseListener,MouseMotionListener{ 
        public void mouseClicked(MouseEvent z){
            if(l1.contains(z.getX(),z.getY()))updateLocation(z); 
        } 

        public void mousePressed(MouseEvent z){ } 
        public void mouseDragged(MouseEvent z){ updateLocation(z); } 
        public void mouseReleased(MouseEvent z){ }
        public void mouseEntered(MouseEvent z){} 
        public void mouseExited(MouseEvent z){} 
        public void mouseMoved(MouseEvent z){ }
        public void updateLocation(MouseEvent z){ 
            l1.setLocation(z.getX(), z.getY()); 
            if(l1.contains(z.getX(),z.getY())) { 
            } else {
            } 
            repaint(); 
        }
    }
}
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.Action;
import java.util.*;
import java.io.*; 
import javax.swing.*; 

public class testout extends JFrame{ 
    public JLabel l1; 

    //declaring images
    ImageIcon a= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b1.png");
    ImageIcon b= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b2.png");
    ImageIcon c= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b3.png");
    ImageIcon d= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b4.png");
    ImageIcon e= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b5.png");
    ImageIcon f= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b6.png");
    ImageIcon g= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b7.png");
    ImageIcon h= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b8.png");
    ImageIcon i= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b9.png");
    ImageIcon j= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b10.png");
    ImageIcon k= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b11.png");
    ImageIcon l= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b12.png");

    public testout(){ 
        super("Checkers v0.1 beta");//name of window 
        try{
            JFrame checkerboard = new JFrame(); 
            checkerboard.setSize(400, 400);
            checkerboard.setLayout(new GridLayout(8,8));
            checkerboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            checkerboard.setResizable(false); 

            JLabel b1= new JLabel();
            b1.setBackground(Color.black); 
            checkerboard.add(b1); 
            JLabel l1=new JLabel(); 
            l1.setIcon(a); //sets icon for label
            checkerboard.add(l1);

            //mouse event 
            JLabel b2=new JLabel(); 
            checkerboard.add(b2);
            b2.setBackground(Color.BLACK); 
            JLabel l2=new JLabel();
            l2.setIcon(b); 
            checkerboard.add(l2); 

            JLabel b3=new JLabel();
            checkerboard.add(b3); 
            b3.setBackground(Color.BLACK); 
            JLabel l3=new JLabel(); 
            l3.setIcon(c); 
            checkerboard.add(l3); 

            JLabel b4=new JLabel(); 
            checkerboard.add(b4); 
            b4.setBackground(Color.BLACK);
            JLabel l4=new JLabel(); 
            l4.setIcon(d); 
            checkerboard.add(l4);

            JLabel l5=new JLabel(); 
            l5.setIcon(e); 
            checkerboard.add(l5); 
            JLabel b5=new JLabel(); 
            checkerboard.add(b5); 
            b5.setBackground(Color.BLACK);

            JLabel l6=new JLabel(); 
            l6.setIcon(f); 
            checkerboard.add(l6); 
            JLabel b6=new JLabel(); 
            checkerboard.add(b6);
            b6.setBackground(Color.BLACK); 

            JLabel l7=new JLabel();
            l7.setIcon(g); 
            checkerboard.add(l7); 
            JLabel b7=new JLabel();
            checkerboard.add(b7); 
            b7.setBackground(Color.BLACK); 

            JLabel l8=new
            JLabel(); l8.setIcon(h); checkerboard.add(l8); JLabel b8=new
            JLabel(); checkerboard.add(b8); b8.setBackground(Color.BLACK);

            JLabel b9= new JLabel(); checkerboard.add(b9);
            b9.setBackground(Color.BLACK); JLabel l9=new JLabel();
            l9.setIcon(i); checkerboard.add(l9); 

            JLabel b10=new JLabel();
            checkerboard.add(b10); b10.setBackground(Color.BLACK); JLabel
            l10=new JLabel(); l10.setIcon(j); checkerboard.add(l10); JLabel

            b11=new JLabel(); checkerboard.add(b11);
            b11.setBackground(Color.BLACK); JLabel l11=new JLabel();
            l11.setIcon(k); checkerboard.add(l11); 

            JLabel b12=new JLabel();
            checkerboard.add(b12); b12.setBackground(Color.BLACK); JLabel
            l12=new JLabel(); l12.setIcon(l); checkerboard.add(l12); 

            JLabel
            l13=new JLabel("13"); checkerboard.add(l13); JLabel b13=new
            JLabel("13"); checkerboard.add(b13); b13.setBackground(Color.BLACK);

            JLabel l14=new JLabel("14"); checkerboard.add(l14); JLabel b14=new
            JLabel("14"); checkerboard.add(b14); b14.setBackground(Color.BLACK);

            JLabel l15=new JLabel("15"); checkerboard.add(l15); JLabel b15=new
            JLabel("15"); checkerboard.add(b15); b15.setBackground(Color.BLACK);

            JLabel l16=new JLabel("16"); checkerboard.add(l16); JLabel b16= new
            JLabel("16"); checkerboard.add(b16); b16.setBackground(Color.BLACK);

            JLabel b17=new JLabel("17"); checkerboard.add(b17);
            b17.setBackground(Color.BLACK); JLabel l17=new JLabel("17");
            checkerboard.add(l17); 

            JLabel b18=new JLabel("18");
            checkerboard.add(b18); b18.setBackground(Color.BLACK); JLabel
            l18=new JLabel("18"); checkerboard.add(l18); 

            JLabel b19=new
            JLabel("19"); checkerboard.add(b19); b19.setBackground(Color.BLACK);
            JLabel l19=new JLabel("19"); checkerboard.add(l19); 

            JLabel b20=new
            JLabel("20"); checkerboard.add(b20); b20.setBackground(Color.BLACK);
            JLabel l20=new JLabel("20"); checkerboard.add(l20); 

            JLabel l21=new
            JLabel("21"); checkerboard.add(l21); JLabel b21=new JLabel("21");
            checkerboard.add(b21); b21.setBackground(Color.BLACK); 

            JLabel
            l22=new JLabel("22"); checkerboard.add(l22); JLabel b22=new
            JLabel("22"); checkerboard.add(b22); b22.setBackground(Color.BLACK);

            JLabel l23=new JLabel("23"); checkerboard.add(l23); JLabel b23= new
            JLabel("23"); checkerboard.add(b23); b23.setBackground(Color.BLACK);

            JLabel l24=new JLabel("24"); checkerboard.add(l24); JLabel b24=new
            JLabel("24"); checkerboard.add(b24); b24.setBackground(Color.BLACK);

            JLabel b25=new JLabel("25"); checkerboard.add(b25);
            b25.setBackground(Color.BLACK); JLabel l25=new JLabel("25");
            checkerboard.add(l25); 

            JLabel b26=new JLabel("26");
            checkerboard.add(b26); b26.setBackground(Color.BLACK); JLabel
            l26=new JLabel("26"); checkerboard.add(l26); 

            JLabel b27=new
            JLabel("27"); b27.setBackground(Color.BLACK); checkerboard.add(b27);
            JLabel l27=new JLabel("27"); checkerboard.add(l27); 

            JLabel b28=new
            JLabel("28"); checkerboard.add(b28); b28.setBackground(Color.BLACK);
            JLabel l28=new JLabel("28"); checkerboard.add(l28); 

            JLabel l29=new
            JLabel("29"); checkerboard.add(l29); JLabel b29=new JLabel("29");
            checkerboard.add(b29); b29.setBackground(Color.BLACK); 

            JLabel
            l30=new JLabel("30"); checkerboard.add(l30); JLabel b30= new
            JLabel("30"); checkerboard.add(b30); b30.setBackground(Color.BLACK);

            JLabel l31=new JLabel("31"); checkerboard.add(l31); JLabel b31=new
            JLabel("31"); checkerboard.add(b31); b31.setBackground(Color.BLACK);

            JLabel l32=new JLabel("32"); checkerboard.add(l32); JLabel b32=new
            JLabel("32"); checkerboard.add(b32); b32.setBackground(Color.BLACK);

            checkerboard.setVisible(true); //event handlers 
            moves move=new moves(); //listener 
            l1.addMouseListener(move);
            l1.addMouseMotionListener(move); 
            l12.addMouseMotionListener(move);
            l12.addMouseListener(move); 
        } catch(Exception w){ } 
    } 

    public class moves implements MouseListener,MouseMotionListener{ 
        public void mouseClicked(MouseEvent z){
            if(l1.contains(z.getX(),z.getY()))updateLocation(z); 
        } 

        public void mousePressed(MouseEvent z){ } 
        public void mouseDragged(MouseEvent z){ updateLocation(z); } 
        public void mouseReleased(MouseEvent z){ }
        public void mouseEntered(MouseEvent z){} 
        public void mouseExited(MouseEvent z){} 
        public void mouseMoved(MouseEvent z){ }
        public void updateLocation(MouseEvent z){ 
            l1.setLocation(z.getX(), z.getY()); 
            if(l1.contains(z.getX(),z.getY())) { 
            } else {
            } 
            repaint(); 
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2025-01-03 05:27:33

也许您需要学习的最重要的编程原则是“不要重复自己”(DRY)。请注意您的代码有多么重复。

在第一个块中,您声明 12 个变量(名为 a、b、c 等),这些变量仅在成员名称和文件名上有所不同。您应该用数组等集合替换重复的数据结构,并用循环替换重复的代码。例如:

private ImageIcon[] icons;

// Then, in your constructor
for (int i = 0; i < 12; i++) {
    icons[i] = new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b" + (i + 1).toString() + ".png");
}

注意如何将 12 行代码替换为 4 行代码。考虑用循环和集合替换其余的重复代码和数据。否则,您可能会发现代码太大而难以管理,并且太耗时而无法更改。

Maybe the most important programming principle for you to learn is Don't Repeat Yourself (DRY). Notice how repetitive your code is.

In the first block, you declare 12 variables (named a, b, c, etc.), that only vary on the member names and the file names. You should replace repeated data structures with collections like arrays and repeated code with loops. For example:

private ImageIcon[] icons;

// Then, in your constructor
for (int i = 0; i < 12; i++) {
    icons[i] = new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b" + (i + 1).toString() + ".png");
}

Notice how 12 lines of code can be replaced with 4. Consider replacing the rest of your repetitive code and data with loops and collections. Otherwise, you may find the code too big to manage and too time-intensive to change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文