如何用C#替代JPanel实现《大富翁》40个方格的有序ArrayList存储?
Absolutely! You can absolutely replicate this functionality in C#—it’s actually pretty straightforward once you map the Java concepts to their C# equivalents. Let’s break this down into two core parts: storing the squares and building the UI.
1. Storing the 40 Squares (Java ArrayList → C# List<T>)
In Java, you’d use ArrayList to store ordered objects. In C#, the direct equivalent is List<T>—a generic, dynamic collection that works exactly how you’d expect. First, define a class to represent each Monopoly square, then populate a List with your 40 ordered squares.
Example Code:
// Class to represent a single Monopoly square public class MonopolySquare { public string Name { get; set; } public SquareType Type { get; set; } public int? Price { get; set; } // Nullable for non-property squares like Go/Jail // Add any other properties you need (e.g., owner, rent value) } // Enum to categorize square types for easy logic/UI styling public enum SquareType { Go, Property, Chance, CommunityChest, Jail, FreeParking, GoToJail, Tax } // Initialize and populate your board list List<MonopolySquare> gameBoard = new List<MonopolySquare>(); // Add squares in order (example of the first few; repeat for all 40) gameBoard.Add(new MonopolySquare { Name = "Go", Type = SquareType.Go }); gameBoard.Add(new MonopolySquare { Name = "Mediterranean Avenue", Type = SquareType.Property, Price = 60 }); gameBoard.Add(new MonopolySquare { Name = "Community Chest", Type = SquareType.CommunityChest }); gameBoard.Add(new MonopolySquare { Name = "Baltic Avenue", Type = SquareType.Property, Price = 60 }); // ... Continue adding the remaining 36 squares in sequence
2. Building the UI (Java JPanel → C# Panel/Layout Containers)
Java’s JPanel is a container for UI elements. In C#, you have two main UI frameworks to choose from—WinForms (simpler for desktop apps) and WPF (more modern, flexible). Here’s how to implement the board in both:
Option A: WinForms (Using FlowLayoutPanel or Panel)
WinForms has FlowLayoutPanel which automatically arranges child controls in a row/column (perfect for a Monopoly board’s grid-like layout). You can also use a regular Panel and manually position controls if you need precise grid placement.
Example Code:
// Assume you have a WinForms Form with a FlowLayoutPanel named `boardLayoutPanel` // Set layout properties for the board boardLayoutPanel.FlowDirection = FlowDirection.TopDown; boardLayoutPanel.WrapContents = true; boardLayoutPanel.Size = new Size(800, 800); // Adjust to fit your square size // Loop through the gameBoard list and create UI elements for each square foreach (var square in gameBoard) { // Create a custom label (or UserControl for more complex squares) Label squareLabel = new Label { Text = square.Name, BorderStyle = BorderStyle.FixedSingle, Size = new Size(80, 80), TextAlign = ContentAlignment.MiddleCenter, Font = new Font("Arial", 8, FontStyle.Bold) }; // Style based on square type switch (square.Type) { case SquareType.Go: squareLabel.BackColor = Color.LightGreen; break; case SquareType.Property: squareLabel.BackColor = Color.LightBlue; break; case SquareType.Jail: squareLabel.BackColor = Color.LightCoral; break; case SquareType.FreeParking: squareLabel.BackColor = Color.LightYellow; break; // Add more cases for other square types } // Add the square to the board container boardLayoutPanel.Controls.Add(squareLabel); }
Option B: WPF (Using ItemsControl + WrapPanel)
WPF uses a more declarative approach with data binding. You can bind your List<MonopolySquare> to an ItemsControl and define a DataTemplate to style each square. This is great for more complex UIs and follows MVVM principles.
Example XAML:
<!-- In your Window/UserControl --> <ItemsControl ItemsSource="{Binding GameBoard}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Width="800" Height="800" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border BorderBrush="Black" BorderThickness="1" Width="80" Height="80" Margin="2"> <StackPanel Background="{Binding Type, Converter={StaticResource SquareTypeToColorConverter}}"> <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="10" FontWeight="Bold"/> <TextBlock Text="{Binding Price, StringFormat='${0}'}" HorizontalAlignment="Center" Visibility="{Binding Price, Converter={StaticResource NullToVisibilityConverter}}"/> </StackPanel> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
You’d need to add value converters to map SquareType to colors and handle visibility for non-property squares, but this keeps your UI logic clean and separated from your data.
Final Notes
C# has all the tools you need to build this—List<T> is a direct replacement for ArrayList, and WinForms/WPF provide flexible container controls like Panel and WrapPanel to replicate JPanel’s functionality. You can even extend this further with custom controls for more interactive squares (e.g., clickable property squares to show purchase options).
内容的提问来源于stack exchange,提问作者As Ma Rezgui

