//这是button组件的类,谢谢帮忙修改,在线等
package Graph_Calculator;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ButtonPanel implements ActionListener {
public ButtonPanel()
{
//txt.setSize(200,80);
cPanel1.add(txt,BorderLayout.NORTH);
for(int i = 0; i <= 15; i++)
{
button[i] = new JButton(s[i]);
button[i].addActionListener(this);
cPanel2.add(button[i]);
}
//cPanel1.add(cPanel2, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
ready = txt.getText();
String etxt = e.getActionCommand();
txt.setText(ready + etxt);
if (etxt.equals("+"))
{
flag = etxt;
}
if(e.getActionCommand().equals(etxt))
flag = etxt;
if(etxt.equals("*"))
flag = etxt;
if(etxt.equals("/"))
flag = etxt;
if(etxt.equals("."))
flag = etxt;
else if(f2 == 0f&&flag!=null)
{
f1 = Float.parseFloat(ready + etxt);
}
else if(f2 == 0f && flag!=null)
{
f2 = Float.parseFloat(etxt);
}
else if (f2 != 0f && flag != null)
{
f2 = f2 * 10 +Float.parseFloat(etxt);
}
if(f1!=0f && f2 != 0f && etxt.equals("="))
{
txt.setText(ready +flag + Result());
}
}
public float Result()
{
if(flag.equals("+") )
result = f1 + f2;
if(flag.equals("-"))
result = f1 - f2;
if(flag == "*")
result = f1 * f2;
if(flag == "/")
result = f1 / f2;
return result;
}
public JPanel getCPanel1() {
return cPanel1;
}
public JPanel getCPanel2() {
return cPanel2;
}
private JButton[] button = new JButton[16];
private String s[] = {"7","8","9","+","4","5","6","-","1","2","3","*","/",".","0","="};
private JPanel cPanel1 = new JPanel();
private JPanel cPanel2 = new JPanel(new GridLayout(4,4));
private JTextField txt = new JTextField(10);
private String ready = "";
private float f1=0f,f2=0f;
private String flag =null;
private float result ;
}
------解决方案--------------------
你的代码逻辑比较乱, 给你改了下.
- Java code
package com.easteq.test;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;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.LineBorder;public class ButtonPanel implements ActionListener { public ButtonPanel() { // txt.setSize(200,80); cPanel1.add(txt, BorderLayout.NORTH); for (int i = 0; i <= 15; i++) { button[i] = new JButton(s[i]); button[i].addActionListener(this); cPanel2.add(button[i]); } // cPanel1.add(cPanel2, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { ready = txt.getText(); String etxt = e.getActionCommand(); String newTxt = ""; if (etxt.equals(".")) { newTxt = ready + etxt; } else if (etxt.equals("+") || etxt.equals("-") || etxt.equals("*") || etxt.equals("/")) { flag = etxt; newTxt = ready + etxt; } else if (etxt.equals("=")) { if (flag != null && f2 != 0f) { float result = Result(); f1 = result; flag = null; newTxt = String.valueOf(result); } } else { // now, numbers if (flag == null) { f1 = Float.parseFloat(ready + etxt); } else { f2 = Float.parseFloat(etxt);; } newTxt = ready + etxt; } txt.setText(newTxt); } public float Result() { System.out.println("Getting result of flag:" + flag); if (flag.equals("+")) result = f1 + f2; if (flag.equals("-")) result = f1 - f2; if (flag == "*") result = f1 * f2; if (flag == "/") result = f1 / f2; return result; } public JPanel getCPanel1() { return cPanel1; } public JPanel getCPanel2() { return cPanel2; } private JButton[] button = new JButton[16]; private String s[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "/", ".", "0", "=" }; private JPanel cPanel1 = new JPanel(); private JPanel cPanel2 = new JPanel(new GridLayout(4, 4)); private JTextField txt = new JTextField(10); private String ready = ""; private float f1 = 0f, f2 = 0f; private String flag = null; private float result; public static void main(String[] args) { JFrame frame = new JFrame(); ButtonPanel pan = new ButtonPanel(); frame.getContentPane().add(pan.getCPanel1(), "Center"); frame.getContentPane().add(pan.getCPanel2(), "South"); frame.setSize(200, 200); frame.setVisible(true); }}