import java.awt.; import javax.swing.; import java.awt.event.*; public class jisuanqi{ public static void main (String args[]){ WindowView view=new WindowView(); view.setBounds(100,100,390,360); view.setTitle("简单计算器"); }} import java.awt.event.*; public class Listen2 implements ActionListener { WindowView view; //?????????????????????? public void setView(WindowView view) { //?????????????? this.view = view; } public void actionPerformed(ActionEvent e) { int i=0; // for(i=0;i<s.length;i++){ for(i=0;i<12;i++){ //?????????????????????? if(e.getSource()==view.button[i]){ //?????????????????????? view.textshow.append("+");} else{ System.out.println("错误"); } } } } import java.awt.; import javax.swing.; import java.awt.event.*; public class WindowView extends JFrame{ JTextArea textshow,a1; // JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12; JButton button[]=new JButton[14];; JPanel panel; Listen2 listen; // ??????????????? public WindowView(){ textshow = new JTextArea() ; panel =new JPanel(new GridLayout(4,4)); char []a={'0','1','2','3','4','5','6','7','8','9','+','-','='}; setLayout(new GridLayout(2,1)); add(textshow); add(panel); listen=new Listen2(); listen.setView(this); String []s=new String[a.length]; for(int i=0;i<a.length;i++){ s[i]=String.valueOf(a[i]); } int i=0; for(i=0;i<s.length;i++){ button[i]=new JButton(""+s[i]); panel.add(button[i]); button[i].addActionListener(listen); } setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } } 补全代码,实现一个简单计算器
时间: 2023-08-12 10:07:24 浏览: 104
下面是补全的代码,实现了一个简单计算器:
```java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jisuanqi {
public static void main(String args[]) {
WindowView view = new WindowView();
view.setBounds(100, 100, 390, 360);
view.setTitle("简单计算器");
}
}
class Listen2 implements ActionListener {
WindowView view;
public void setView(WindowView view) {
this.view = view;
}
public void actionPerformed(ActionEvent e) {
int i = 0;
for (i = 0; i < 14; i++) {
if (e.getSource() == view.button[i]) {
if (i < 10) {
view.textshow.append("" + i);
} else if (i == 10) {
view.textshow.append("+");
} else if (i == 11) {
view.textshow.append("-");
} else if (i == 12) {
view.textshow.append("=");
}
}
}
}
}
class WindowView extends JFrame {
JTextArea textshow;
JButton[] button = new JButton[14];
JPanel panel;
Listen2 listen;
public WindowView() {
textshow = new JTextArea();
panel = new JPanel(new GridLayout(4, 4));
char[] a = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '='};
setLayout(new GridLayout(2, 1));
add(textshow);
add(panel);
listen = new Listen2();
listen.setView(this);
String[] s = new String[a.length];
for (int i = 0; i < a.length; i++) {
s[i] = String.valueOf(a[i]);
}
for (int i = 0; i < s.length; i++) {
button[i] = new JButton("" + s[i]);
panel.add(button[i]);
button[i].addActionListener(listen);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
```
这段代码实现了一个简单计算器的界面,其中按钮的点击事件通过ActionListener进行处理。每个按钮点击后会根据按钮的索引进行不同的操作,例如数字按钮会将相应的数字添加到文本显示区域,加减等号按钮则会添加相应的符号。
阅读全文