- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,这是代码
Gui.java
package mainProgram;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Gui extends JFrame {
final private static JButton numberButtons[] = new JButton[10];
private JButton sum = new JButton("+");
private JButton substract = new JButton("-");
private JButton divide = new JButton("/");
private JButton multiply = new JButton("*");
private JButton sqrt = new JButton("√");
private JButton square = new JButton("x^2");
private JButton cubic = new JButton("x^3");
private JButton percentage = new JButton("%");
private JButton divideByOne = new JButton("1/x");
private JButton C = new JButton("C");
private JButton OK = new JButton("=");
private JButton point = new JButton(".");
private JButton plusMinus = new JButton("+-");
private JTextArea output = new JTextArea();
private JTextField inputOne = new JTextField();
private JPanel panel = new JPanel();
functions fn = new functions();
GuiUpdate gridBagConstr = new GuiUpdate();
HandlerClass handler = new HandlerClass();
public Gui() {
super("Calculator");
setLayout(new GridBagLayout());
requestFocus();
output.setEditable(false);
output.setBackground(Color.GREEN);
// keyBinding for 0-9
for (int i = 0; i <= numberButtons.length - 1; i++) {
String text = String.valueOf(i);
JButton button = new JButton(text);
button.addActionListener(numberAction);
numberButtons[i] = button;
add(button);
// Support Key Bindings
KeyStroke pressed = KeyStroke.getKeyStroke(text);
InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(pressed, text);
button.getActionMap().put(text, numberAction);
}// keyBinding for NUMPAD 0-9
for (int i = 0; i <= numberButtons.length - 1; i++) {
String text = String.valueOf(i);
KeyStroke pressed = KeyStroke.getKeyStroke(0x60 + i, 0);
InputMap inputMap = numberButtons[i]
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(pressed, text);
numberButtons[i].getActionMap().put(text, numberAction);
}
// add Key Bindings listeners
substract.addActionListener(numberAction);
sum.addActionListener(numberAction);
divide.addActionListener(numberAction);
multiply.addActionListener(numberAction);
//keyBinding for divide button
KeyStroke pressed1 = KeyStroke.getKeyStroke(KeyEvent.VK_DIVIDE, 0);
divide.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(pressed1,"divide");
divide.getActionMap().put("divide", numberAction);
//keyBinding for sum button
KeyStroke pressed2 = KeyStroke.getKeyStroke( 0x6B, 0);
sum.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(pressed2, "add");
sum.getActionMap().put("add", numberAction);
//keyBinding for equal button
KeyStroke pressed3 = KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, 0);
OK.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(pressed3, "=");
OK.getActionMap().put("=", numberAction);
gridBagConstr.setConstrains("HORIZONTAL", 4, 1, 0, 0, 30);
add(output, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 4, 1, 0, 1, 15);
add(inputOne, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 2, 0);
add(divide, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 2, 0);
add(multiply, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 2, 0);
add(substract, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 2, 0);
add(sum, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 4, 2, 0);
add(divideByOne, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 3, 0);
add(numberButtons[7], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 3, 0);
add(numberButtons[8], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 3, 0);
add(numberButtons[9], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 3, 0);
add(sqrt, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 4, 0);
add(numberButtons[4], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 4, 0);
add(numberButtons[5], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 4, 0);
add(numberButtons[6], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 4, 0);
add(square, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 5, 0);
add(numberButtons[1], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 5, 0);
add(numberButtons[2], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 5, 0);
add(numberButtons[3], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 5, 0);
add(cubic, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 0, 8, 0);
add(numberButtons[0], gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 1, 8, 0);
add(point, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 2, 8, 0);
add(C, gridBagConstr.c);
gridBagConstr.setConstrains("HORIZONTAL", 1, 1, 3, 8, 0);
add(OK, gridBagConstr.c);
// actionListeners for other operations and for input from the program not from keyboard
inputOne.addActionListener(handler);
divide.addActionListener(handler);
multiply.addActionListener(handler);
substract.addActionListener(handler);
sum.addActionListener(handler);
divideByOne.addActionListener(handler);
sqrt.addActionListener(handler);
square.addActionListener(handler);
cubic.addActionListener(handler);
point.addActionListener(handler);
C.addActionListener(handler);
OK.addActionListener(handler);
}
public void eraseData() {
fn.setA(0);
inputOne.setText("");
}
public void saveInput() {
if (fn.isNumeric(inputOne.getText())) {
fn.setResult(Double.parseDouble(inputOne.getText()));
output.setText(inputOne.getText());
}
}
Action numberAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
String x = e.getActionCommand();
String op = null ;
switch (x) {
case "0":
inputOne.setText(inputOne.getText() + x);
break;
case "1":
inputOne.setText(inputOne.getText() + x);
break;
case "2":
inputOne.setText(inputOne.getText() + x);
break;
case "3":
inputOne.setText(inputOne.getText() + x);
break;
case "4":
inputOne.setText(inputOne.getText() + x);
break;
case "5":
inputOne.setText(inputOne.getText() + x);
break;
case "6":
inputOne.setText(inputOne.getText() + x);
break;
case "7":
inputOne.setText(inputOne.getText() + x);
break;
case "8":
inputOne.setText(inputOne.getText() + x);
break;
case "9":
inputOne.setText(inputOne.getText() + x);
break;
case ".":
inputOne.setText(inputOne.getText() + x);
break;
case "/":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "*":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "-":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "+":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "=":
switch (op) {
case "/":
fn.divide(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "*":
fn.multiply(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "+":
fn.sum(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "-":
fn.substract(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
}
break;
}
}
};
private class HandlerClass implements ActionListener {
String op;
public void actionPerformed(ActionEvent e) {
String x = e.getActionCommand();
switch (x) {
case ".":
inputOne.setText(inputOne.getText() + x);
break;
case "/":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "*":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "-":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "+":
op = e.getActionCommand();
saveInput();
eraseData();
break;
case "√":
if (outputCheckZero(inputOne.getText())) {
fn.sqrt(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
} else {
fn.sqrt(inputOne.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
}
break;
case "x^2":
if (outputCheckZero(inputOne.getText())) {
fn.square(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
} else {
fn.square(inputOne.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
}
break;
case "x^3":
if (outputCheckZero(inputOne.getText())) {
fn.cubic(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
} else {
fn.cubic(inputOne.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
}
break;
case "%":
break;
case "1/x":
if (outputCheckZero(inputOne.getText())) {
fn.divideByOne(output.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
} else {
fn.divideByOne(inputOne.getText());
output.setText(Double.toString(fn.getResult()));
eraseData();
}
break;
case "C":
eraseData();
output.setText("");
fn.setResult(0);
break;
case "=":
switch (op) {
case "/":
fn.divide(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "*":
fn.multiply(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "+":
fn.sum(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
case "-":
fn.substract(inputOne.getText(), fn.getResult());
output.setText(Double.toString(fn.getResult()));
eraseData();
}
break;
case "+-":
break;
default:
break;
}
}
public boolean outputCheckZero(String x) {
if (x.isEmpty())
return true;
else
return false;
}
}
}
functions.java
package mainProgram;
import java.math.*;
import java.util.*;
public class functions {
private double a;
private double result = 0;
public boolean isNumeric(String x) {
try {
Double.parseDouble(x);
return true;
} catch (NumberFormatException nfe) {
}
return false;
}
public double getNumber(String x) {
if (isNumeric(x)) {
this.a = Double.parseDouble(x);
}
return a;
}
public double sum(String a, double b) {
if (isNumeric(a)) {
double c = getNumber(a);
result = b + c;
return result;
} else
return b;
}
public double divide(String a, double b) {
if (isNumeric(a)) {
double c = getNumber(a);
result = b / c;
return result;
} else
return b;
}
public double multiply(String a, double b) {
if (isNumeric(a)) {
double c = getNumber(a);
result = b * c;
return result;
} else
return b;
}
public double substract(String a, double b) {
if (isNumeric(a)) {
double c = getNumber(a);
result = b - c;
return result;
} else
return b;
}
public double sqrt(String a) {
if (isNumeric(a)) {
double c = getNumber(a);
result = Math.sqrt(c);
return result;
} else
return 0;
}
public double square(String a) {
if (isNumeric(a)) {
double c = getNumber(a);
result = c * c;
return result;
} else
return 0;
}
public double cubic(String a) {
if (isNumeric(a)) {
double c = getNumber(a);
result = c * c * c;
return result;
} else
return 0;
}
public double divideByOne(String a) {
if (isNumeric(a)) {
double c = getNumber(a);
result = 1 / c;
return result;
} else
return 0;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}
package mainProgram;
import java.awt.GridBagConstraints;
public class GuiUpdate extends GridBagConstraints {
GridBagConstraints c = new GridBagConstraints();
public void setConstrains (String FILL, int gridwidth, int gridheight, int gridx, int gridy, int ipady ){
if(FILL.toUpperCase().equals("HORIZONTAL")){
this.c.fill = GridBagConstraints.HORIZONTAL;
}else if(FILL.toUpperCase().equals("VERTICAL")){
c.fill = GridBagConstraints.VERTICAL;
}else if (FILL.toUpperCase().equals("BOTH")){
c.fill = GridBagConstraints.BOTH;
}else c.fill = GridBagConstraints.NONE;
this.c.gridwidth = gridwidth;
this.c.gridheight = gridheight;
this.c.gridx = gridx;
this.c.gridy = gridy;
this.c.ipady = ipady;
}
}
package mainProgram;
import javax.swing.JFrame;
public class mainClass {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(250,400);
go.setVisible(true);
}
}
我收到此错误“线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException 在 mainProgram.Gui$1.actionPerformed(Gui.java:225)”。我最好的猜测是我的 String 操作没有值,所以当我单击键盘上的“=”按钮时,编译器不知道要切换到哪种情况.但我不明白的是为什么操作不从我的“/”,“*”,“-”,“+”案例中获取ActionCommand的值。
例如,如果我在程序中写入一个数字,然后单击键盘上的“/”按钮,程序似乎会执行 AbstractAction 方法中“/”情况下的任何操作,但随后我写入第二个数字并单击键盘上的“=”按钮我收到此错误。有什么快速解决办法吗?
最佳答案
该行中的
op
必须为 null
。
在 actionPerformed
之外定义一个 op
变量。
因此它将始终包含最后执行的操作。
如果您启动调试器并进入 actionPerformed
,您会注意到原因。
当调用该方法时,将创建变量 op
,然后为其分配一个值。然后方法结束,变量也结束。
下次创建该方法时,将创建一个值为 null
的新变量。然后您到达线路
case "=":
switch (op) {
其中 op
必须为 null,因为它自创建以来尚未被赋值。
如果将 op
放置在 actionPerformed
之外,它会在方法完成后保留其值。它应该被称为 lastOp
以反射(reflect)它是在该方法的最后一次运行时分配的。
(请注意,在多线程环境中,这种方法可能很危险,但对于 GUI 来说它应该足够好了。)
作为有关如何改进 actionPerformed
方法的建议。记住这个概念:不要重复自己。
String lastOp = null ;
public void actionPerformed(ActionEvent e) {
String x = e.getActionCommand();
switch (x) {
case "/": // an empty case line will execute the elements of the next case.
case "*": // repeatative, so all operator cases here will execute the
case "-": // "+" case, but with their operator.
case "+":
lastOp = e.getActionCommand();
saveInput();
eraseData();
break;
case "=":
switch (op) {
case "/":
fn.divide(inputOne.getText(), fn.getResult());
case "*":
fn.multiply(inputOne.getText(), fn.getResult());
case "+":
fn.sum(inputOne.getText(), fn.getResult());
case "-":
fn.substract(inputOne.getText(), fn.getResult());
}
output.setText(Double.toString(fn.getResult()));
eraseData();
lastOp = null;
break;
}
default: // this handles all number cases.
inputOne.setText(inputOne.getText() + x);
break;
}
关于java - KeyBinding,无法获取按下的按键的String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20594403/
如果您想使用 String.Concat() 连接 5 个或更多字符串,则它会使用 Concat(String[])。 为什么不一直使用 Concat(String[]) 而不再需要 Concat(S
今天在使用 String 时,我遇到了一种我以前不知道的行为。我无法理解内部发生的事情。 public String returnVal(){ return "5";
似乎在我所看到的任何地方,都有一些过时的版本,这些版本不再起作用。 我的问题似乎很简单。我有一个Java类,它映射到derby数据库。我正在使用注释,并且已经成功地在数据库中创建了所有其他表,但是在这
一、string::size_type() 在C++标准库类型 string ,在调用size函数求解string 对象时,返回值为size_type类型,一种类似于unsigned类型的int 数据
我正在尝试将数据保存到我的 plist 文件中,其中包含字符串数组的定义。我的plist - enter image description here 我将数据写入 plist 的代码是 -- let
我有一个带有键/值对的 JavaScript 对象,其中值是字符串数组: var errors = { "Message": ["Error #1", "Error #2"], "Em
例如,为了使用相同的函数迭代 List 和 List> ,我可以编写如下内容: import java.util.*; public class Test{ public static voi
第一个Dictionary就像 Dictionary ParentDict = new Dictionary(); ParentDict.Add("A_1", "1")
这是我的 jsp 文件: 我遇到了错误 The method replace(String, String, String) in the type Functions is not appl
我需要一些帮助。我有一个方法应该输出一个包含列表内容的 txt 文件(每行中的每个项目)。列表项是字符串数组。问题是,当我调用 string.Join 时,它返回文字字符串 "System.Strin
一位同事告诉我,使用以下方法: string url = "SomeURL"; string ext = "SomeExt"; string sub = "SomeSub"; string s
给定类: public class CategoryValuePair { String category; String value; } 还有一个方法: public
我正在尝试合并 Stream>>对象与所有 Streams 中的键一起映射到单个映射中. 例如, final Map someObject; final List>> list = someObjec
在这里使用 IDictionary 的值(value)是什么? 最佳答案 使用接口(interface)的值(value)始终相同:切换到另一个后端实现时,您不必更改客户端代码。 请考虑稍后分析您的代
我可以知道这两个字典声明之间的区别吗? var places = [String: String]() var places = [Dictionary()] 为什么当我尝试以这种方式附加声明时,只有
在 .NET 4.0 及更高版本中存在 string.IsNullOrWhiteSpace(string) 时,在检查字符串时使用 string.IsNullOrEmpty(string) 是否被视为
这个名字背后的原因是什么? SS64在 PowerShell 中解释此处的字符串如下: A here string is a single-quoted or double-quoted string
我打算离开 this 文章,尝试编写一个接受字符串和 &str 的函数,但我遇到了问题。我有以下功能: pub fn new(t_num: S) -> BigNum where S: Into {
我有一个结构为 [String: [String: String]] 的多维数组。我可以使用 for 循环到达 [String: String] 位,但我不知道如何访问主键(这个位 [String:
我正在尝试使用 sarama(管理员模式)创建主题。没有 ConfigEntries 工作正常。但我需要定义一些配置。 我设置了主题配置(这里发生了错误): tConfigs := map[s
我是一名优秀的程序员,十分优秀!