一组radiobutton有二个选项,选中这一通行x的平方,选中另一个运行X立方,并显示
时间: 2025-06-26 13:22:57 浏览: 9
### 实现 RadioButton 功能以计算 X 的平方和立方
以下是基于 C# 和 Java 的两种实现方式,以及 JavaScript 的前端实现方法。
#### 使用 C#
在 Windows Forms 应用程序中可以轻松创建此功能:
```csharp
using System;
using System.Windows.Forms;
public class SquareCubeCalculator : Form
{
private Label labelInput, labelResult;
private TextBox textBoxX;
private RadioButton radioButtonSquare, radioButtonCube;
private Button buttonCalculate;
public SquareCubeCalculator()
{
labelInput = new Label { Text = "Enter value of X:", Location = new System.Drawing.Point(10, 10) };
textBoxX = new TextBox { Location = new System.Drawing.Point(150, 10), Width = 100 };
radioButtonSquare = new RadioButton { Text = "Square", Checked = true, Location = new System.Drawing.Point(10, 40) };
radioButtonCube = new RadioButton { Text = "Cube", Location = new System.Drawing.Point(10, 70) };
buttonCalculate = new Button { Text = "Calculate", Location = new System.Drawing.Point(10, 100) };
buttonCalculate.Click += Calculate_Click;
labelResult = new Label { Text = "", Location = new System.Drawing.Point(10, 140), Font = new System.Drawing.Font("Arial", 12) };
this.Controls.Add(labelInput);
this.Controls.Add(textBoxX);
this.Controls.Add(radioButtonSquare);
this.Controls.Add(radioButtonCube);
this.Controls.Add(buttonCalculate);
this.Controls.Add(labelResult);
this.Text = "Square and Cube Calculator";
this.Size = new System.Drawing.Size(300, 200);
}
private void Calculate_Click(object sender, EventArgs e)
{
double x;
if (double.TryParse(textBoxX.Text, out x))
{
if (radioButtonSquare.Checked)
labelResult.Text = $"Result: {Math.Pow(x, 2)}"; // 计算平方[^1]
else if (radioButtonCube.Checked)
labelResult.Text = $"Result: {Math.Pow(x, 3)}"; // 计算立方[^2]
}
else
{
MessageBox.Show("Please enter a valid number.");
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new SquareCubeCalculator());
}
}
```
#### 使用 Java Swing
通过 Java Swing 可以构建类似的图形界面应用程序:
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SquareCubeCalculator extends JFrame implements ActionListener {
private JLabel labelInput, labelResult;
private JTextField textFieldX;
private JRadioButton radioSquare, radioCube;
private JButton calculateButton;
public SquareCubeCalculator() {
super("Square and Cube Calculator");
setLayout(null);
labelInput = new JLabel("Enter Value of X:");
labelInput.setBounds(10, 10, 150, 20);
add(labelInput);
textFieldX = new JTextField();
textFieldX.setBounds(160, 10, 80, 20);
add(textFieldX);
radioSquare = new JRadioButton("Square");
radioSquare.setBounds(10, 40, 100, 20);
radioSquare.setSelected(true);
add(radioSquare);
radioCube = new JRadioButton("Cube");
radioCube.setBounds(10, 70, 100, 20);
add(radioCube);
ButtonGroup group = new ButtonGroup();
group.add(radioSquare);
group.add(radioCube);
calculateButton = new JButton("Calculate");
calculateButton.setBounds(10, 100, 100, 20);
calculateButton.addActionListener(this);
add(calculateButton);
labelResult = new JLabel("");
labelResult.setBounds(10, 140, 200, 20);
add(labelResult);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(textFieldX.getText());
if (radioSquare.isSelected()) {
labelResult.setText(String.format("Result: %.2f", Math.pow(x, 2))); // 显示平方结果[^3]
} else if (radioCube.isSelected()) {
labelResult.setText(String.format("Result: %.2f", Math.pow(x, 3))); // 显示立方结果[^4]
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid input! Please enter a numeric value.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new SquareCubeCalculator();
}
}
```
#### 使用 JavaScript
对于 Web 前端开发,可以通过 HTML 和 JavaScript 来完成该功能:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Square and Cube Calculator</title>
<script>
function calculateValue() {
const inputValue = parseFloat(document.getElementById('inputX').value);
const resultElement = document.getElementById('result');
if (!isNaN(inputValue)) {
if (document.getElementById('squareRadio').checked) {
resultElement.textContent = `Result: ${Math.pow(inputValue, 2).toFixed(2)}`; // 平方计算[^5]
} else if (document.getElementById('cubeRadio').checked) {
resultElement.textContent = `Result: ${Math.pow(inputValue, 3).toFixed(2)}`; // 立方计算[^6]
}
} else {
alert('Please enter a valid number.');
}
}
</script>
</head>
<body>
<h3>Square and Cube Calculator</h3>
<label for="inputX">Enter value of X:</label>
<input type="number" id="inputX">
<br><br>
<input type="radio" name="operation" id="squareRadio" checked> <label for="squareRadio">Square</label>
<input type="radio" name="operation" id="cubeRadio"> <label for="cubeRadio">Cube</label>
<br><br>
<button onclick="calculateValue()">Calculate</button>
<p id="result"></p>
</body>
</html>
```
阅读全文
相关推荐


















