大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
创新互联建站主营鄂伦春网站建设的网络公司,主营网站建设方案,重庆App定制开发,鄂伦春h5微信小程序搭建,鄂伦春网站营销推广欢迎鄂伦春等地区企业咨询
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList; //工具包
import java.text.NumberFormat; //文本包
/**
* java swing计算器
* @author young
*
*/
public class Calculator extends Frame implements ActionListener // 计算器类
{
JTextField result;
NumberButton numberButton[];
OperatorButton operatorButton[];
Button radixpoint, positiveminus, backspace, reciprocal, equal, clear; // 声明成员变量
// 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
Panel panel;
String operator[] = { "+", "-", "*", "/" };
LinkedList linklist;
boolean pressequal = false;
public Calculator() // 构造方法
{
super("计算器");
linklist = new LinkedList();
numberButton = new NumberButton[10];
for (int i = 0; i = 9; i++) {
numberButton[i] = new NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton = new OperatorButton[4];
for (int i = 0; i 4; i++) {
operatorButton[i] = new OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint = new Button(".");
positiveminus = new Button("+/-");
backspace = new Button("CE");
reciprocal = new Button("1/x");
equal = new Button("=");
clear = new Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result = new JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new Font("TimesRoman", Font.PLAIN, 14));
result.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel = new Panel();
panel.setLayout(new GridLayout(4, 5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result, "North");
add(panel, "Center");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(270, 200);
setLocation(300, 230);
setVisible(true);
}
public void actionPerformed(ActionEvent e) // 按钮的单击事件处理方法
{
if (e.getSource() instanceof NumberButton) // 数字按钮
{
NumberButton b = (NumberButton) e.getSource();
if (linklist.size() == 0) {
int number = b.getNumber();
linklist.add("" + number);
result.setText("" + number);
pressequal = false;
}
else if (linklist.size() == 1 pressequal == false) {
int number = b.getNumber();
String num = (String) linklist.getFirst();
String s = num.concat("" + number);
linklist.set(0, s);
result.setText(s);
} else if (linklist.size() == 1 pressequal == true) {
int number = b.getNumber();
linklist.removeFirst();
linklist.add("" + number);
pressequal = false;
result.setText("" + number);
} else if (linklist.size() == 2) {
int number = b.getNumber();
linklist.add("" + number);
result.setText("" + number);
} else if (linklist.size() == 3) {
int number = b.getNumber();
String num = (String) linklist.getLast();
String s = num.concat("" + number);
linklist.set(2, s);
result.setText(s);
}
} else if (e.getSource() instanceof OperatorButton) // 操作按钮
{
OperatorButton b = (OperatorButton) e.getSource();
if (linklist.size() == 1) {
String fuhao = b.getOperator();
linklist.add(fuhao);
} else if (linklist.size() == 2) {
String fuhao = b.getOperator();
linklist.set(1, fuhao);
} else if (linklist.size() == 3) {
String fuhao = b.getOperator();
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals("+")) {
n = n1 + n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
linklist.clear();
linklist.add("" + n);
linklist.add(fuhao);
result.setText("" + n);
} catch (Exception ee) {
}
}
} else if (e.getSource() == equal) // 等号按钮
{
pressequal = true;
if (linklist.size() == 1 || linklist.size() == 2) {
String num = (String) linklist.getFirst();
result.setText("" + num);
} else if (linklist.size() == 3) {
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals("+")) {
n = n1 + n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
result.setText("" + n);
linklist.set(0, "" + n);
linklist.removeLast();
linklist.removeLast();
} catch (Exception ee) {
}
}
} else if (e.getSource() == radixpoint) // 小数点按钮
{
if (linklist.size() == 0) {
pressequal = false;
} else if (linklist.size() == 1) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getFirst();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(0, s);
} else {
s = num;
}
linklist.set(0, s);
result.setText(s);
}
else if (linklist.size() == 3) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getLast();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(2, s);
} else {
s = num;
}
result.setText(s);
}
} else if (e.getSource() == backspace) // 退格按钮
{
if (linklist.size() == 1) {
String num = (String) linklist.getFirst();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(0, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
} else if (linklist.size() == 3) {
String num = (String) linklist.getLast();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(2, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
}
} else if (e.getSource() == positiveminus) // 正负号按钮
{
if (linklist.size() == 1) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(2, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == reciprocal) // 求倒数按钮
{
if (linklist.size() == 1 || linklist.size() == 2) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == clear) // 清零按钮
{
pressequal = false;
result.setText("0");
linklist.clear();
}
}
public static void main(String args[]) {
new Calculator();
}
}
class NumberButton extends Button // 数字按钮类
{
int number;
public NumberButton(int number) // 构造方法
{
super("" + number);
this.number = number;
setForeground(Color.blue);
}
public int getNumber() {
return number;
}
}
class OperatorButton extends Button // 运算符号按钮类
{
String operator;
public OperatorButton(String operator) // 构造方法
{
super(operator);
this.operator = operator;
setForeground(Color.red);
}
public String getOperator() {
return operator;
}
}
eclipse怎样自动补全代码_vs2010代码自动补全Eclipse代码自动补全功能默认只包括点"." ,即只有输入”."后才出现自动补全的提示框。想要自动补全总是去按“Alt+/”也很麻烦。其实只需简单在Eclipse中进行设置即可实现输入任意及符合自动出现自动补全提示框。具体设置步骤如下: 选择Eclipse菜单条中的Windows菜单下的Preferences项。在左侧找到“Java”=》“Editor”=》
Eclipse代码自动补全功能默认只包括 点"." ,即只有输入”."后才出现自动补全的提示框。想要自动补全总是去按 “Alt + / ”也很麻烦。
其实只需简单在Eclipse中进行设置即可实现输入任意及符合自动出现自动补全提示框。
具体设置步骤如下:
选择Eclipse菜单条中的Windows菜单下的Preferences项。在左侧找到“Java” =》 “Editor” =》 “Content Assist”(鼠标点击此项)。在右侧“Auto Activation”项目下找到“Auto activation triggers for Java:”(可以看到设置框中默认的只有 "." ,这就是为什么默认只有点“." 可以触发自动补全窗口了)
eclipse怎样自动补全代码_vs2010代码自动补全
在框中点"."后输入你想要的触发自动补全的字母(中间不需要隔开,挨着依次键入即可),如:“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.”,这样写代码时输入任意的大小写字母均会触发自动补全窗口弹出,输入完后确定OK即可。
eclipse怎样自动补全代码_vs2010代码自动补全
PS:关于网上搜索到的文章大多所述是:Eclipse只支持4个字符的代码补全,要想输入更多的字符需要通过一系列操作编辑文件来实现。这个应该是Eclipse以前版本的特征。目前的新版本已不存在这个问题,直接设置即可实现。
步骤
第一步:打开eclipse,随便开一个java文件。
第二步:在代码中右键,选择Preferences。
第三步:再Preferences里选择java
第四步:再选择Editor,再选择Content Assist
第五步:再在所圈出的地方设置时间(200)和字母(.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz),而后点击ok即可。