我在创建一个简单的计算器时遇到了一些问题,但在提问时(如2-1),系统会返回0.0作为结果。我在想,我没有用一种好方法来分析信息。这就是我尝试将双“t”设置为文本框内的数字的方法。
insert = new JTextField();
insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
i = insert.getText();
t = Double.parseDouble(i);
} finally {}
}; {}
});
insert.setBackground(new Color(135, 206, 235));
insert.setText("0");
insert.setFont(new Font("Tahoma", Font.PLAIN, 18));
insert.setColumns(10);
insert.setBounds(0, 50, 292, 28);
frame.getContentPane().add(insert);
如果没有错误,这是完整的代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.awt.event.InputMethodEvent;
public class Window {
double t, x, a, l, s;
String w, i;
Scanner dati = new Scanner(System. in );
private JFrame frame;
private JTextField result;
private JTextField insert;
private JTextField hint;
private JTextField score;
private final JButton help = new JButton("?");
private final JButton calculator = new JButton("=");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Window() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(135, 206, 235));
frame.getContentPane().setLayout(null);
calculator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (l < 6 && l > 0) {
if (l != 2 && l != 3 && l != 4 && l != 5); {
s = x + t;
}
if (l != 1 && l != 3 && l != 4 && l != 5); {
s = x - t;
}
if (l != 1 && l != 2 && l != 4 && l != 5); {
s = x * t;
}
if (l != 1 && l != 2 && l != 3 && l != 5); {
s = x / t;
}
if (l != 1 && l != 2 && l != 4 && l != 3); {
s = (x / 100) * t;
}
result.setText(Double.toString(s));
} else {
result.setText("Kļūda programmā :(");
}
t = 0;
x = 0;
l = 0;
a = 0;
}
});
calculator.setBounds(292, 57, 52, 20);
frame.getContentPane().add(calculator);
calculator.setBackground(new Color(135, 206, 235));
help.setBackground(new Color(135, 206, 235));
help.setBounds(292, 0, 52, 20);
frame.getContentPane().add(help);
score = new JTextField();
score.setEditable(false);
score.setText("\u0160eit rezult\u0101ts");
score.setColumns(10);
score.setBounds(344, 0, 100, 20);
frame.getContentPane().add(score);
hint = new JTextField();
hint.setEditable(false);
hint.setText("\u0160eit tavi skait\u013Ci");
hint.setBounds(344, 57, 100, 20);
frame.getContentPane().add(hint);
hint.setColumns(10);
insert = new JTextField();
insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
i = insert.getText();
t = Double.parseDouble(i);
} finally {}
}; {}
});
insert.setBackground(new Color(135, 206, 235));
insert.setText("0");
insert.setFont(new Font("Tahoma", Font.PLAIN, 18));
insert.setColumns(10);
insert.setBounds(0, 50, 292, 28);
frame.getContentPane().add(insert);
result = new JTextField();
result.setEditable(false);
result.setBackground(new Color(135, 206, 235));
result.setBounds(0, 0, 292, 28);
result.setText("Hei!");
result.setFont(new Font("Tahoma", Font.PLAIN, 18));
frame.getContentPane().add(result);
result.setColumns(10);
JButton add = new JButton("+");
add.setBackground(new Color(135, 206, 250));
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (a != 0) {
x = x + t;
t = 0;
l = 1;
} else {
x = t;
t = 0;
a = 1;
l = 1;
}
}
});
add.setBounds(0, 28, 89, 23);
frame.getContentPane().add(add);
JButton subtract = new JButton("-");
subtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = x - t;
t = 0;
l = 2;
} else {
x = t;
t = 0;
a = 1;
l = 2;
}
}
});
subtract.setBackground(new Color(135, 206, 235));
subtract.setBounds(89, 28, 89, 23);
frame.getContentPane().add(subtract);
JButton multiply = new JButton("x");
multiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = x * t;
t = 0;
l = 3;
} else {
x = t;
t = 0;
a = 1;
l = 3;
}
}
});
multiply.setBackground(new Color(135, 206, 235));
multiply.setForeground(new Color(0, 0, 0));
multiply.setBounds(178, 28, 89, 23);
frame.getContentPane().add(multiply);
JButton divide = new JButton("/");
divide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = x / t;
t = 0;
l = 4;
} else {
x = t;
t = 0;
a = 1;
l = 4;
}
}
});
divide.setBackground(new Color(135, 206, 235));
divide.setBounds(266, 28, 89, 23);
frame.getContentPane().add(divide);
JButton percent = new JButton("%");
percent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a != 0) {
x = (x / 100) * t;
t = 0;
l = 5;
} else {
x = t;
t = 0;
a = 1;
l = 5;
}
}
});
percent.setBackground(new Color(135, 206, 235));
percent.setBounds(355, 28, 89, 23);
frame.getContentPane().add(percent);
frame.setBounds(100, 100, 460, 115);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dati.close();
}
}
可能我用的是一种糟糕的计算方法。
如果你想要一个简单的解释,我试图用五个函数创建一个计算器 - +, - ,*,/,% - 并有一个文本框来插入你想要计算的数字 - 插入,点击功能按钮,插入,单击功能按钮,完成后 - =按钮。 '?'按钮计划用于说明计算器。顺便说一下,我正在为学校项目做这件事。
提前致谢。
P.S。 Kļūdaprogrammā转化为程序错误。
答案 0 :(得分:3)
在actionListener
;
语句中{
if
之前else
switch
此外,你的条件很奇怪,改变它。并添加calculator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (l < 6 && l > 0) {
if (l != 2 && l != 3 && l != 4 && l != 5); {
s = x + t;
}
if (l != 1 && l != 3 && l != 4 && l != 5); {
s = x - t;
}
if (l != 1 && l != 2 && l != 4 && l != 5); {
s = x * t;
}
if (l != 1 && l != 2 && l != 3 && l != 5); {
s = x / t;
}
if (l != 1 && l != 2 && l != 4 && l != 3); {
s = (x / 100) * t;
}
result.setText(Double.toString(s));
} else {
result.setText("Kļūda programmā :(");
}
t = 0;
x = 0;
l = 0;
a = 0;
}
});
语句或calculator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (l < 6 && l > 0) {
if (l == 1) {
s = x + t;
} else if (l == 2) {
s = x - t;
} else if (l == 3) {
s = x * t;
} else if (l == 4) {
s = x / t;
} else if (l == 5) {
s = (x / 100) * t;
}
result.setText(Double.toString(s));
} else {
result.setText("Kļūda programmā :(");
}
t = 0;
x = 0;
l = 0;
a = 0;
}
});
calculator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switch (l) {
case 1:
s = x + t;
result.setText(Double.toString(s));
break;
case 2:
s = x - t;
result.setText(Double.toString(s));
break;
case 3:
s = x * t;
result.setText(Double.toString(s));
break;
case 4:
s = x / t;
result.setText(Double.toString(s));
break;
case 5:
s = (x / 100) * t;
result.setText(Double.toString(s));
break;
default:
result.setText("Kļūda programmā :(");
break;
}
t = 0;
x = 0;
l = 0;
a = 0;
}
});
对此:
+
或者这个(推荐):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class LayoutExample {
double number = 0;
JFrame frame;
JLabel label;
JTextField field;
JButton add, substract, mult, divide, total, help, perc;
JPanel topPane, buttonPane, fieldPane;
LayoutExample() {
frame = new JFrame("Calculator");
label = new JLabel("Result: ");
field = new JTextField("0");
add = new JButton("+");
substract = new JButton("-");
mult = new JButton("*");
divide = new JButton("/");
total = new JButton("=");
perc = new JButton("%");
help = new JButton("?");
topPane = new JPanel();
buttonPane = new JPanel();
fieldPane = new JPanel();
field.setColumns(10);
topPane.setLayout(new FlowLayout());
buttonPane.setLayout(new FlowLayout());
fieldPane.setLayout(new FlowLayout());
topPane.add(label);
topPane.add(help);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
number += Integer.parseInt(field.getText());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
});
total.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Result: " + number);
}
});
buttonPane.add(add);
buttonPane.add(substract);
buttonPane.add(mult);
buttonPane.add(divide);
buttonPane.add(perc);
fieldPane.add(field);
fieldPane.add(total);
frame.add(topPane, BorderLayout.PAGE_START);
frame.add(buttonPane, BorderLayout.CENTER);
frame.add(fieldPane, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
new LayoutExample();
}
}
另外建议避免使用Null Layout,而是使用Layout Manager或它们的组合。这将防止错误,因为Swing旨在与它们一起使用。有关此阅读Why is it frowned upon to use a null layout in swing的更多信息。另请查看The use of multiple JFrames, Good / Bad Practice
要从程序中获得非常相似的视图并使其工作,您可以尝试这样的事情,它不会使用空布局并在每次clic var rgbColorData = ctx.getImageData(e.offsetX, e.offsetY, 1, 1).data;
if (rgbColorData[0] == 255) {
isDrawing = true;
ctx.strokeStyle = "green";
ctx.lineWidth = 4;
ctx.lineJoin = ctx.lineCap = 'round';
ctx.moveTo(e.offsetX, e.offsetY);
}
按钮时打印值。剩下的所有按钮都还不能正常工作(看看我的代码如何与这些按钮一起工作,并将这个逻辑带到其余部分):
{{1}}
输出与您所看到的输出非常相似: