在我的骰子上添加“滚动”按钮?

时间:2014-10-22 23:12:42

标签: java button dice

我无法弄清楚如何让滚动按钮与我的程序一起使用。我可以让滚动按钮出现,但无法弄清楚如何让听众工作。我不想继续重新运行该程序以使其工作。

这是我的Die Class:

public class Die {

    public static void main(String[] args) {
    }
        protected int face = 1;

        void roll() {
            face=(int)(Math.random()*6 + 1);
        }

        public int getFace() {
            return face;
        }

        public int setFace() {
            return face;
        }

这是我制作骰子的Graphics类:

public class Graphics extends Die {
    private int x,y;
    boolean locked;


    public Graphics(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void draw(DrawingKit dk){
        Rectangle2D.Float die1 = new Rectangle2D.Float(x, y, 80, 80);

        Ellipse2D.Float topleft = new Ellipse2D.Float(x+3,y+3,18,18);
        Ellipse2D.Float topright = new Ellipse2D.Float(x+55,y+3,18,18);
        Ellipse2D.Float middleleft = new Ellipse2D.Float(x+3,y+28,18,18);
        Ellipse2D.Float middle = new Ellipse2D.Float(x+28,y+28,18,18);
        Ellipse2D.Float middleright = new Ellipse2D.Float(x+55,y+28,18,18);
        Ellipse2D.Float bottomleft = new Ellipse2D.Float(x+3,y+53,18,18);
        Ellipse2D.Float bottomright = new Ellipse2D.Float(x+55,y+53,18,18);
        dk.setPaint(Color.green);
        dk.fill(die1);
        if (face > 1) {
            dk.draw(topleft);
            dk.setPaint(Color.black);
            dk.fill(topleft);}
        if (face > 3)
        {   dk.draw(topright);
        dk.setPaint(Color.black);
        dk.fill(topright);}
        if (face == 6)
        {   dk.draw(middleleft);
        dk.setPaint(Color.black);
        dk.fill(middleleft);}
        if (face % 2 == 1)
        {   dk.draw(middle);
        dk.setPaint(Color.black);
        dk.fill(middle);}
        if (face == 6)
        {   dk.draw(middleright);
        dk.setPaint(Color.black);
        dk.fill(middleright);}
        if (face > 3)
        {   dk.draw(bottomleft);
        dk.setPaint(Color.black);
        dk.fill(bottomleft);}
        if (face > 1)
        {   dk.draw(bottomright);
        dk.setPaint(Color.black);
        dk.fill(bottomright);}
            }



    public static void main(String[] args) {

}
}

最后,这是我的滚动代码,它有效地运行程序并显示按钮:

public class Roll {


    public static void main(String [] args ){

        JPanel topPanel = new JPanel ();
        JButton button1 = new JButton("Roll");
        topPanel.add(button1);

        int cnt1 = 1, cnt2 = 2, cnt3 = 3, cnt4 = 4, cnt5 = 5, cnt6 = 6;
        DrawingKit dk = new DrawingKit("My Dice");
        dk.addPanel(topPanel);
        Graphics die1 = new Graphics(0,45);
                die1.roll();
                die1.draw(dk);
                Graphics die2 = new Graphics(100,45);
                die2.roll();
                die2.draw(dk);
                Graphics die3 = new Graphics(200,45);
                die3.roll();
                die3.draw(dk);
                Graphics die4 = new Graphics(300,45);
                die4.roll();
                die4.draw(dk);
                Graphics die5 = new Graphics(400,45);
                die5.roll();
                die5.draw(dk);


        String s1 = String.format("val1 = %d", (int)10.5678);



        // System.out.println(s1);

        String result = String.format("Rolled a %d", die1.getFace());
        System.out.println(result);

        for (int i = 0; i< 60000; i++) {
            die1.roll();
        die1.setFace();
        switch (die1.getFace()) {
        case 1: cnt1++;;
        break;
        case 2: cnt2++;;
        break;
        case 3: cnt3++;;
        break;
        case 4: cnt4++;;
        break;
        case 5: cnt5++;;
        break;
        case 6: cnt6++;;
        break;
        default: System.out.print("Unexpected value");
        System.out.println(die1.getFace());
    }

}
        System.out.print("Number of 1's ");
        System.out.println(cnt1);
        System.out.print("Number of 2's ");
        System.out.println(cnt2);
        System.out.print("Number of 3's ");
        System.out.println(cnt3);
        System.out.print("Number of 4's ");
        System.out.println(cnt4);
        System.out.print("Number of 5's ");
        System.out.println(cnt5);
        System.out.print("Number of 6's ");
        System.out.println(cnt6);
}
}

任何人都可以向我解释我做错了什么并帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

创建JButton之后,必须为该按钮添加ActionListener。像这样的事情:

button1.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent event) {

        // Whatever you want to do when the user clicks the button

    }

});

例如:

public class Roll {

    DrawingKit dk = new DrawingKit("My Dice");

    Graphics die1 = new Graphics(0,45);
    Graphics die2 = new Graphics(100,45);
    Graphics die3 = new Graphics(200,45);
    Graphics die4 = new Graphics(300,45);
    Graphics die5 = new Graphics(400,45);    

    public static void main(String [] args ){

        JPanel topPanel = new JPanel ();
        JButton button1 = new JButton("Roll");

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                // Roll and draws the dices

                rollDices();
                drawDices();

            }
        });


        topPanel.add(button1);
        dk.addPanel(topPanel);

    }

    public static void rollDices() {

        die1.roll();
        die2.roll();
        die3.roll();
        die4.roll();
        die5.roll();

    }

    public static void rollDices() {

        die1.draw(dk);
        die2.draw(dk);
        die3.draw(dk);
        die4.draw(dk);
        die5.draw(dk);

    }

}