代码拉取完成,页面将自动刷新
package cn.edu.shengda;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class JiSuanqi {
public static void main(String[] args) {
new myFrame("TWY计算器");
}
}
class myFrame extends JFrame {
JTextField text = new JTextField();
JButton[] bt = new JButton[20];
String key[] = new String[] { "ans", "del", "EC", "C", "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*",
"0", ".", "=", "/" };
JPanel keyPanel = new JPanel();
boolean finish = false;
double ans=0;
public myFrame(String title) {
super(title);
init();
}
public void init() {
text.setHorizontalAlignment(JTextField.RIGHT);
text.setPreferredSize(new Dimension(0, 40));
text.setFont(new Font("宋体", Font.BOLD, 20));
for (int i = 0; i < bt.length; i++) {
bt[i] = new JButton(key[i]);
}
Container pane = this.getContentPane();
this.add(text, BorderLayout.NORTH);
keyPanel.setLayout(new GridLayout(5, 4, 3, 3));
this.add(keyPanel, BorderLayout.CENTER);
for (int i = 0; i < bt.length; i++) {
keyPanel.add(bt[i]);
bt[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (finish == true) {
text.setText("");
finish = false;
}
String command = e.getActionCommand();
String s = text.getText();
if ("0123456789.+-*/".indexOf(command) >= 0)
text.setText(text.getText() + command);
else if (command.equals("del"))
text.setText(s.substring(0, s.length() - 1));
else if (command.equals("C") || command.equals("EC"))
text.setText("");
else if (command.equals("ans") )
text.setText(text.getText() + ans);
else if (command.equals("=")) {
ans=new calc(s).result;
text.setText(text.getText() + "="+ans);
finish = true;
}
}
});
}
this.setVisible(true);
this.setLocation(500, 200);
this.setSize(325, 325);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。