不行啊?我已经把private全部改成public了啊?具体程序如下 // Fig. 6.10: RollDie.cs // Using random number generation to simulate dice rolling. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; // enables reading data from files using System.Threading;namespace csphtp1.ch06.fig06_09 { /// <summary> /// form simulates the rolling of 12 dice, /// and displays them /// </summary> public class RollDie : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.Button rollButton; public System.Windows.Forms.Label dieLabel2; public System.Windows.Forms.Label dieLabel1; public System.Windows.Forms.Label dieLabel3; public System.Windows.Forms.Label dieLabel4; private Random randomNumber = new Random(); public RollDie() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> public void InitializeComponent() { this.dieLabel3 = new System.Windows.Forms.Label(); this.dieLabel4 = new System.Windows.Forms.Label(); this.dieLabel1 = new System.Windows.Forms.Label(); this.dieLabel2 = new System.Windows.Forms.Label(); this.rollButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // dieLabel3 // this.dieLabel3.Location = new System.Drawing.Point(19, 120); this.dieLabel3.Name = "dieLabel3"; this.dieLabel3.Size = new System.Drawing.Size(92, 74); this.dieLabel3.TabIndex = 4; // // dieLabel4 // this.dieLabel4.Location = new System.Drawing.Point(142, 120); this.dieLabel4.Name = "dieLabel4"; this.dieLabel4.Size = new System.Drawing.Size(92, 74); this.dieLabel4.TabIndex = 5; // // dieLabel1 // this.dieLabel1.Location = new System.Drawing.Point(19, 19); this.dieLabel1.Name = "dieLabel1"; this.dieLabel1.Size = new System.Drawing.Size(92, 74); this.dieLabel1.TabIndex = 0; // // dieLabel2 // this.dieLabel2.Location = new System.Drawing.Point(142, 19); this.dieLabel2.Name = "dieLabel2"; this.dieLabel2.Size = new System.Drawing.Size(92, 74); this.dieLabel2.TabIndex = 1; // // rollButton // this.rollButton.Location = new System.Drawing.Point(50, 211); this.rollButton.Name = "rollButton"; this.rollButton.Size = new System.Drawing.Size(154, 55); this.rollButton.TabIndex = 12; this.rollButton.Text = "Roll"; this.rollButton.Click += new System.EventHandler(this.rollButton_Click); // // RollDie // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(254, 282); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.rollButton, this.dieLabel4, this.dieLabel3, this.dieLabel2, this.dieLabel1}); this.Name = "RollDie"; this.Text = "RollDie"; this.Load += new System.EventHandler(this.RollDie_Load); this.ResumeLayout(false); } #endregion // method called when rollButton clicked, // passes labels to another method protected void rollButton_Click( object sender, System.EventArgs e ) { // pass the labels to a method that will // randomly assign a face to each die
Rolldie rolldie1 = new Rolldie(); Rolldie rolldie2 = new Rolldie(); Thread rolldie1Thread = new Thread ( new ThreadStart (rolldie1.Roll) ); rolldie1Thread.Name = "thread1"; Thread rolldie2Thread = new Thread ( new ThreadStart (rolldie2.Roll) ); rolldie2Thread.Name = "thread2"; rolldie1Thread.Start(); rolldie2Thread.Start(); //DisplayDie( dieLabel1 ); //DisplayDie( dieLabel2 ); //DisplayDie( dieLabel3 ); //DisplayDie( dieLabel4 ); } // end rollButton_Click // determines image to be displayed by current die public void DisplayDie( Label dieLabel ) { int face = randomNumber.Next( 1, 7 );
// displays image specified by filename dieLabel.Image = Image.FromFile( Directory.GetCurrentDirectory() + "\\images\\die" + face +".gif" ); }
/// <summary> /// main entry point for application /// </summary>
private Random randomNumber = new Random(); private Random randomInt = new Random(); public void Roll() {
int sleepTime=randomInt.Next(500,5001); Thread.Sleep(sleepTime); int face =randomNumber.Next(1,7); dieLabel1.Image = Image.FromFile(Directory.GetCurrentDirectory()+"\\images\\die"+face+".gif"); } }} // end namespace csphtp1.ch06.fig06_09
// Fig. 6.10: RollDie.cs
// Using random number generation to simulate dice rolling.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO; // enables reading data from files
using System.Threading;namespace csphtp1.ch06.fig06_09
{
/// <summary>
/// form simulates the rolling of 12 dice,
/// and displays them
/// </summary>
public class RollDie : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; private System.Windows.Forms.Button rollButton; public System.Windows.Forms.Label dieLabel2;
public System.Windows.Forms.Label dieLabel1;
public System.Windows.Forms.Label dieLabel3;
public System.Windows.Forms.Label dieLabel4; private Random randomNumber = new Random(); public RollDie()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
public void InitializeComponent()
{
this.dieLabel3 = new System.Windows.Forms.Label();
this.dieLabel4 = new System.Windows.Forms.Label();
this.dieLabel1 = new System.Windows.Forms.Label();
this.dieLabel2 = new System.Windows.Forms.Label();
this.rollButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// dieLabel3
//
this.dieLabel3.Location = new System.Drawing.Point(19, 120);
this.dieLabel3.Name = "dieLabel3";
this.dieLabel3.Size = new System.Drawing.Size(92, 74);
this.dieLabel3.TabIndex = 4;
//
// dieLabel4
//
this.dieLabel4.Location = new System.Drawing.Point(142, 120);
this.dieLabel4.Name = "dieLabel4";
this.dieLabel4.Size = new System.Drawing.Size(92, 74);
this.dieLabel4.TabIndex = 5;
//
// dieLabel1
//
this.dieLabel1.Location = new System.Drawing.Point(19, 19);
this.dieLabel1.Name = "dieLabel1";
this.dieLabel1.Size = new System.Drawing.Size(92, 74);
this.dieLabel1.TabIndex = 0;
//
// dieLabel2
//
this.dieLabel2.Location = new System.Drawing.Point(142, 19);
this.dieLabel2.Name = "dieLabel2";
this.dieLabel2.Size = new System.Drawing.Size(92, 74);
this.dieLabel2.TabIndex = 1;
//
// rollButton
//
this.rollButton.Location = new System.Drawing.Point(50, 211);
this.rollButton.Name = "rollButton";
this.rollButton.Size = new System.Drawing.Size(154, 55);
this.rollButton.TabIndex = 12;
this.rollButton.Text = "Roll";
this.rollButton.Click += new System.EventHandler(this.rollButton_Click);
//
// RollDie
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(254, 282);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rollButton,
this.dieLabel4,
this.dieLabel3,
this.dieLabel2,
this.dieLabel1});
this.Name = "RollDie";
this.Text = "RollDie";
this.Load += new System.EventHandler(this.RollDie_Load);
this.ResumeLayout(false); }
#endregion // method called when rollButton clicked,
// passes labels to another method
protected void rollButton_Click(
object sender, System.EventArgs e )
{
// pass the labels to a method that will
// randomly assign a face to each die
Rolldie rolldie1 = new Rolldie();
Rolldie rolldie2 = new Rolldie(); Thread rolldie1Thread = new Thread ( new ThreadStart (rolldie1.Roll) );
rolldie1Thread.Name = "thread1";
Thread rolldie2Thread = new Thread ( new ThreadStart (rolldie2.Roll) );
rolldie2Thread.Name = "thread2"; rolldie1Thread.Start();
rolldie2Thread.Start();
//DisplayDie( dieLabel1 );
//DisplayDie( dieLabel2 );
//DisplayDie( dieLabel3 );
//DisplayDie( dieLabel4 ); } // end rollButton_Click // determines image to be displayed by current die
public void DisplayDie( Label dieLabel )
{
int face = randomNumber.Next( 1, 7 );
// displays image specified by filename
dieLabel.Image = Image.FromFile(
Directory.GetCurrentDirectory() +
"\\images\\die" + face +".gif" );
}
/// <summary>
/// main entry point for application
/// </summary>
static void Main()
{
Application.Run( new RollDie() );
} private void RollDie_Load(object sender, System.EventArgs e)
{
} } // end class RollDie
public class Rolldie
{
private Random randomNumber = new Random();
private Random randomInt = new Random();
public void Roll()
{
int sleepTime=randomInt.Next(500,5001);
Thread.Sleep(sleepTime);
int face =randomNumber.Next(1,7);
dieLabel1.Image = Image.FromFile(Directory.GetCurrentDirectory()+"\\images\\die"+face+".gif");
}
}} // end namespace csphtp1.ch06.fig06_09