import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.jar.JarFile;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorJFrame implements ActionListener
{
private final String[] str =
{ "7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
".", "0", "=", "+"
};
private JFrame frame;
private JButton[ ] buttons;
private JButton reset, back;
private JTextField display;
private JPanel panel1, panel2,panel3,panel4,panel5;
private JLabel label;
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
CalculatorJFrame that = new CalculatorJFrame();
that.Layout();
}
public void Layout()
{
frame = new JFrame("Calculator");
Container contentpane = frame.getContentPane();
frame.setLayout(new BorderLayout());
label=new JLabel("計算器");
label.setForeground(Color.BLACK);
label.setHorizontalAlignment(label.CENTER);
display = new JTextField("0");
display.setHorizontalAlignment(JTextField.RIGHT);
reset=new JButton("C");
back=new JButton("back");
panel3 = new JPanel(new GridLayout(4, 4));
buttons = new JButton[str.length];
for (int i = 0; i < str.length; i++)
{
buttons[i] = new JButton(str[i]);
panel3.add(buttons[i]);
buttons[i].addActionListener(this);
}
panel1=new JPanel(new BorderLayout());
panel1.add("North",label);
panel1.add("South",display);
panel2=new JPanel(new GridLayout(1,2));
panel2.add(reset);
panel2.add(back);
panel4=new JPanel(new BorderLayout());
panel4.add("North",panel2);
panel4.add("Center",panel3);
contentpane.add("North",panel1);
contentpane.add("Center",panel4);
frame.setSize(200, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object target = e.getSource();
String label = e.getActionCommand();
check(label);
}
public void check(String label)
{
if ("0123456789".indexOf(label) >= 0)
{
if(display.getText().equals("0"))
{
display.setText(" ");
show(label);
}
else
{
show(label);
}
}
else if(".+-*/".indexOf(label) >= 0)
{
if(display.getText().indexOf(".") < 0)
{
show(label);
}
}
}
public void show(String label)
{
String s=display.getText();
if(s.equals("0"))
{
}
display.setText(s+label);
}
}