我需要在“解释器模式”的表单设计模式中创建此代码。我该怎么办? [英] I need to make this code to in form design pattern of "Interpreter pattern". How I can do?

查看:82
本文介绍了我需要在“解释器模式”的表单设计模式中创建此代码。我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在解释器模式的表单设计模式中创建此代码。我怎么办?

--------------------------------- -------------------------------------------------- ----------------------------------------





包测试;



import java.awt.BorderLayout;

import java .awt.Container;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt。 event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;



import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;



公共类计算器扩展JPanel实现ActionListener {

private JTextField display = new JTextField(0);



private String buttonText =789/456 * 123-0。= +;



private double result = 0;



private String operator ==;



private boolean calculation = true;



public Calculator(){

setLayout(new BorderLayout());



display.setEditable(false);

add(显示,北);



JPanel p =新JPanel();

p.setLayout(new GridLayout(4,4)) ;



for(int i = 0;我< buttonText.length(); i ++){

JButton b = new JButton(buttonText.substring(i,i + 1));

p.add(b);

b.addActionListener(this);



}

add(p,Center);

}



public void actionPerformed(ActionEvent evt){

String cmd = evt.getActionCommand();

if('0'< = cmd.charAt(0)&& cmd.charAt(0)< ='9'|| cmd.equals(。)){

如果(计算)

display.setText(cmd);

else

display.setText(display.getText()+ cmd);

计算=假;

}否则{

if(计算){

if(cmd.equals( - )){

display.setText(cmd);

计算=假;

}否则

operator = cmd;

} else {

double x = Double.parseDouble(display.getText());

calculate(x) ;

operator = cmd;

计算=真;

}

}

}



private void calculate(double n){

if(operator.equals(+))

result + = n;

else if(operator.equals( - ))

result - = n;

else if(operator.equals(*))

结果* = n;

else if(operator.equals(/))

result / = n;

else if(operator.equals(=))

result = n;

display.setText(+ result);

}



public static void main(String [] args){

JFrame frame = new JFrame();

frame.setTitle(计算器);

frame.setSize(200,200);

frame.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});



容器contentPane = frame.getContentPane();

contentPane.add(新计算器());

frame.show();

}

}

I need to make this code to in form design pattern of "Interpreter pattern". How I can do?
---------------------------------------------------------------------------------------------------------------------------


package test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JPanel implements ActionListener {
private JTextField display = new JTextField("0");

private String buttonText = "789/456*123-0.=+";

private double result = 0;

private String operator = "=";

private boolean calculating = true;

public Calculator() {
setLayout(new BorderLayout());

display.setEditable(false);
add(display, "North");

JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));

for (int i = 0; i < buttonText.length(); i++) {
JButton b = new JButton(buttonText.substring(i, i + 1));
p.add(b);
b.addActionListener(this);

}
add(p, "Center");
}

public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals(".")) {
if (calculating)
display.setText(cmd);
else
display.setText(display.getText() + cmd);
calculating = false;
} else {
if (calculating) {
if (cmd.equals("-")) {
display.setText(cmd);
calculating = false;
} else
operator = cmd;
} else {
double x = Double.parseDouble(display.getText());
calculate(x);
operator = cmd;
calculating = true;
}
}
}

private void calculate(double n) {
if (operator.equals("+"))
result += n;
else if (operator.equals("-"))
result -= n;
else if (operator.equals("*"))
result *= n;
else if (operator.equals("/"))
result /= n;
else if (operator.equals("="))
result = n;
display.setText("" + result);
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Calculator");
frame.setSize(200, 200);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

Container contentPane = frame.getContentPane();
contentPane.add(new Calculator());
frame.show();
}
}

推荐答案

我们没有o你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于我需要在“解释器模式”的表单设计模式中创建此代码。我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆