txtInput.setText(Double.toString(num1 + num2));是什么意思
时间: 2023-06-14 20:06:41 浏览: 53
`txtInput` 是一个文本框控件,`setText()` 是该控件的方法,用于设置文本框中的文本。`Double.toString(num1 + num2)` 是将 `num1` 和 `num2` 两个 double 类型的变量相加后,将结果转换为字符串。所以,整个语句的意思是将 `num1` 和 `num2` 的和显示在 `txtInput` 文本框中。
相关问题
实现java计算器,支持2、8、10进制。切换进制时数据清空。用btnBin.setBounds。
好的,我可以帮你实现一个支持2、8、10进制切换的Java计算器,并且在切换进制时清空数据,并且用 `btnBin.setBounds` 来设置按钮位置。你可以按照以下步骤进行实现:
1. 创建一个名为 Calculator 的类,其中包含以下成员变量:
```
private int base; // 当前进制
private String input; // 用户输入的数据
private double result; // 计算结果
```
2. 创建一个名为 switchBase 的方法,用于切换进制并清空数据:
```
public void switchBase(int newBase) {
this.base = newBase;
this.input = "";
this.result = 0.0;
}
```
3. 创建一个名为 clear 的方法,用于清空数据:
```
public void clear() {
this.input = "";
this.result = 0.0;
}
```
4. 创建一个名为 input 的方法,用于处理用户输入数据:
```
public void input(String data) {
this.input += data;
}
```
5. 创建一个名为 calculate 的方法,用于计算结果:
```
public void calculate() {
switch (this.base) {
case 2:
this.result = Integer.parseInt(this.input, 2);
break;
case 8:
this.result = Integer.parseInt(this.input, 8);
break;
case 10:
this.result = Double.parseDouble(this.input);
break;
default:
System.out.println("Unsupported base!");
return;
}
this.input = String.valueOf(this.result);
}
```
6. 最后,你可以编写一个简单的测试程序来测试你的计算器:
```
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setSize(300, 400);
Calculator calc = new Calculator();
JTextField txtInput = new JTextField("");
txtInput.setBounds(10, 10, 270, 40);
JButton btnBin = new JButton("Bin");
btnBin.setBounds(10, 60, 80, 40);
btnBin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.switchBase(2);
calc.clear();
txtInput.setText("");
}
});
JButton btnOct = new JButton("Oct");
btnOct.setBounds(100, 60, 80, 40);
btnOct.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.switchBase(8);
calc.clear();
txtInput.setText("");
}
});
JButton btnDec = new JButton("Dec");
btnDec.setBounds(190, 60, 80, 40);
btnDec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.switchBase(10);
calc.clear();
txtInput.setText("");
}
});
JButton btnClear = new JButton("C");
btnClear.setBounds(10, 110, 80, 40);
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.clear();
txtInput.setText("");
}
});
JButton btnEquals = new JButton("=");
btnEquals.setBounds(100, 110, 80, 40);
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.calculate();
txtInput.setText(String.valueOf(calc.getResult()));
}
});
JButton btn0 = new JButton("0");
btn0.setBounds(10, 160, 80, 40);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.input("0");
txtInput.setText(txtInput.getText() + "0");
}
});
JButton btn1 = new JButton("1");
btn1.setBounds(100, 160, 80, 40);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.input("1");
txtInput.setText(txtInput.getText() + "1");
}
});
JButton btn2 = new JButton("2");
btn2.setBounds(190, 160, 80, 40);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calc.input("2");
txtInput.setText(txtInput.getText() + "2");
}
});
frame.add(txtInput);
frame.add(btnBin);
frame.add(btnOct);
frame.add(btnDec);
frame.add(btnClear);
frame.add(btnEquals);
frame.add(btn0);
frame.add(btn1);
frame.add(btn2);
frame.setLayout(null);
frame.setVisible(true);
}
```
这样,你就可以通过点击按钮进行计算器的使用了。希望这个例子对你有所帮助!
用c#写一个计算器窗体程序
好的,下面是一个简单的C#计算器窗体程序示例:
1. 创建一个Windows窗体应用程序项目。
2. 在窗体上放置一些控件,包括Label、TextBox和一些Button。
3. 命名控件的名称,例如,将Label命名为lblResult,TextBox命名为txtInput,Button命名为btnAdd、btnSubtract、btnMultiply、btnDivide和btnEqual。
4. 在窗体的代码文件中添加以下代码:
```csharp
using System;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
Double value = 0;
String operation = "";
bool operation_pressed = false;
public Form1()
{
InitializeComponent();
}
private void button_click(object sender, EventArgs e)
{
if ((txtInput.Text == "0") || (operation_pressed))
txtInput.Clear();
operation_pressed = false;
Button button = (Button)sender;
txtInput.Text = txtInput.Text + button.Text;
}
private void operator_click(object sender, EventArgs e)
{
Button button = (Button)sender;
operation = button.Text;
value = Double.Parse(txtInput.Text);
operation_pressed = true;
lblResult.Text = value + " " + operation;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInput.Text = "0";
value = 0;
lblResult.Text = "";
}
private void btnEqual_Click(object sender, EventArgs e)
{
switch (operation)
{
case "+":
txtInput.Text = (value + Double.Parse(txtInput.Text)).ToString();
break;
case "-":
txtInput.Text = (value - Double.Parse(txtInput.Text)).ToString();
break;
case "*":
txtInput.Text = (value * Double.Parse(txtInput.Text)).ToString();
break;
case "/":
txtInput.Text = (value / Double.Parse(txtInput.Text)).ToString();
break;
default:
break;
}
lblResult.Text = "";
}
}
}
```
5. 运行程序,您就可以使用这个简单的计算器窗体应用程序了。
这个计算器程序可以进行加、减、乘、除四种基本运算,可以清除输入文本框的内容,也可以计算输入的表达式的结果。您可以根据需要进行修改和拓展,增加其他的功能和运算。