0

你好,我用 Java 做了一个骰子(非常简单),现在当我启动应用程序时,我得到一个非常漂亮的骰子,随机值在 1 到 6 之间,唯一不起作用的是我的按钮再次抛出(比如类不记得什么的),我希望有人能告诉我我做错了什么。您可以在下面找到我的 2 节课

throwbutton 动作类:

package h05Dobbelsteen;

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

public class Worp extends JPanel implements ActionListener {

private JButton worp;

DobbelSteen dobbelsteen;

public Worp() {

    dobbelsteen = new DobbelSteen();
    worp = new JButton("Werp de dobbelsteen");
    worp.addActionListener(this);

    add(worp);

}

@Override
public void actionPerformed(ActionEvent e) {

    dobbelsteen.roll();

}

}

我的模具类如下

package h05Dobbelsteen;

导入 java.awt.*;

导入 javax.swing.JPanel;

公共类 DobbelSteen 扩展 JPanel {

private final static int SPOT_DIAMETER = 40; // diameter dobbelsteen rondjes
private int faceValue; // getoonde waarde op scherm

public DobbelSteen() {

    roll();

}

/*
 * roll de dobbelsteen
 */
public int roll() {

    int val = (int) (6*Math.random() + 1); // bepaal getal tussen 1 - 6
    setValue(val);
    return val;

}

/*
 * set de waarde van de roll
 */
public void setValue(int spots) {
    faceValue = spots;
    repaint();
}

/*
 * get de waarde van de roll
 */
public int getValue() {
    return faceValue;
}

/*
 * teken de view van de dobbelsteen
 */
public void paintComponent(Graphics g) {

    int w = getWidth();  // Get height and width
    int h = getHeight();

    // Graphics naar 2d
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    //... Paint background
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, w, h);
    g2.setColor(Color.BLACK);

    g2.drawRect(0, 0, w-1, h-1);  // Draw border

    switch (faceValue) {
        case 1:
            drawSpot(g2, w/2, h/2);
            break;
        case 3:
            drawSpot(g2, w/2, h/2);

        case 2:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            break;
        case 5:
            drawSpot(g2, w/2, h/2);
        case 4:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            drawSpot(g2, 3*w/4, h/4);
            drawSpot(g2, w/4, 3*h/4);
            break;
        case 6:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            drawSpot(g2, 3*w/4, h/4);
            drawSpot(g2, w/4, 3*h/4);
            drawSpot(g2, w/4, h/2);
            drawSpot(g2, 3*w/4, h/2);
            break;
    }
}

/*
 * Teken de spots
 */
private void drawSpot(Graphics2D g2, int x, int y) {
    g2.fillOval(x-SPOT_DIAMETER/2, y-SPOT_DIAMETER/2, SPOT_DIAMETER, SPOT_DIAMETER);
}

}

JFrame 类

package h05Dobbelsteen;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.*;

public class startDobbelsteen {

JFrame frame = null;

public startDobbelsteen() {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBackground(Color.white);
    frame.setTitle("Dobbelsteen");
    frame.setBounds(200, 200, 450, 450);

    DobbelSteen dobbelsteen = new DobbelSteen();
    frame.add(dobbelsteen, BorderLayout.CENTER);

    Worp worp = new Worp();
    frame.add(worp, BorderLayout.SOUTH);

    frame.setVisible(true);

}

}
4

2 回答 2

1

该程序不完整(没有 main 方法,没有 JFrame),但是您没有在该程序中显示任何 DobbelSteen 实例。由显示的按钮 ( ) 滚动的 DobbelSteenworp永远不会添加到任何面板,因此滚动它不会改变应用程序的外观。

于 2012-10-13T12:13:50.557 回答
1
    DobbelSteen dobbelsteen;

    public Worp(DobbelSteen ds) {

        dobbelsteen = ds;
        worp = new JButton("Werp de dobbelsteen");
       worp.addActionListener(this);

        add(worp);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        dobbelsteen.roll();

    }

    }

你可以试试这个。在您的 JFrame 类构造函数中创建一个 DobbelSteen 并将其添加到容器中。然后做 Worp worp = new Worp(dobbelsteenNaam); 并将 worp 也添加到容器中。我不确定您的 repaint() 调用是否在正确的位置。

于 2012-10-13T12:41:46.397 回答