我是Java的初学者,我需要制作一个滚动1-8个骰子的程序。如何添加一个文本框,我可以用Java Netbeans填写要显示(和滚动)的骰子数量?
这是我掷3骰子的代码:
public class ToetsJan extends JFrame {
public static void main(String args[]){
JFrame frame = new ToetsJan();
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Dobbelsteen toets");
frame.setContentPane(new TekenPaneel());
frame.setVisible(true);
}
}
class TekenPaneel extends JPanel {
private Dobbelsteen steen1, steen2, steen3;
private JButton werpKnop;
//constructor
public TekenPaneel() {
setLayout(null);
steen1=new Dobbelsteen(1);
steen2=new Dobbelsteen(3);
steen3=new Dobbelsteen(5);
werpKnop = new JButton();
werpKnop.setText("Gooi!");
werpKnop.setBounds(50,150,70,25);
werpKnop.addActionListener(new WerpKnopHandler());
add (werpKnop);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
steen1.tekenStenen(g);
steen1.tekenOgen(g);
steen2.tekenStenen(g);
steen2.tekenOgen(g);
steen3.tekenStenen(g);
steen3.tekenOgen(g);
}
class WerpKnopHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
steen1.gooi();
repaint();
}
}
}
public class Dobbelsteen{
private int x, y;
public Dobbelsteen(int x){
this.x = x;
y = 50*x;
}
public void tekenStenen(Graphics g){
// Teken de dobbelsteen
g.setColor( Color.BLACK );
g.fillRoundRect((y), 55, 60, 60, 25, 25 );
g.setColor( Color.WHITE );
g.drawRoundRect((y), 55, 60, 60, 25, 25 );
}
public void tekenOgen(Graphics g){
int getal = gooi();
if (getal == 1) {
g.fillOval((y+25), 80, 8, 8);
}
else if (getal == 2) {
g.fillOval((y+5), 60, 8, 8);
g.fillOval((y+45), 100, 8, 8);
}
else if (getal == 3){
g.fillOval((y+25), 80, 8, 8);
g.fillOval((y+5), 60, 8, 8);
g.fillOval((y+45), 100, 8, 8);
}
else if (getal == 4){
g.fillOval((y+5), 60, 8, 8);
g.fillOval((y+45), 100, 8, 8);
g.fillOval((y+5), 100, 8, 8);
g.fillOval((y+45), 60, 8, 8);
}
else if (getal == 5){
g.fillOval((y+25), 80, 8, 8);
g.fillOval((y+5), 60, 8, 8);
g.fillOval((y+45), 100, 8, 8);
g.fillOval((y+5), 100, 8, 8);
g.fillOval((y+45), 60, 8, 8);
}
else if (getal == 6){
g.fillOval((y+5), 60, 8, 8);
g.fillOval((y+45), 100, 8, 8);
g.fillOval((y+5), 100, 8, 8);
g.fillOval((y+45), 60, 8, 8);
g.fillOval((y+5), 80, 8, 8);
g.fillOval((y+45), 80, 8, 8);
}
}
public int gooi() {
return (int) (6 * Math.random() + 1);
}
}
答案 0 :(得分:1)
您的代码布局是火车残骸。请使用匹配的缩进编写整洁易读的代码。我建议你使用英文名称来表示类,变量和方法。否则,当您将它们与JDK或几乎总是英语的库名称一起阅读时,它将会令人困惑。
要显示文本对话框,您可以使用以下代码:
public class TextDialog extends Dialog implements ActionListener {
private static final long serialVersionUID = 1L;
private boolean dialogCompleted = false;
private Button ok, can;
private TextField input;
public TextDialog() {
super(new Frame(""), "Text dialog", true);
setLayout(new FlowLayout());
input = new TextField(15);
add(new Label("Input :"));
add(input);
addOKCancelPanel();
createFrame();
pack();
setVisible(true);
}
private void addOKCancelPanel() {
Panel p = new Panel();
p.setLayout(new FlowLayout());
createButtons(p);
add(p);
}
private void createButtons(Panel p) {
p.add(ok = new Button("OK"));
ok.addActionListener(this);
p.add(can = new Button("Cancel"));
can.addActionListener(this);
}
private void createFrame() {
Dimension d = getToolkit().getScreenSize();
setLocation(d.width / 4, d.height / 3);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == ok) {
dialogCompleted = true;
setVisible(false);
} else if (ae.getSource() == can) {
dialogCompleted = false;
setVisible(false);
}
}
public boolean isDialogCompleted() {
return dialogCompleted;
}
public String getInput() {
return input.getText();
}
}
要设置任意数量的骰子,您需要一个可以容纳任意数量骰子的数据结构。看看java.util.List
例如:
private List<Dobbelsteen> dice;
public TekenPaneel() {
TextDialog dialog = new TextDialog();
int numberOfDice;
if (dialog.isDialogCompleted()) {
numberOfDice = Integer.parseInt(dialog.getInput());
}
else {
// use a default if the user cancels the input
numberOfDice = 2;
}
dice = new ArrayList<Dobbelsteen>();
int x = 1;
for(int i = 1; i < numberOfDice; i++) {
dice.add(new Dobbelsteen(x));
x = x + 2;
}