2014-04-10 46 views
-2

我是一名Java初學者,我需要製作一個可擲骰子的程序。我怎樣才能添加一個文本框,在那裏我可以填寫用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); 
    } 
} 

回答

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; 
    } 
+0

無我有JAVA一個新手 – Koessien