
java编程高手帮帮忙
设计GUI界面的计算器程序,用户可以通过鼠标或键盘输入需要计算的数据,进行加、减、乘、除等运算。该系统采用Swing/SWT组件实现。谢谢了,做好后另有分希望高手帮我做个...
设计GUI界面的计算器程序,用户可以通过鼠标或键盘输入需要计算的数据,进行加、减、乘、除等运算。该系统采用Swing/SWT组件实现。
谢谢了,做好后另有分
希望高手帮我做个完整点的程序,不要老出些小错误。主要是键盘输入时的错误,要保证计算器上面的按钮键盘都能输入,而且一次运算完后再按一个数能把上次运算结果覆盖,看见好几个的能把运算符号连输几遍的错误。谢谢了,做完后我再给100分。 展开
谢谢了,做好后另有分
希望高手帮我做个完整点的程序,不要老出些小错误。主要是键盘输入时的错误,要保证计算器上面的按钮键盘都能输入,而且一次运算完后再按一个数能把上次运算结果覆盖,看见好几个的能把运算符号连输几遍的错误。谢谢了,做完后我再给100分。 展开
展开全部
package calc;
import java.awt.BorderLayout;
import java.awt.Color;
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;
//计算器
public class Calc extends JFrame implements ActionListener {
private JPanel mainPanel = new JPanel();
private Font font = new Font("", Font.PLAIN, 20);
private int N1; // 数1
private int N2; // 数2
private String ope; // 存储运算符
private JTextField tf;
public JPanel createP1() {
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1, 5, 5));
tf = new JTextField();
tf.setFont(font);
p1.add(tf);
return p1;
}
public JPanel createP2() {
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(5, 1, 5, 5));
JButton[] btns = new JButton[5];
String[] labels = new String[] { "", "MC", "MR", "MS", "M+" };
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton(labels[i]);
btns[i].setForeground(Color.red);
btns[i].setFont(font);
p2.add(btns[i]);
}
return p2;
}
public JPanel createP3() {
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout(5, 5));
JPanel p3_1 = this.createP3_1();
JPanel p3_2 = this.createP3_2();
p3.add(p3_1, BorderLayout.NORTH);
p3.add(p3_2, BorderLayout.CENTER);
return p3;
}
public JPanel createP3_1() {
JPanel p3_1 = new JPanel();
p3_1.setLayout(new GridLayout(1, 3, 5, 5));
JButton[] btns = new JButton[3];
String[] labels = new String[] { "Backspace", "CE", "C" };
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton(labels[i]);
btns[i].setForeground(Color.red);
btns[i].setFont(font);
p3_1.add(btns[i]);
}
return p3_1;
}
public JPanel createP3_2() {
JPanel p3_2 = new JPanel();
GridLayout gl = new GridLayout(4, 5, 5, 5);
p3_2.setLayout(gl);
/* 按钮数组 */
String[] labels = new String[] { "7", "8", "9", "/", "sqrt", "4", "5",
"6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+",
"=" };
Color[] colors = new Color[] { Color.blue, Color.blue, Color.blue,
Color.red, Color.blue, Color.blue, Color.blue, Color.blue,
Color.red, Color.blue, Color.blue, Color.blue, Color.blue,
Color.red, Color.blue, Color.blue, Color.blue, Color.blue,
Color.red, Color.red };
JButton[] btns = new JButton[20];
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton();
btns[i].setLabel(labels[i]);
btns[i].setForeground(colors[i]);
btns[i].setFont(font);
p3_2.add(btns[i]);
btns[i].addActionListener(this);
}
return p3_2;
}
public void actionPerformed(ActionEvent e) {
JButton jbt = (JButton) e.getSource();
String label = jbt.getText();
if (label.equals("0") || label.equals("1") || label.equals("2")
|| label.equals("3") || label.equals("4") || label.equals("5")
|| label.equals("6") || label.equals("7") || label.equals("8")
|| label.equals("9")) {
tf.setText(tf.getText() + label);
} else if (label.equals("+") || label.equals("-") || label.equals("*")
|| label.equals("/")) {
ope = label;
N1 = Integer.parseInt(tf.getText());
tf.setText("");
} else if (label.equals("=")) {
N2 = Integer.parseInt(tf.getText());
int result = 0;
if (ope.equals("+")) {
result = N1 + N2;
} else if (ope.equals("-")) {
result = N1 - N2;
} else if (ope.equals("*")) {
result = N1 * N2;
} else {
result = N1 / N2;
}
tf.setText(String.valueOf(result));
}
}
public Calc() {
mainPanel.setLayout(new BorderLayout(5, 5));
JPanel p1 = this.createP1();
JPanel p2 = this.createP2();
JPanel p3 = this.createP3();
mainPanel.add(p1, BorderLayout.NORTH);
mainPanel.add(p2, BorderLayout.WEST);
mainPanel.add(p3, BorderLayout.CENTER);
this.getContentPane().add(mainPanel);
mainPanel.setBackground(new Color(212, 208, 200));
this.setSize(350, 320);
}
public static void main(String[] args) {
Calc rf = new Calc();
rf.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
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;
//计算器
public class Calc extends JFrame implements ActionListener {
private JPanel mainPanel = new JPanel();
private Font font = new Font("", Font.PLAIN, 20);
private int N1; // 数1
private int N2; // 数2
private String ope; // 存储运算符
private JTextField tf;
public JPanel createP1() {
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1, 5, 5));
tf = new JTextField();
tf.setFont(font);
p1.add(tf);
return p1;
}
public JPanel createP2() {
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(5, 1, 5, 5));
JButton[] btns = new JButton[5];
String[] labels = new String[] { "", "MC", "MR", "MS", "M+" };
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton(labels[i]);
btns[i].setForeground(Color.red);
btns[i].setFont(font);
p2.add(btns[i]);
}
return p2;
}
public JPanel createP3() {
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout(5, 5));
JPanel p3_1 = this.createP3_1();
JPanel p3_2 = this.createP3_2();
p3.add(p3_1, BorderLayout.NORTH);
p3.add(p3_2, BorderLayout.CENTER);
return p3;
}
public JPanel createP3_1() {
JPanel p3_1 = new JPanel();
p3_1.setLayout(new GridLayout(1, 3, 5, 5));
JButton[] btns = new JButton[3];
String[] labels = new String[] { "Backspace", "CE", "C" };
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton(labels[i]);
btns[i].setForeground(Color.red);
btns[i].setFont(font);
p3_1.add(btns[i]);
}
return p3_1;
}
public JPanel createP3_2() {
JPanel p3_2 = new JPanel();
GridLayout gl = new GridLayout(4, 5, 5, 5);
p3_2.setLayout(gl);
/* 按钮数组 */
String[] labels = new String[] { "7", "8", "9", "/", "sqrt", "4", "5",
"6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+",
"=" };
Color[] colors = new Color[] { Color.blue, Color.blue, Color.blue,
Color.red, Color.blue, Color.blue, Color.blue, Color.blue,
Color.red, Color.blue, Color.blue, Color.blue, Color.blue,
Color.red, Color.blue, Color.blue, Color.blue, Color.blue,
Color.red, Color.red };
JButton[] btns = new JButton[20];
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton();
btns[i].setLabel(labels[i]);
btns[i].setForeground(colors[i]);
btns[i].setFont(font);
p3_2.add(btns[i]);
btns[i].addActionListener(this);
}
return p3_2;
}
public void actionPerformed(ActionEvent e) {
JButton jbt = (JButton) e.getSource();
String label = jbt.getText();
if (label.equals("0") || label.equals("1") || label.equals("2")
|| label.equals("3") || label.equals("4") || label.equals("5")
|| label.equals("6") || label.equals("7") || label.equals("8")
|| label.equals("9")) {
tf.setText(tf.getText() + label);
} else if (label.equals("+") || label.equals("-") || label.equals("*")
|| label.equals("/")) {
ope = label;
N1 = Integer.parseInt(tf.getText());
tf.setText("");
} else if (label.equals("=")) {
N2 = Integer.parseInt(tf.getText());
int result = 0;
if (ope.equals("+")) {
result = N1 + N2;
} else if (ope.equals("-")) {
result = N1 - N2;
} else if (ope.equals("*")) {
result = N1 * N2;
} else {
result = N1 / N2;
}
tf.setText(String.valueOf(result));
}
}
public Calc() {
mainPanel.setLayout(new BorderLayout(5, 5));
JPanel p1 = this.createP1();
JPanel p2 = this.createP2();
JPanel p3 = this.createP3();
mainPanel.add(p1, BorderLayout.NORTH);
mainPanel.add(p2, BorderLayout.WEST);
mainPanel.add(p3, BorderLayout.CENTER);
this.getContentPane().add(mainPanel);
mainPanel.setBackground(new Color(212, 208, 200));
this.setSize(350, 320);
}
public static void main(String[] args) {
Calc rf = new Calc();
rf.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询