<!--[if !supportLists]-->1.<!--[endif]-->界面
public class jiemiantest {
private JTextField input;
public void initUI(){
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(3);//jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//关闭时停止程序
jf.setSize(280, 440);
jf.setLocation(400, 200);
//创建北面板
JPanel northPanel = creatNorthPanel();
jf.add(northPanel, BorderLayout.NORTH);
//创建中心面板
JPanel centerPanel = createCenterPanel();
jf.add(centerPanel, BorderLayout.CENTER);
//设置大小不可改变
jf.setResizable(false);
//设置窗体可见
jf.setVisible(true);
}
//上面板
private JPanel creatNorthPanel(){
JPanel jp = new JPanel();
//设置面板布局
//设置边框布局
jp.setLayout(new BorderLayout());
//设置边框大小
jp.setPreferredSize(new Dimension(0, 60));
jp.setBackground(Color.BLUE);
//设置输入框
input = new JTextField();
jp.add(input,BorderLayout.CENTER);
return jp;
}
//下面板
private JPanel createCenterPanel(){
JPanel jp = new JPanel();
jp.setBackground(Color.CYAN);
//设置为空布局
jp.setLayout(null);
//创建按钮
JButton but_1 = new JButton("1");
JButton but_2 = new JButton("2");
JButton but_3 = new JButton("3");
JButton but_4 = new JButton("4");
JButton but_5 = new JButton("5");
JButton but_6 = new JButton("6");
JButton but_7 = new JButton("7");
JButton but_8 = new JButton("8");
JButton but_9 = new JButton("9");
JButton but_0 = new JButton("0");
JButton but_11 = new JButton("+");
JButton but_12= new JButton("-");
JButton but_13 = new JButton("*");
JButton but_14 = new JButton("/");
JButton but_15 = new JButton("=");
JButton but_16 = new JButton(".");
JButton but_17 = new JButton("C");
JButton but_18 = new JButton("DEL");
//设置按钮的位置
but_17.setBounds(10, 10, 60, 50);
but_18.setBounds(75, 10, 60, 50);
but_14.setBounds(140, 10, 60, 50);
but_13.setBounds(205, 10, 60, 50);
but_7.setBounds(10, 80, 60, 50);
but_8.setBounds(75, 80, 60, 50);
but_9.setBounds(140, 80, 60, 50);
but_12.setBounds(205, 80, 60, 50);
//
but_4.setBounds(10, 150 , 60, 50);
but_5.setBounds(75, 150, 60, 50);
but_6.setBounds(140, 150, 60, 50);
but_11.setBounds(205,150, 60, 50);
//
but_1.setBounds(10, 220, 60, 50);
but_2.setBounds(75, 220, 60, 50);
but_3.setBounds(140, 220, 60, 50);
but_15.setBounds(205, 220, 60, 120);
//
but_0.setBounds(10, 290, 125, 50);
but_16.setBounds(140, 290, 60, 50);
//给按钮添加监听器
MyActionListener mlis = new MyActionListener(input);
but_1.addActionListener(mlis);
but_2.addActionListener(mlis);
but_3.addActionListener(mlis);
but_4.addActionListener(mlis);
but_5.addActionListener(mlis);
but_6.addActionListener(mlis);
but_7.addActionListener(mlis);
but_8.addActionListener(mlis);
but_9.addActionListener(mlis);
but_0.addActionListener(mlis);
but_11.addActionListener(mlis);
but_12.addActionListener(mlis);
but_13.addActionListener(mlis);
but_14.addActionListener(mlis);
but_15.addActionListener(mlis);
but_16.addActionListener(mlis);
but_17.addActionListener(mlis);
but_18.addActionListener(mlis);
//在面板上添加按钮
jp.add(but_1);
jp.add(but_2);
jp.add(but_3);
jp.add(but_4);
jp.add(but_5);
jp.add(but_6);
jp.add(but_7);
jp.add(but_8);
jp.add(but_9);
jp.add(but_0);
jp.add(but_11);
jp.add(but_12);
jp.add(but_13);
jp.add(but_14);
jp.add(but_15);
jp.add(but_16);
jp.add(but_17);
jp.add(but_18);
return jp;
}
public static void main(String[] args) {
jiemiantest jm = new jiemiantest();
jm.initUI();
}
}
<!--[if !supportLists]-->2.<!--[endif]-->监听器
public class MyActionListener implements ActionListener {
private JTextField input;
private String s1 = "";
private String s2;
private String calc;
//通过构造方法传参数
public MyActionListener(JTextField input){
this.input = input;
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
//得到输入框里原来的内容
String str = input.getText();
str += cmd;
if("+".equals(cmd) ||
"-".equals(cmd) ||
"*".equals(cmd) ||
"/".equals(cmd) ){
calc = cmd;
//保存原来的内容
s1 = input.getText();
//输入框清空
input.setText("");
//删除
} else if("DEL".equals(cmd)){
if((input.getText()!=null) && !("".equals(input.getText().trim()))){
String s = input.getText().substring(0, input.getText().length()-1);
input.setText(s);}
}//清空
else if("C".equals(cmd)){
if((input.getText()!=null) && !("".equals(input.getText().trim()))){
String s = input.getText().substring(0, 0);
input.setText(s);}
}
else if("=".equals(cmd)){
s2 = input.getText();
input.setText("");
float n1 = Float.parseFloat(s1);
float n2 = Float.parseFloat(s2);
if("+".equals(calc)){
float n = n1+n2;
input.setText(""+n);
}else if("-".equals(calc)){
float n = n1-n2;
input.setText(""+n);
}else if("*".equals(calc)){
float n = n1*n2;
input.setText(""+n);
}else if("/".equals(calc)){
float n = n1/n2;
input.setText(""+n);}
} else
input.setText(str);
System.out.println(cmd);
}
}
经过测试,已经实现了计算器简单的功能。其中删除和清空的操作不是很理解,需要继续体会一下。