- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 java 编程已经有大约 4 - 5 个月了,我没有什么可提示的。这很棒。虽然我仍处于学习阶段,但就我投入的时间而言,我已经取得了很大的进步。
好的,我已经创建了一个计算器类。通常,我会非常巧妙地使用多态性(OO)并尝试尽可能地创建相关的类。
只有这一次,当我将计算器类分成单独的类时,我遇到了计算器按钮的问题。
即:我想做的是,制作一个 Calculator 类、一个 ButtonHandler 类和一个 ActionListener 类,为了不写那么多 Operaants,我还想要一个 ScriptEngineManager 类(js)。
但是当我将 Calculator java 类分成这些类时,按钮不会使用react,我得到:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at calc_758D6.NumButtonActionListener.actionPerformed(NumButtonActionListener.java:29)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
但我觉得这很奇怪,因为我使用了“implements ActionListener”并且还使用了“extends Calculator”
所以对象应该正确链接,才能正常工作,对吗?
如果有人可以告诉我如何将此 Calculator 类分成不同的类,那就太好了。
这是我的代码:
package calculator;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Calculator extends JApplet {
// ;which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
public static final long serialVersionUID = 1;
// boolean A flag whether the text in the antwoordCalculatieEntryfield is the result of a calculation
private boolean antwoordCalculatieEntryfield;
//Container The container of our app
private Container con;
//JPanel The 'root' panel
private JPanel panel;
//JTextField The field in which the calculations are created
private JTextField entryField;
//double Memory
private double defaultMemory = 0;
//new
private Menu editDropDownMenu;
private Menu viewDropDownMenu;
private Menu helpDropDownMenu;
private MenuBar menuBalk;
private MenuItem optie1, optie2, optie3;
private MenuItem viewOptie1, viewOptie2;
private MenuItem helpOptie;
/**
* This method is called when the app is an applet
* This will prepare for the app being used as an in-browser applet
*/
public void init() {
this.con = getContentPane();
createGUI();
}
/**
* This method is called when the app is started from the jar file
* This will prepare the app for being used as a standalone application
*/
public void go() {
// Create a frame and set the container
JFrame frame = new JFrame();
menuBalk = new MenuBar();
editDropDownMenu = new Menu ("Edit");
viewDropDownMenu = new Menu ("View");
helpDropDownMenu = new Menu ("Help");
this.con = frame.getContentPane();
// Build the GUI
createGUI();
frame.setLocation(400,400);
frame.setResizable(false);
frame.setMenuBar(menuBalk);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
frame.setVisible(true);
}
/**
* Create the GUI
*/
private void createGUI() {
panel = new JPanel(new BorderLayout());
entryField = new JTextField("0.");
panel.add(entryField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new BorderLayout(10,0));
// Build the different panels
buttonPanel.add(buildNorthPanel(), BorderLayout.NORTH);
buttonPanel.add(buildWestPanel(), BorderLayout.WEST);
buttonPanel.add(buildNumPanel(), BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.CENTER);
con.add(panel);
/**
* De menubalk componenten realiseren en benoemen.
*/
optie1 = new MenuItem(" Copy ");
optie2 = new MenuItem(" Paste ");
optie3 = new MenuItem(" Quit ");
viewOptie1 = new MenuItem("Default");
viewOptie2 = new MenuItem("Scientific");
helpOptie = new MenuItem("Info");
/**
* Toevoegen van edit/view/help componenten aan de menus.
*/
editDropDownMenu.add(optie1);
editDropDownMenu.add(optie2);
editDropDownMenu.addSeparator();
editDropDownMenu.add(optie3);
viewDropDownMenu.add(viewOptie1);
viewDropDownMenu.add(viewOptie2);
helpDropDownMenu.add(helpOptie);
/**
* aan het menubalkje de edit/view/help dropdown opties toevoegen.
*/
menuBalk.add(editDropDownMenu);
menuBalk.add(viewDropDownMenu);
menuBalk.add(helpDropDownMenu);
/**
* Build the northPanel
*
* @return JPanel The created panel
*/
private JPanel buildNorthPanel() {
JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
// We have a placeholder on the top right
JPanel placeHolder = new JPanel();
placeHolder.setPreferredSize(new Dimension(BUTTON_HEIGHT, BUTTON_HEIGHT));
placeHolder.setBorder(BorderFactory.createLoweredBevelBorder());
p.add(placeHolder);
createNorthButton("Backspace", p).addActionListener(new BackspaceActionListener());
createNorthButton("CE", p).addActionListener(new CEActionListener());
createNorthButton("C", p).addActionListener(new CActionListener());
return p;
}
/**
* Build the westPanel
*
* @return JPanel The created panel
*/
private JPanel buildWestPanel() {
JPanel p = new JPanel(new GridLayout(4, 1, 5, 5));
p.setBorder(new EmptyBorder(0, 3, 3, 0));
createWestButton("MC", p);
createWestButton("MR", p);
createWestButton("MS", p);
createWestButton("M+", p);
return p;
}
/**
* Build the numPanel
*
* @return JPanel The created panel
*/
private JPanel buildNumPanel() {
JPanel p = new JPanel(new GridLayout(4, 5, 5, 5));
p.setBorder(new EmptyBorder(0, 0, 4, 4));
createNumButton("7", p);
createNumButton("8", p);
createNumButton("9", p);
createNumButton("/", p);
createNumButton("sqrt", p);
createNumButton("4", p);
createNumButton("5", p);
createNumButton("6", p);
createNumButton("*", p);
createNumButton("%", p);
createNumButton("1", p);
createNumButton("2", p);
createNumButton("3", p);
createNumButton("-", p);
createNumButton("1/x", p);
createNumButton("0", p);
createNumButton("+/-", p);
createNumButton(",", p);
createNumButton("+", p);
createNumButton("=", p);
return p;
}
/**
* Calculate the result from a string containing a calculation
*/
public double calcResult(String text) {
// We use the build-in JavaScript engine to calculate the result
// this saves us a lot of weird conversions and complex calculations
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("JavaScript");
// Do some pre processing to allow javascript to do something with it
text = text.replace("sqrt(", "Math.sqrt(");
try {
return (double) engine.eval(text);
} catch (ScriptException e) {
JOptionPane.showMessageDialog(null, "Er was een probleem met het evalueren van uw input", "foutmelding", JOptionPane.ERROR_MESSAGE);
}
return 0.0;
}
/**
* This will run when the application is run from the jar file
*
* @param args Arguments
*/
public static void main(String[] args) {
Calculator app = new Calculator();
app.go();
}
/**
* Create a button
*
* @param String Contents
* @param JPanel The panel to add
* @return JButton The created button
*/
private JButton createButton(String contents, JPanel p) {
JButton b = new JButton(contents);
b.setMargin(new Insets(0,0,0,0));
p.add(b);
return b;
}
/**
* Create a button for the NumPanel
*
* @param String Contents
* @param JPanel The panel to add
* @return JButton The created button
*/
private JButton createNumButton(String contents, JPanel p) {
JButton b = createButton(contents, p);
b.setPreferredSize(new Dimension(BUTTON_HEIGHT, BUTTON_HEIGHT));
b.addActionListener(new NumButtonActionListener());
return b;
}
/**
* Create a button for the WestPanel
*
* @param String Contents
* @param JPanel The panel to add
* @return JButton The created button
*/
private JButton createWestButton(String contents, JPanel p) {
JButton b = createButton(contents, p);
b.setPreferredSize(new Dimension(BUTTON_HEIGHT, BUTTON_HEIGHT));
b.addActionListener(new MemoryActionListener());
return b;
}
/**
* Create a button for the NorthPanel
*
* @param String Contents
* @param JPanel The panel to add
* @return JButton The created button
*/
private JButton createNorthButton(String contents, JPanel p) {
JButton b = createButton(contents, p);
b.setPreferredSize(new Dimension(100, BUTTON_HEIGHT));
return b;
}
/**
* Check if string b is a number
*
* @param String The string to check
* @return boolean String is a number or not
*/
public boolean isNumber(String b) {
try {
Double.parseDouble(b);
} catch(NumberFormatException ex) {
return false;
}
return true;
}
/**
* This class handles the buttons in the 'numPanel'
*/
class NumButtonActionListener implements ActionListener {
/**
* Responds to a click on the button
*
* @param ActionEvent The event object
*/
public void actionPerformed(ActionEvent ev) {
JButton source = (JButton) ev.getSource();
String sourceText = source.getText();
String origText = calcField.getText();
String textToAdd = "";
// If the text equals the default text, clear it
if(origText.equals(DEFAULT_TEXT)) {
calcField.setText("");
}
// If you just calculated something and wish to use the result
// we don't want to clear the text for you, the text should only
// be cleared when you enter a new number
if (fieldIsResult && isNumber(sourceText)) {
calcField.setText("");
fieldIsResult = false;
} else {
fieldIsResult = false;
}
// Some buttons have special uses
if(sourceText.equals("=")) { // Calculate the result
calcField.setText(""+calcResult(calcField.getText()));
fieldIsResult = true;
} else if(sourceText.equals("sqrt")) { // Square root needs a '('
textToAdd = "sqrt(";
} else if(sourceText.equals("+/-") && origText.length() > 0) { // Toggle negative/non-negative number
char firstChar = origText.charAt(0);
if(firstChar == '-') {
calcField.setText(origText.substring(1, origText.length()));
} else {
calcField.setText("-"+origText);
}
} else { // Just append the button text
textToAdd = sourceText;
}
if( ! textToAdd.equals("")) { // If there is something that needs to be appended
calcField.setText(calcField.getText() + textToAdd);
}
}
}
/**
* This class handles the backspace button
*/
class BackspaceActionListener implements ActionListener {
/**
* Responds to a click on the button
*
* @param ActionEvent The event object
*/
public void actionPerformed(ActionEvent ev) {
String newText = calcField.getText();
// If there are characters
if(newText.length() > 0) {
// Remove the last one
newText = newText.substring(0, newText.length() - 1);
}
calcField.setText(newText);
}
}
/**
* This class handles the C button
*/
class CActionListener implements ActionListener {
/**
* Responds to a click on the button
*
* @param ActionEvent The event object
*/
public void actionPerformed(ActionEvent ev) {
// C will simply clear the field
calcField.setText("");
}
}
/**
* This class handles the CE button
*/
class CEActionListener implements ActionListener {
/**
* Responds to a click on the button
*
* @param ActionEvent The event object
*/
public void actionPerformed(ActionEvent ev) {
// C will simply clear the field
String newText = calcField.getText().replaceAll("[0-9]+$", "");
calcField.setText(newText);
}
}
/**
* This class handles all the memory actions
*/
class MemoryActionListener implements ActionListener {
/**
* Respons to a click on the button
*
* @param ActionEvent The event object
*/
public void actionPerformed(ActionEvent ev) {
JButton source = (JButton) ev.getSource();
String sourceText = source.getText();
if(sourceText.equals("MC")) {
memory = 0.0; // Clear memory
} else if (sourceText.equals("MR")) {
calcField.setText(""+memory);
fieldIsResult = true;
} else if (sourceText.equals("MS") && fieldIsResult) {
memory = Double.parseDouble(calcField.getText());
} else if (sourceText.equals("M+") && fieldIsResult) {
memory = calcResult(memory+"+"+calcField.getText());
}
}
}
}
这就是我将“NumButtonActionListener”类分离时的样子。
public class NumButtonActionListener extends Calculator implements ActionListener {
// boolean A flag whether the text in the antwoordCalculatieEntryfield is the result of a calculation
private boolean antwoordCalculatieEntryfield;
//JTextField The field in which the calculations are created
private JTextField entryField;
/**
* Responds to a click on the button
*
* @param ActionEvent The event object
*/
//line 25
public void actionPerformed(ActionEvent ev) {
JButton source = (JButton) ev.getSource();
String sourceText = source.getText();
//line 29
String origText = entryField.getText();
String textToAdd = "";
// If the text equals the default text, clear it
if(origText.equals("0.")) {
entryField.setText("");
}
// If you just calculated something and wish to use the result
// we don't want to clear the text for you, the text should only
// be cleared when you enter a new number
if (antwoordCalculatieEntryfield && isNumber(sourceText)) {
entryField.setText("");
antwoordCalculatieEntryfield = false;
} else {
antwoordCalculatieEntryfield = false;
}
// Some buttons have special uses
if(sourceText.equals("=")) { // Calculate the result
entryField.setText(""+calcResult(entryField.getText()));
antwoordCalculatieEntryfield = true;
} else if(sourceText.equals("sqrt")) { // Square root needs a '('
textToAdd = "sqrt(";
} else if(sourceText.equals("+/-") && origText.length() > 0) { // Toggle negative/non-negative number
char firstChar = origText.charAt(0);
if(firstChar == '-') {
entryField.setText(origText.substring(1, origText.length()));
} else {
entryField.setText("-"+origText);
}
} else { // Just append the button text
textToAdd = sourceText;
}
if( ! textToAdd.equals("")) { // If there is something that needs to be appended
entryField.setText(entryField.getText() + textToAdd);
}
}
/**
* Check if string b is a number
*
* @param b The string to check
* @return boolean String is a number or not
*/
public boolean isNumber(String b) {
try {
Double.parseDouble(b);
} catch(NumberFormatException ex) {
return false;
}
return true;
}
/**
* Calculate the result from a string containing a calculation
*/
public double calcResult(String text) {
// We use the build-in JavaScript engine to calculate the result
// this saves us a lot of weird conversions and complex calculations
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("JavaScript");
// Do some pre processing to allow javascript to do something with it
text = text.replace("sqrt(", "Math.sqrt(");
try {
return (double) engine.eval(text);
} catch (ScriptException e) {
JOptionPane.showMessageDialog(null, "Er was een probleem met het evalueren van uw input", "foutmelding", JOptionPane.ERROR_MESSAGE);
}
return 0.0;
}
}
最佳答案
你对继承的理解都是错误的
NumButtonActionListener extends Calculator
考虑以下示例:
public class Foo {
FooBoo foofoo;
public Foo() {
foofoo = new FooBoo();
Bar bar = new Bar();
}
}
public class Bar extends Foo {
public Bar() {
someMethod();
}
void someMethod() {
foofoo.method();
}
}
当您创建一个实例 new Bar()
时,这将导致 NullPointerException
因为 Foo
的 FooBoo
code> 类不与 Bar
类中的 FooBoo
相同。它们绝不是同一个引用。 Bar
实例中的 FooBoo
从未初始化过。
您有太多代码需要查看,因此我不会尝试修复您的代码。但我会告诉你如何尝试解决这个问题。为了让 Bar
使用与 Foo
中的对象相同的 FooBoo
对象,它需要对其进行引用。一种方法是简单地将它传递给它的构造函数。类似的东西
public class Foo {
FooBoo foofoo;
public Foo() {
foofoo = new FooBoo();
Bar bar = new Bar(foofoo);
}
}
public class Bar {
FooBoo foo;
public Bar(FooBoo foo) {
this.foo = foo;
someMethod();
}
void someMethod() {
foo.method();
}
}
因此,您将 Foo
的 FooBoo
的引用传递给 Bar
。那么它就是同一个引用的 FooBoo
对象。请注意,我没有使 Bar
扩展 Foo
。这不是继承的正确用法。
关于java - 如何拆分计算器类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25688330/
假设我有这个变量 var image = "image.jpg"; 我正在尝试拆分变量图像的内容并将 _thumbs 插入其中以获得类似 image_thumbs.jpg 的内容。 我该如何解决这个问
我有一个包含多个问题和答案的单元格,其组织方式类似于 CSV。因此,为了将所有这些问题和答案分开,使用逗号作为分隔符的简单拆分应该很容易分开。 不幸的是,有些值使用逗号作为小数分隔符。有没有办法避免这
这是简单的代码: import std.algorithm; import std.array; import std.file; void main(string[] args) { aut
我正在尝试解析一个看起来像的 txt 文件 A - 19 B - 2 C - 3 我正在使用扫描仪方法读取它并在“- ”中拆分,以便我的输出看起来像: A 19 B 2 C 3 但是它似乎没有正确拆分
我有这些网址字符串 file:///home/we/Pictures/neededWord/3193_n.jpg file:///home/smes/Pictures/neededWord/jds_2
我正在解析一个 CVS 文件,如下所示: "07555555555",25.70,18/11/2010,01/03/2011,N,133,0,36,,896,537,547,,Mr,John,Doe,
我在脚本中使用以下行返回 $folder 处所有文件夹的所有路径地点。 dir -recurse $folder|?{$_.PSIsContainer}|select -ExpandProperty
我正在尝试将字符串格式化为word+word+word 例如 “超音乐节”变成“超+音乐+节日” 我尝试过使用以下代码 query.split(" ").join("+"); 或 query.repl
我叫 luis,住在 arg。我有一个问题,无法解决。 **IN BASH** pwd /home/labs-perl ls file1.pl file2.pl **IN PERL** my $ls
我想从包 javax.json 中拆分 JsonArray,但我找不到完成这项工作的便捷方法。我查看了文档,只能想到迭代 JsonArray 并使用 JsonArrayBuilder 手动添加项目。
我希望在第一个 ':' 处拆分字符串,以防止字符串的第二部分包含 ':' 时出现问题。我一直在研究正则表达式,但仍然遇到一些问题,有人可以帮我吗?谢谢。 最佳答案 您可以使用overload of s
我想拆分列表的列表 ((A,1,2,3),(B,4,5,6),(C,7,8,9))进入: (A,1) (A,2) (A,3) (B,4) (B,5) ... 我试过rdd.flatMapValues(
我有一个文本文件,其中每一行都有数据。它看起来像这样: number0;text0 number1;text1 number2;text2 ..等等 所以我通过 xmlhttprequest 将该文本
问题很简单——比如说,我得到了函数,它接收数组作为参数 void calc(double[] data) 如何将这些数据“拆分”成两个子数组并像这样传递给子函数 calc_sub(data(0, le
我想显示来自 EMAIL_TEXT 数据库列的数据,在定义的字符处拆分列。出于某种原因,我的结果只打印第一行到我拆分字符串的位置,跳过其余行。这是我希望在每个“|”之后拆分的数据。 这里是要拆分的数据
我有一个动态数组,我想排除字符串的第一部分,但我不知道第一部分之后会有多少对象,我想将它们全部包含在一个新字符串中。 string = "text.'''hi''','''who''' '''are'
我想拆分 URL 的某些特定部分,这是我目前所做的。 var query = window.location.pathname.split( '/' ); query = window.locati
我有一条消息携带 XML(订单),其中包含多个同质节点(比如产品列表)以及其他信息(比如地址、客户详细信息等)。我必须使用另一个外部服务提供的详细信息来丰富每个“产品”,并返回带有丰富“产品”的相同完
我有一个动态生成的大字符串,我正在拆分它。 var myString="val1, val, val3, val4..... val400" 我对此字符串进行了简单的拆分, myString= myS
这个问题在这里已经有了答案: Java String split removed empty values (5 个答案) 关闭 7 年前。 我正在尝试使用 split(";") 将字符串转换为数组
我是一名优秀的程序员,十分优秀!