我的程序没有显示计算器的JTextField部分,并且清晰的JButton无法重置计算器,因此当我清除按钮并尝试执行新计算时,我收到运行时错误。 BTW布尔值应该是操作选择
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class calculator extends JFrame
{
private JButton num1;
private JButton num2;
private JButton num3;
private JButton num4;
private JButton num5;
private JButton num6;
private JButton num7;
private JButton num8;
private JButton num9;
private JButton num0;
private JButton equal;
private JButton addition;
private JButton sub;
private JButton mult;
private JButton divide;
private JButton modulus;
private JButton solve;
private JButton clear;
private boolean s=false;
private boolean equalsClicked=false;
private double val;
private String temp1,temp2,answer;
private JTextField answerfield;
private JPanel contentPane;
private char operation=' ';
public calculator(String x)
{
super(x);
answerfield=new JTextField();
answerfield.setEditable(false);
answerfield.setPreferredSize(new Dimension(160, 20));
answerfield.setBackground(Color.WHITE);
answerfield.setEnabled(false);
answerfield.setHorizontalAlignment(4);
answerfield.setDisabledTextColor(Color.BLACK);
contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.add(answerfield,BorderLayout.NORTH);
setBounds(400,300,255,400);
this.setContentPane(contentPane);
Numbers n=new Numbers();
Calculation c=new Calculation();
num1=new JButton("1");
num1.setBounds(10, 130, 52, 37);
add(num1);
num1.setActionCommand("1");
num1.addActionListener(n);
num2=new JButton("2");
num2.setBounds(72, 130, 52, 37);
add(num2);
num2.setActionCommand("2");
num2.addActionListener(n);
num3=new JButton("3");
num3.setBounds(134, 130, 52, 37);
add(num3);
num3.setActionCommand("3");
num3.addActionListener(n);
num4=new JButton("4");
num4.setBounds(10, 178, 52, 37);
add(num4);
num4.setActionCommand("4");
num4.addActionListener(n);
num5=new JButton("5");
num5.setBounds(72, 178, 52, 37);
add(num5);
num5.setActionCommand("5");
num5.addActionListener(n);
num6=new JButton("6");
num6.setBounds(134, 178, 52, 37);
add(num6);
num6.setActionCommand("6");
num6.addActionListener(n);
num7=new JButton("7");
num7.setBounds(10, 226, 52, 37);
add(num7);
num7.setActionCommand("7");
num7.addActionListener(n);
num8=new JButton("8");
num8.setBounds(72, 226, 52, 37);
add(num8);
num8.setActionCommand("8");
num8.addActionListener(n);
num9=new JButton("9");
num9.setBounds(134, 226, 52, 37);
add(num9);
num9.setActionCommand("9");
num9.addActionListener(n);
num0=new JButton("0");
num0.setBounds(10, 274, 52, 37);
add(num0);
num0.setActionCommand("0");
num0.addActionListener(n);
equal=new JButton("=");
equal.setBounds(72, 274, 52, 37);
add(equal);
equal.setActionCommand("=");
equal.addActionListener(c);
addition=new JButton("+");
addition.setBounds(193, 130,45, 35);
add(addition);
addition.setActionCommand("+");
addition.addActionListener(c);
sub=new JButton("-");
sub.setBounds(193, 179, 45, 35);
add(sub);
sub.setActionCommand("-");
sub.addActionListener(c);
mult=new JButton("*");
mult.setBounds(193,226, 45, 35);
add(mult);
mult.setActionCommand("*");
mult.addActionListener(c);
divide=new JButton("/");
divide.setBounds(193, 274, 45, 35);
add(divide);
divide.setActionCommand("/");
divide.addActionListener(c);
modulus=new JButton("%");
modulus.setBounds(193,322, 45, 35);
add(modulus);
modulus.setActionCommand("%");
modulus.addActionListener(c);
clear=new JButton("C");
clear.setBounds(53, 343, 89, 23);
add(clear);
clear.setActionCommand("C");
clear.addActionListener(c);
setVisible(true);
show();
setResizable(false);
}
private class Numbers implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JButton x=(JButton)event.getSource();
if(x.equals(num1))
{
if(s==false)
{
if(temp1==null)
{
temp1="1";
}
else
temp1+="1";
}
else
{
if(temp2==null)
{
temp2="1";
}
else
temp2+="1";
}
}
if(x.equals(num2))
{
if(s==false)
{
if(temp1==null)
{
temp1="2";
}
else
temp1+="2";
}
else
{
if(temp2==null)
{
temp2="2";
}
else
temp2+="2";
}
}
if(x.equals(num3))
{
if(s==false)
{
if(temp1==null)
{
temp1="3";
}
else
temp1+="3";
}
else
{
if(temp2==null)
{
temp2="3";
}
else
temp2+="3";
}
}
if(x.equals(num4))
{
if(s==false)
{
if(temp1==null)
{
temp1="4";
}
else
temp1+="4";
}
else
{
if(temp2==null)
{
temp2="4";
}
else
temp2+="4";
}
}
if(x.equals(num5))
{
if(s==false)
{
if(temp1==null)
{
temp1="5";
}
else
temp1+="5";
}
else
{
if(temp2==null)
{
temp2="5";
}
else
temp2+="5";
}
}
if(x.equals(num6))
{
if(s==false)
{
if(temp1==null)
{
temp1="6";
}
else
temp1+="6";
}
else
{
if(temp2==null)
{
temp2="6";
}
else
temp2+="6";
}
}
if(x.equals(num7))
{
if(s==false)
{
if(temp1==null)
{
temp1="7";
}
else
temp1+="7";
}
else
{
if(temp2==null)
{
temp2="7";
}
else
temp2+="7";
}
}
if(x.equals(num8))
{
if(s==false)
{
if(temp1==null)
{
temp1="8";
}
else
temp1+="8";
}
else
{
if(temp2==null)
{
temp2="8";
}
else
temp2+="8";
}
}
if(x.equals(num9))
{
if(s==false)
{
if(temp1==null)
{
temp1="9";
}
else
temp1+="9";
}
else
{
if(temp2==null)
{
temp2="9";
}
else
temp2+="9";
}
}
if(x.equals(num0))
{
if(s==false)
{
if(temp1==null)
{
temp1="0";
}
else
temp1+="0";
}
else
{
if(temp2==null)
{
temp2="0";
}
else
temp2+="0";
}
}
if(equalsClicked==false)
{
if(s==false)
{
answerfield.setText(temp1);
}
else
{
answerfield.setText(temp2);
}
}
}
}
public void Clear()
{
temp1="";
temp2="";
s=false;
operation=' ';
val=0;
answer="";
equalsClicked=false;
System.out.println("hey");
}
private class Calculation implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JButton x=(JButton)event.getSource();
if(x.equals(addition))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='+';
}
}
}
if(x.equals(sub))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='-';
}
}
}
if(x.equals(mult))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='*';
}
}
}
if(x.equals(divide))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='/';
}
}
}
if(x.equals(modulus))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='%';
}
}
}
if(x.equals(equal))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
System.out.println("choose numbers");
}
if(temp1!=null&&temp2!=null)
{
double d=0.0;
double s=0.0;
d=Double.parseDouble(temp1);
s=Double.parseDouble(temp2);
switch(operation)
{
case '+':
{
val=d+s;
break;
}
case '-':
{
val=d-s;
break;
}
case'*':
{
val=d*s;
break;
}
case'/':
{
val=d/s;
break;
}
case'%':
{
val=d%s;
break;
}
}
answer=Double.toString(val);
answerfield.setText(answer);
System.out.println(answer);
}
}
if(x.equals(clear))
{
Clear();
}
}
}
public static void main(String args[])
{
new calculator("math");
}
}
答案 0 :(得分:1)
首先你这样做......
answerfield = new JTextField();
answerfield.setEditable(false);
answerfield.setPreferredSize(new Dimension(160, 20));
answerfield.setBackground(Color.WHITE);
answerfield.setEnabled(false);
answerfield.setHorizontalAlignment(4);
answerfield.setDisabledTextColor(Color.BLACK);
这一切看起来都很酷......
但是你这样做......
contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.add(answerfield, BorderLayout.NORTH);
... answerfield
没有定义的尺寸或位置,因此它的尺寸为0x0
,其位置为0x0
。 Swing很聪明,不会画它...
答案是,不要使用null
布局......
避免使用null
布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正
请查看Why is it frowned upon to use a null layout in SWING?和Laying Out Components Within a Container了解详情
更新了布局示例
此示例使用GridBagLayout
,它可能是最强大的默认布局管理器之一,但它也是最难使用的一个...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Calculator extends JFrame {
private boolean s = false;
private boolean equalsClicked = false;
private double val;
private String temp1, temp2, answer;
private JTextField answerfield;
private char operation = ' ';
public Calculator(String x) {
super(x);
answerfield = new JTextField();
answerfield.setEditable(false);
answerfield.setBackground(Color.WHITE);
answerfield.setEnabled(false);
answerfield.setHorizontalAlignment(4);
answerfield.setDisabledTextColor(Color.BLACK);
answerfield.setFont(answerfield.getFont().deriveFont(Font.BOLD, 24));
JPanel field = new JPanel(new BorderLayout());
field.setBorder(new EmptyBorder(4, 4, 0, 4));
field.add(answerfield);
add(field, BorderLayout.NORTH);
JPanel numbers = new JPanel(new GridBagLayout());
Numbers n = new Numbers();
Calculation c = new Calculation();
addButtonTo(numbers, "1", n, 0, 0);
addButtonTo(numbers, "2", n, 1, 0);
addButtonTo(numbers, "3", n, 2, 0);
addButtonTo(numbers, "+", c, 3, 0);
addButtonTo(numbers, "4", n, 0, 1);
addButtonTo(numbers, "5", n, 1, 1);
addButtonTo(numbers, "6", n, 2, 1);
addButtonTo(numbers, "-", c, 3, 1);
addButtonTo(numbers, "7", n, 0, 2);
addButtonTo(numbers, "8", n, 1, 2);
addButtonTo(numbers, "9", n, 2, 2);
addButtonTo(numbers, "*", c, 3, 2);
addButtonTo(numbers, "0", n, 0, 3);
addButtonTo(numbers, "=", c, 1, 3);
addButtonTo(numbers, "", null, 2, 3);
addButtonTo(numbers, "/", n, 3, 3);
addButtonTo(numbers, "C", c, 0, 4, 3);
addButtonTo(numbers, "%", c, 3, 4);
add(numbers);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY) {
addButtonTo(panel, text, listener, gridX, gridY, 1);
}
protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY, int gridWidth) {
JComponent comp = null;
if (text == null || text.trim().isEmpty()) {
comp = new JPanel();
} else {
JButton button = new JButton(text);
button.setActionCommand(text);
button.addActionListener(listener);
comp = button;
}
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridX;
gbc.gridy = gridY;
gbc.gridwidth = gridWidth;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.fill = GridBagConstraints.BOTH;
panel.add(comp, gbc);
}
private class Numbers implements ActionListener {
public void actionPerformed(ActionEvent event) {
JButton x = (JButton) event.getSource();
String text = x.getActionCommand();
if (s == false) {
if (temp1 == null) {
temp1 = text;
} else {
temp1 += text;
}
} else {
if (temp2 == null) {
temp2 = text;
} else {
temp2 += text;
}
}
if (equalsClicked == false) {
if (s == false) {
answerfield.setText(temp1);
} else {
answerfield.setText(temp2);
}
}
}
}
public void Clear() {
temp1 = "";
temp2 = "";
s = false;
operation = ' ';
val = 0;
answer = "";
equalsClicked = false;
System.out.println("hey");
answerfield.setText("");
}
private class Calculation implements ActionListener {
public void actionPerformed(ActionEvent event) {
JButton x = (JButton) event.getSource();
String text = x.getActionCommand();
System.out.println(text);
switch (text) {
case "+":
case "-":
case "*":
case "/":
case "%":
if (temp1 == null) {
System.out.println("choose numbers");
} else {
if (temp1 != null && temp2 == null) {
s = true;
operation = text.charAt(0);
}
}
break;
case "=":
if (temp1 == null) {
System.out.println("choose numbers");
} else {
if (temp1 != null && temp2 == null) {
System.out.println("choose numbers");
}
}
if (temp1 != null && temp2 != null) {
double d = 0.0;
double s = 0.0;
d = Double.parseDouble(temp1);
s = Double.parseDouble(temp2);
switch (operation) {
case '+': {
val = d + s;
break;
}
case '-': {
val = d - s;
break;
}
case '*': {
val = d * s;
break;
}
case '/': {
val = d / s;
break;
}
case '%': {
val = d % s;
break;
}
}
answer = Double.toString(val);
answerfield.setText(answer);
System.out.println(answer);
}
break;
case "C":
Clear();
break;
}
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
new Calculator("math");
}
});
}
}