public void add(
Component c,
GridBagConstraints constraints,
int x,
int y,
int w,
int h) {
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
add(c, constraints);
}
}
*/
class AboutDialog extends JDialog {
public AboutDialog(JFrame owner) {
super(owner, "About Jalculator", true);
Container contentPane = getContentPane();
contentPane.add(
new JLabel(
"<HTML><H1><I>Jalculator</I></H1><HR>" + "By ZhuHua</HTML>"),
BorderLayout.CENTER);
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setVisible(false);
}
});
JPanel panel = new JPanel();
panel.add(ok);
contentPane.add(panel,BorderLayout.SOUTH);
我也做了一个,基本仿照WINDOWS计算器
/*
* Created on 2004-11-18
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package basic;
/**
* @author zhuhua
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Jalculator {
public static void main(String[] args) {
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class CalculatorFrame extends JFrame {
public CalculatorFrame() {
setTitle("My Calculator");
setSize(320, 250);
Container contentPane = getContentPane();
//CalculatorPanel panel = new CalculatorPanel();
//contentPane.add(panel);
GridBagLayout layout = new GridBagLayout();
contentPane.setLayout(layout);
result = new JTextField(20);
//result.setColumns(20);
result.setText("0.");
result.setEditable(false);
start = true;
InsertAction insert = new InsertAction();
CommandAction command = new CommandAction();
ClearAction clear = new ClearAction();
InvertAction invertAction = new InvertAction();
MemoryAction memory = new MemoryAction();
BackAction back = new BackAction();
NegationAction negationAction = new NegationAction();
calResult = 0;
memoryResult = 0;
copyResult = 0;
lastCommand = "=";
zero = new JButton("0");
//zero.setSize(20,20);
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
plus = new JButton("+");
minus = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
MC = new JButton("MC");
MR = new JButton("MR");
MS = new JButton("MS");
Mplus = new JButton("M+");
point = new JButton(".");
equal = new JButton("=");
invert = new JButton("1/x");
percent = new JButton("%");
sqrt = new JButton("sqrt");
backspace = new JButton("Backspace");
CE = new JButton("CE");
C = new JButton("C");
negation = new JButton("+/-");
//negation.setSize(10,20);
memoryLabel = new JLabel();
GridBagConstraints constraints = new GridBagConstraints();
//do not resize the component
constraints.fill = GridBagConstraints.NONE;
//determine where to place the component
constraints.anchor = GridBagConstraints.CENTER;
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(dialog == null)
dialog = new AboutDialog(CalculatorFrame.this);
dialog.show();
}
});
}
public void add(
Component c,
GridBagConstraints constraints,
int x,
int y,
int w,
int h) {
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
getContentPane().add(c, constraints);
}
private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
result.setText("");
start = false;
}
result.setText(result.getText() + input);
}
}
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();