import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculator extends JFrame implements ActionListener
{
Button[] bu = new Button [17];
TextField te = new TextField(16);
Panel pa1 = new Panel();
Panel pa2 = new Panel();
Panel pa3 = new Panel();
Calculator(String s)
{
super(s);
setSize(200,200);
pa1.setSize(200,50);
pa2.setSize(150,150);
pa3.setSize(50,150);
setVisible(true);
setLayout(new BorderLayout());
pa1.setLayout(new FlowLayout());
pa2.setLayout(new GridLayout(4,3));
pa3.setLayout(new GridLayout(4,1));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pa1.add(te);
pa1.add(bu[15] = new Button(String.valueOf("清除")));bu[15].addActionListener(this);
for(int i=0;i<17;i++)
{
if(i<9){pa2.add(bu[i] = new Button(String.valueOf(i+1)));bu[i].addActionListener(this);}
else if(i == 9){pa2.add(bu[i] = new Button(String.valueOf(".")));bu[i].addActionListener(this);}
else if(i == 10){pa2.add(bu[i] = new Button(String.valueOf("0")));bu[i].addActionListener(this);}
else if(i == 11){pa2.add(bu[i] = new Button(String.valueOf("=")));bu[i].addActionListener(this);}
else if(i == 12){pa3.add(bu[i] = new Button(String.valueOf("+")));bu[i].addActionListener(this);}
else if(i == 13){pa3.add(bu[i] = new Button(String.valueOf("-")));bu[i].addActionListener(this);}
else if(i == 14){pa3.add(bu[i] = new Button(String.valueOf("*")));bu[i].addActionListener(this);}
else if(i == 16){pa3.add(bu[i] = new Button(String.valueOf("/")));bu[i].addActionListener(this);}
}
add("North",pa1);
add("Center",pa2);
add("East",pa3);
}
public void actionPerformed(ActionEvent e)
{
}
}
public class CalculatorApp
{
public static void main(String[] args)
{
new Calculator("计算器");
}
}
------ Solution ------------------------------------- -------
setPreferedSize
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculator extends JFrame implements ActionListener {
Button[] bu = new Button[17];
TextField te = new TextField(16);
Panel pa1 = new Panel();
Panel pa2 = new Panel();
Panel pa3 = new Panel();
Calculator(String s) {
super(s);
setSize(200, 200);
pa1.setSize(200, 50);
pa2.setSize(150, 150);
pa3.setSize(50, 150);
pa3.setPreferredSize(new Dimension(50, 150));
setVisible(true);
setLayout(new BorderLayout());
pa1.setLayout(new FlowLayout());
pa2.setLayout(new GridLayout(4, 3));
pa3.setLayout(new GridLayout(4, 1));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pa1.add(te);
pa1.add(bu[15] = new Button(String.valueOf("清除")));
bu[15].addActionListener(this);
for (int i = 0; i < 17; i++) {
if (i < 9) {
pa2.add(bu[i] = new Button(String.valueOf(i + 1)));
bu[i].addActionListener(this);
} else if (i == 9) {
pa2.add(bu[i] = new Button(String.valueOf(".")));
bu[i].addActionListener(this);
} else if (i == 10) {
pa2.add(bu[i] = new Button(String.valueOf("0")));
bu[i].addActionListener(this);
} else if (i == 11) {
pa2.add(bu[i] = new Button(String.valueOf("=")));
bu[i].addActionListener(this);
} else if (i == 12) {
pa3.add(bu[i] = new Button(String.valueOf("+")));
bu[i].addActionListener(this);
} else if (i == 13) {
pa3.add(bu[i] = new Button(String.valueOf("-")));
bu[i].addActionListener(this);
} else if (i == 14) {
pa3.add(bu[i] = new Button(String.valueOf("*")));
bu[i].addActionListener(this);
} else if (i == 16) {
pa3.add(bu[i] = new Button(String.valueOf("/")));
bu[i].addActionListener(this);
}
}
add("North", pa1);
add("Center", pa2);
add("East", pa3);
}
public void actionPerformed(ActionEvent e) {
}
}
public class CalculatorApp {
public static void main(String[] args) {
new Calculator("计算器");
}
}
------ For reference only ----------------------------------- ----
Thank
没有评论:
发表评论